• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Should I store seo-friendly slugs (for use in pretty URLs) in the database?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some database entities


And URLs are supposed to be the following:

site.com/category-name
site.com/category-name/product-name

where category-name and product-name are slugifyed category.name and product.name respectively. The question is what is the best design pattern: add one more field slugifiedName to the database entities Category and Product (and to the database)? Or to generate slugified names on fly? First variant is easy but seems to be not beautiful.
It seems to be better not to store duplicated names in the database. But then when should I generate them?







 
Ekaterina Galkina
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose It's better to generate slugified links when creating client web page.
 
Ekaterina Galkina
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ekaterina Galkina wrote:I suppose It's better to generate slugified links when creating client web page.



But the problem is how to recognize them in @RequestMapping if they are not in the database and can be very different
 
Ekaterina Galkina
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be to create the 3th table with slugs?



???
Seems to be even worse...
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slugs are not interesting to your application, only to the viewer. The client can generate them and then rewrite the URL. Your application should work regardless of whether a slug is added to the URL.
 
Ekaterina Galkina
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, didn't quite understand. How can client rewrite urls? using JS?

Anyway how can my application process request, e.g. site.com/best-wi-routers/ if the slug "best-wi-routers" is not stored in the database?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't. Slugs are not intended to be processed. They're just a piece of text that make the URL more readable to the user. You still need to include your entity IDs in your URL and use those to identify your resource. That means an URL could look something like this:

/categories/3/products/14/best-wi-routers

Your controller then only processes the category ID and the product ID, and it ignores the slug at the end.

Yes, you can let the client rewrite the URL using JavaScript, or you can redirect the browser when it browses to a URL that has a missing or wrong slug. It depends on how important it is to you that the slug is always correct.
 
Ekaterina Galkina
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You don't. Slugs are not intended to be processed. They're just a piece of text that make the URL more readable to the user. You still need to include your entity IDs in your URL and use those to identify your resource. That means an URL could look something like this:

/categories/3/products/14/best-wi-routers

Your controller then only processes the category ID and the product ID, and it ignores the slug at the end.



Yes, this approach is clear. Probably I'll use it.

But the matter is that I saw many sites without ids, especially wordpress blogs often don't use any ids. But now I think it's unnecessary complexity, at least I'll start without it.

 
Tell me how it all turns out. Here is a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic