Next.js Discord

Discord Forum

Next.js with React-Router and static export

Unanswered
Short mackerel posted this in #help-forum
Open in Discord
Short mackerelOP
If I use react-router-dom to handle routing in the next.js app can I export that as static HTML site without running a server?

20 Replies

Using react-router-dom is more of an antipattern in nextjs as next comes with its own routing. [Read more about static export.](https://nextjs.org/docs/app/building-your-application/deploying/static-exports)
Short mackerelOP
My concern with next js routing is that I cannot use dynamic routes with static export
Red harvester ant
no you can use dynamic routes.
by using []
no you can't use dynamic pages with static export
Red harvester ant
we can use /page/product/[id]. that is the way to make it possible.
Red harvester ant
You mean that when build time?
@gin why do u want to do that tho?
Short mackerelOP
I need to use dynamic route with static export
Red harvester ant
what about this?

/product/[id]/page.js

// This is a server-side component by default
async function getProduct(id) {
const res = await fetch(https://path/products/${id});
if (!res.ok) {
....
}
const product = await res.json();
return product;
}

export default async function ProductPage({ params }) {
const { id } = params;

const product = await getProduct(id);

return (
<div>
<p>{product.name}</p>
<p>Price: ${product.price}</p>
</div>
);
}
Red harvester ant
oh... I see
you use AWS
I will ask my senior. 🙂
@Short mackerel My concern with next js routing is that I cannot use dynamic routes with static export
If you can list all the slugs of all dynamic routes you have, then you can do it in the app router in static export.

If you can’t list the slugs and would like to use client side rendering, then it’s not possible to do in the app router but possible in the pages router.

If you use the pages router you might be able to use react-router but that’s strongly discouraged because nextjs brings its own router and react-router may likely not play well with the nextjs router.