I have a Next.js page, where the pages are statically generated (using next export
). It's a dynamic route, but I will only fetch data on the client after the initial page load (in a useEffect
).
The path is something like: /pages/foo/[id].tsx
. This is in fact the only route which is going to exist.
My problem is:
- If I access the URL directly (e.g. typing in https://mysite.fake/foo/1337 into the URL bar) I get a 404.
- If I navigate to that route using a
<Link />
it does work as expected - If I then reload the page, I get a 404 again.
- On local dev, it works fine. The problem only exists when deployed.
The page in question is using the router.query
, but I have the same problem with a completely static page (e.g. just /pages/bar/baz.tsx
).
Example:
export const Home: NextPage = () => {
const router = useRouter()
const { id } = router.query
const headerText = id ? `Hello ${id.toString()}` : ''
// in a later iteration, I will use the query id to fetch data in a useEffect
return <h1>{headerText}</h1>
}
As far as I understand from the Next.js documentation on dynamic routing and data fetching this should be possible to do.
My next.config.js
is very bare:
/** @type {import('next').NextConfig} */
module.exports = {
reactStrictMode: true,
}
What am I doing wrong?