How to achieve URL like page/[id]-[slug] ?
Answered
Upland Sandpiper posted this in #help-forum
Upland SandpiperOP
Hello there.
I'm looking for the way to get a URL structure like this :
page/[id]-[slug]
I know how to make like this :
page/[id]/[slug]
but I need to have a structure like
page/[id]-[slug].
I tried :
app/[id]-[slug]/page.tsx :
but id and slug are undefined.
Please help me 🙂
I'm looking for the way to get a URL structure like this :
page/[id]-[slug]
I know how to make like this :
page/[id]/[slug]
but I need to have a structure like
page/[id]-[slug].
I tried :
app/[id]-[slug]/page.tsx :
export default function Page({
params,
}: {
params: { catId: string; catSlug: string };
}) {
console.log("catId : ", params.catId);
console.log("catSlug : ", params.catSlug);
return (
<>
<p>catId : {params.catId}</p>
</>
);
}but id and slug are undefined.
Please help me 🙂
Answered by B33fb0n3
@Upland Sandpiper you can add a page/[slug] and then get the slug. Then you should have an slug like "id-slug". This string can be splitted at "-". So you can get id and the slug like
"id-slug".split("-") the first element will be the id and the second the slug49 Replies
@Upland Sandpiper you can add a page/[slug] and then get the slug. Then you should have an slug like "id-slug". This string can be splitted at "-". So you can get id and the slug like
"id-slug".split("-") the first element will be the id and the second the slugAnswer
Upland SandpiperOP
yes thanks. But :
[id] and [slug] are separated fields in my database. So my slug don't have id as prefix.
[id] and [slug] are separated fields in my database. So my slug don't have id as prefix.
ok
Upland SandpiperOP
I mean in my database, slugs are only like : "my-product"
ok, but where is the problem then? Someone request the url https://example.de/id-slug
And you can get both to do your stuff
And you can get both to do your stuff
Upland SandpiperOP
the goal is to get a page with the ID whatever the slug is changed or not
so the slug doesn't matter? Only the id is important?
Upland SandpiperOP
yes because sometimes we have to change the name of the page and so the slug is changed
just to get this right: you sometimes change the slug? The slug, that is a unique id to your specific object in your database. Have I understood that right?
Upland SandpiperOP
you're right the slug has to be unique
let's take an example of a product page
I create a product :
12-apple-macbook
A few days later, I update the name of the product to "apple-macbook-air" but the ID is still the same, so the URL changed :
12-apple-macbook-air
12-apple-macbook
A few days later, I update the name of the product to "apple-macbook-air" but the ID is still the same, so the URL changed :
12-apple-macbook-air
A 301 redirection should be done.
so the id is the unique key to your item, right?
Upland SandpiperOP
yes
I understood, that you safe it as id-slug, yeah, just to get this right lol
Upland SandpiperOP
we get the content thanks to the ID
the slug is for the SEO
ok, and which part is missing?
Imagine we go on an e-commerce site.
In which part is the problem:
1. You list your products,
2. The user click one of the products (-> redirect to /prod_id_12345)
3. The productpage meta will be loaded
4. The productpage loads the specific product
Imagine we go on an e-commerce site.
In which part is the problem:
1. You list your products,
2. The user click one of the products (-> redirect to /prod_id_12345)
3. The productpage meta will be loaded
4. The productpage loads the specific product
Upland SandpiperOP
when I want to access to : "12-apple-macbook" page
@Upland Sandpiper when I want to access to : "12-apple-macbook" page
ok, so part 2,3 or 4?
Upland SandpiperOP
2
In part 1 we fetched the products and got a list of available products. These products have a unique id (in your case
Part 2 clear for you?
id-slug). We can either render link elements or use a client component to redirect the user to this specific id or maybe even an other way I forget right now. Yea ONLY the id, not the slug. So split it like I messaged and redirect the user to your productpage.Part 2 clear for you?
Upland SandpiperOP
Ok it's clearer
what are you missing?
Upland SandpiperOP
part 1 :
<Link href={id}-{slug}>
{name}
</Link>
<Link href={id}-{slug}>
{name}
</Link>
Part 1:
const products = getProducts();
render:
{products.map((product) => ...
splittedString = ...
<Link href={splittedString[0]}>
{name}
</Link>
}Upland SandpiperOP
splittedString ???
oh ok it's pretty same
yea:
This string can be splitted at "-". So you can get id and the slug like This string can be splitted at "-". So you can get id and the slug like https://nextjs-forum.com/post/1179477766211645461#message-1179479584006221894
Upland SandpiperOP
with the mapping for the loop
yes thanks
Part 1 clear?
Part 2 clear?
Upland SandpiperOP
I don't know why I wanted to create a folder like "[id]-[slug]"
yes
a folder like {slug] is enough
then I get the id from this slug param
@Upland Sandpiper a folder like {slug] is enough
exactly what I wanted to tell lol
Upland SandpiperOP
thanks a lot
no problem ^^
Please mark this as the solution: https://nextjs-forum.com/post/1179477766211645461#message-1179479584006221894
Please mark this as the solution: https://nextjs-forum.com/post/1179477766211645461#message-1179479584006221894
Upland SandpiperOP
just another question
please 🙂
the page must be client ?
the page don't need to be a client. It can be a server OR client component 🙂
Upland SandpiperOP
yes because we don't use pathname right ? we get the slug from the params
server component by default so
https://nextjs-forum.com/post/1179477766211645461#message-1179484633356828754
In your product detailpage you only get the unique id to the product:
Fetch the product and maybe use the slug. If you just want to have the slug for metadata, use generatemetadata
In your product detailpage you only get the unique id to the product:
Yea ONLY the id, not the slug
Fetch the product and maybe use the slug. If you just want to have the slug for metadata, use generatemetadata
Upland SandpiperOP
ok