Capture a dynamic URL in a server component
Answered
West Siberian Laika posted this in #help-forum
West Siberian LaikaOP
Hi, I made this directory
I tried this approach:
but the only time it works is when I save the file while being on that directory(/product/[id]).
When I try to access the product from the root or just refresh the page on
/app/product/[id], I wanted to grab the id from the URL to use it my SQL query but I couldn't figure out how to do that on the server side.I tried this approach:
import { headers } from "next/headers";
import { getProductById } from "@/lib/actions";
export default async function Page() {
function getId() {
const headersList = headers();
const pathname = headersList.get("referer");
const index = pathname?.indexOf("/product");
const id = pathname?.slice((index as any) + 9);
return id;
}
const id = getId();
const fetchedProduct = await getProductById(id as string);
console.log(fetchedProduct);
return (
<div>
{fetchedProduct?.map((item) => (
<h1>{item?.title}</h1>
))}
</div>
);
}but the only time it works is when I save the file while being on that directory(/product/[id]).
When I try to access the product from the root or just refresh the page on
/product/[id], it doesn't catch the correct URL(this is the pathname it sends http://localhost:3000, not the expected http://localhost:3000/product/[id])Answered by ᴉuɐpɹɐɐ
You can get
https://nextjs.org/docs/app/api-reference/file-conventions/page#params-optional
id from the parameter of the page component that you default export in page.tsxhttps://nextjs.org/docs/app/api-reference/file-conventions/page#params-optional
2 Replies
@West Siberian Laika Hi, I made this directory `/app/product/[id]`, I wanted to grab the id from the URL to use it my SQL query but I couldn't figure out how to do that on the server side.
I tried this approach:
tsx
import { headers } from "next/headers";
import { getProductById } from "@/lib/actions";
export default async function Page() {
function getId() {
const headersList = headers();
const pathname = headersList.get("referer");
const index = pathname?.indexOf("/product");
const id = pathname?.slice((index as any) + 9);
return id;
}
const id = getId();
const fetchedProduct = await getProductById(id as string);
console.log(fetchedProduct);
return (
<div>
{fetchedProduct?.map((item) => (
<h1>{item?.title}</h1>
))}
</div>
);
}
but the only time it works is when I save the file while being on that directory(/product/[id]).
When I try to access the product from the root or just refresh the page on `/product/[id]`, it doesn't catch the correct URL(this is the pathname it sends `http://localhost:3000`, not the expected `http://localhost:3000/product/[id]`)
You can get
https://nextjs.org/docs/app/api-reference/file-conventions/page#params-optional
id from the parameter of the page component that you default export in page.tsxhttps://nextjs.org/docs/app/api-reference/file-conventions/page#params-optional
Answer
@ᴉuɐpɹɐɐ You can get `id` from the parameter of the page component that you default export in page.tsx
https://nextjs.org/docs/app/api-reference/file-conventions/page#params-optional
West Siberian LaikaOP
well that was easier
thank you.
import { headers } from "next/headers";
import { getProductById } from "@/lib/actions";
export default async function Page({ params }: { params: { id: string } }) {
console.log(params.id); // Outputs the id lolthank you.