New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
404 errors with dynamic routes in next.js and now #3294
Comments
I am having a very similar issue but with a server loading a static react app. I’ve followed every tutorial and put the recommended redirects in (including calling res.sendFile for app.get(“/“) to client, build, index.html) but I get the same pattern of dozens of 404s for pretty much everything much like in this screen shot. Slight update in that by calling app.use(“client/build”), my server now finally loads my client build in localhost fine. (I must have gone through four tutorials that failed to mention that) However, when I deploy it with npm run deploy, these kind of 404s still show. |
I’m also experiencing 404s on dynamic/wildcard routes in production, but not when using Repo and routes in question are viewable here. (Some more context in this Twitter conversation with @leo) |
@daneden same here. dynamic routes work locally with |
@daneden same situation {
"version": 2,
"public": false,
"name": "XXXXXXX",
"env": {
},
"builds": [
{ "src": "apps/web/next.config.js", "use": "@now/next" }
],
"routes": [
{ "src": "/(.*)", "dest": "/apps/web/$1" }
]
} And my exportPathMap +/- this: const exportPathMap = async (defaultPathMap, { dev, dir, outDir, distDir, buildId }) => {
const example = [{ slug: 'test1'' }, { slug: 'test2' }];
const pages = example.reduce(
(pages, agency) =>
Object.assign({}, pages, {
[`/test/${example.slug}/review`]: { page: '/test/[slug]/review' },
}),
{}
);
return Object.assign({}, pages, {
'/': { page: '/' },
});
}; Any suggestions ? |
I'm having same issue with dynamic routing inside the api. It gives me a 404: NOT_FOUND error message. So for example I want to call / PASS /[query] 404 NOT FOUND I didn't do anything on the code, it just stop working by it's own. When i push to application to ZEIT it works fine on LIVE, this only happend on "now dev" |
@ivanhueso — I had the same issue as you. I had a little luck by running
The |
@sergioalen @belgattitude @CaptainChemist hey folks, I had a bit of correspondence with someone on the Zeit team over Twitter and came to a resolution. Not sure if the following steps will help you, but they solved my issues. Part of the reason my routes were
I have a commit here which demonstrates these changes. |
None of these solutions work. I'm using a basic Link to a static page. Works in dev, but broken in production. |
I'm in idiot. Fixed it. Any newbies googling the problem, use HTTPS for your backend while developing. Was using HTTP and been getting 404/unexpected errors. UPDATE: It only fixed the unexpected errors. Still getting 404 errors. |
I fixed it, I was using Router.push('/post/[pid]', `/post/${id}`); First parameter is the For <Link href="/post/[pid]" as={`/post/${id}`}>
<a>First Post</a>
</Link>
|
@Ferezoz Thank you for your solution. I searched a lot and try many ways but none of them solved the problem. Your solution is correct and standard. |
When I directly want to open this page not working Edited & Added: Thanks. |
@zinnah1995 Try updating your now CLI. I had the same issue in 17.1.1 |
Hi @LeonidasEsteban. Can you assist? 17.1.1 seems to be the latest release. What did you update to? I am getting 404's on every page, as if my now.json file isn't being registered. |
dynamic api route not found
To expand on @Ferezoz 's note, and for anyone who is dealing with a route that has multiple params, don't forget to cover all push('/[id]/admin/spells/[spell_id]', `/${id}/admin/spells/${spell.id}`); |
I see similar behavior as @zinnah1995 describes. If I go sequentially to e.g.: /something/[param] it works. When you hit reload (like F5) it gives you 404 Any update on that topic ? |
@zinnah1995 @elvenking @d9cre if you are using I think you could check what is been rendered on your static HTML generated pages when doing the I am not sure but by what you are explaining that's what it seems to be happening. Hope it helps. |
@Ferezoz Thank you for reply. In my case, i am not even running |
@Ferezoz I'm currently running into the exact issue you are describing and seemingly the same issue as @zinnah1995. When running The docs for Static HTML Export suggest that dynamic routes should work as normal:
I've checked the rendered HTML and it seems to be fine. I'm not using Any help would be really useful as I'm coming up short with a solution. |
@neefrehman Hey, I just went into your repo and this is how you will fix it for your case: You need to use
You need to add this configuration in Note: I went to your const fs = require("fs");
const path = require("path");
const withCSS = require("@zeit/next-css");
const getSketchArray = () => {
const sketchArray = [];
const sketchDirectory = path.join(process.cwd(), "src/sketches");
const yearFolders = fs
.readdirSync(sketchDirectory)
.filter(folderName => folderName.length === 2);
yearFolders.forEach(yearFolder => {
const yearDirectory = path.join(
process.cwd(),
`src/sketches/${yearFolder}`
);
const monthFolders = fs
.readdirSync(yearDirectory)
.filter(folderName => folderName.length === 2);
monthFolders.forEach(monthFolder => {
const monthDirectory = path.join(
process.cwd(),
`src/sketches/${yearFolder}/${monthFolder}`
);
const sketches = fs
.readdirSync(monthDirectory)
.filter(sketchFileName => sketchFileName.length === 10);
sketches.forEach(sketch => {
const sketchId = sketch.substr(0, 6);
const isValidSketchId = RegExp(/^[0-9]{6}$/).test(sketchId);
if (isValidSketchId) sketchArray.push(sketchId);
});
});
});
return sketchArray.reverse();
};
module.exports = withCSS({
async exportPathMap() { // <---- Here it is the configuration I added
let sketchsPages = {};
getSketchArray().forEach(sketchId => {
sketchsPages = {
...sketchsPages,
[`/${sketchId}`]: { page: "/[sketch]" }
};
});
return sketchsPages;
},
webpack(config) {
...
return config;
}
}); I did build, export and deploy and it worked. Looks like an interesting project by the way, I wonder what are you building. 🙂 |
@Ferezoz Thanks for this! Just copied it over and it did indeed work. Since the issue is a lack of an It does seem a bit counterintuitive to build the page for each And thanks! I'm just making a site to play around with three.js, p5.js and WebGL in general. |
I have the exact same issue as @zinnah1995 - All pages work fine if URL hit directly except the blog post. I can navigate to the posts perfectly fine but sharing the URL and loading it in a new tab gives 404. |
I have thesame issue with @elvenking |
Hi! Same problem here @zinnah1995 , any update ? |
@FrancoTanzarella @mayowadavid @elisegriset92 I had the exact issue (with |
I solved the problem. 😄Problem: I was not able to route directing to the "dynamic" path in production, it was showing 404 Not Found. Using "yarn dev" was working fine. After build, it was not working as expected. Solution: So the problem was building the static site. I'm sure you must not be using "getStaticProps" and "getStaticPaths" in your dynamic page. You should add both of them so that while building Next will build your dynamic path into static pages.
and
Official documentation: https://nextjs.org/docs/basic-features/pages#static-generation-with-data |
HI,
The requested document was not found on this server. It seems to me the router could not be reconginze once deploye to build Does this also has to apply "getStaticProps" and "getStaticPaths" ? Or it could be just add some configuration in nex.config file ? tks |
I got similar problem. I followed https://github.com/vercel/next.js/tree/canary/examples/dynamic-routing to set up my deployment. |
In static site, dynamic urls works in the app e.g. /dashboard/customer/12345/, however it doesn't work when you use /dashboard/customer/12345/ directly. Refreshing will also cause a 404 error. Error seems to occur because there isn't a index.js entry point like how we handle a create react app. It has nothing got to do with router.query parameter because JS and CSS files etc were totally not loaded when you attempt to load the url directly, or refreshing. |
@tesshsu Did you fixed it? I solved it using "getStaticProps" and "getStaticPaths", and was able to get correct page while directly routing to that URL in production. |
I end up just using Vercel, because they use npm run start. I guess NextJS need npm run start in order for their routing to run. |
When you navigate to it okay, if you reload the page => 404 error will occur |
with newly created product / post / item. what happened to the static files/static paths? (need to waiting for next builds ?) |
well i just got some trick, the issue isnt from nextjs but your server. in my case i was deploying it on apache server so i add .htaccess to redirect 404 page to index.js. it's not an exactly solution but it could help someone to get over this 2 years old issue. |
I was also experiencing this 404 issue and it was extremely frustrating. I managed to hack together a solution that solves the problem though. I wrote a hacky script which extracts top-level HTML files from the The caveats are that you can't use any JS and you have to specify hrefs in your The script is specific to my setup and the I have the script in a gist, but I'll paste the whole thing here to ensure there's no possibility of a dead link. Note: This requires Node ~12 or something for recursive directory creation
|
I'm having a similar issue where my dynamic routes in my webapp are getting 404s when I refresh the page. But only on vercel. Locally it works as expected. The app: https://oma3.vercel.app/ The dynamic pages are populated with data from indexedDB which will be unique to every user. So I don't think I can really use static props. Is there anyway to make NextJS behave more like a SPA on vercel? I also created a discussion on NextJS which has seen no traction. |
Hi everyone, EDIT: I just deployed the app in Heroku and it just worked very well, no issues anymore. |
hi, |
I am also running into this issue. Works just fine when built and run locally, but on vercel I am getting a 404 error. |
Running into same issue |
For those working with dynamic routes and deploying a static export to vercel, github pages, etc: You need to map dynamic request paths to your dynamic page destination using the An example assuming a module.exports = {
exportPathMap: async function ( ) {
return {
'/dynamic/route-a': { page: '/dynamic/[routeId]' },
'/dynamic/route-b': { page: '/dynamic/[routeId]' },
}
},
} |
Not sure if this belongs here, but I thought I would share since I don't see any documentation for it. Dynamic routes cannot have dashes
Hope this saves someone the pain I went through to diagnose this! |
I also had a similar issue with dynamic routing inside the api.
The folder structure at the time of the problem was as above, and it was solved by renaming the duplicate api folder and distributing it. |
Hi! I'm experiencing this issue where local dev and production building on local works fine. I'm trying to have an API Catch all route used as a proxy. But I'm getting 404 when I try to call the proxy in a cloud environment. Next.js version 13.0.6 Example: POST /api/proxy/user/signup 404 |
@CVarisco just import in next.config.js file |
Hello Everyone! I"m seeing the same issue. I am building and exporting my site and deploying on cloudflare pages. As mentioned earlier in this thread, I believe that it is because i have a dynamic route and that route is getting rendered to a 404. I believe that this is most in line with my problem, cross posting here: |
@dm03514 have you use this solution ? |
I solved it. At the root of my app, I routed to same URL again on frontend side. So add this code in your _app.tsx file.
|
👆@curlyz solved it for me. I thought I had started from a Next.js template, so I have no idea why the framework in settings was Changing the framework to Next.js fixed it immediately, and correctly deployed all the built files. |
I'm running into issues with a Next.js version 9.1.3 and now 16.4.4 where everything will deploy and work but I get a number of 404 errors for my apis and dynamic routes.
My now.json only has my environmental variables because I read that the latest versions of Next.js and Now do not need any sort of configuration. Is there something I am missing? Here is my folder structure:
The text was updated successfully, but these errors were encountered: