next.js: Error 404 on static export
Error 404 on static export
next-learn-demo/E1-static-export/
E1-static-export
Describe the bug
Deploying on now leads to error 404 on every page request except “/”. Client side navigation works fine
To Reproduce
Follow instructions here https://nextjs.org/learn/excel/static-html-export
or:
- git clone https://github.com/zeit/next-learn-demo.git
- cd next-learn-demo
- cd E1-static-export
- npm install
- write next.config.js like this
const fetch = require('isomorphic-unfetch');
module.exports = {
exportPathMap: async function() {
const paths = {
'/': { page: '/' },
'/about': { page: '/about' }
};
const res = await fetch('https://api.tvmaze.com/search/shows?q=batman');
const data = await res.json();
const shows = data.map(entry => entry.show);
shows.forEach(show => {
paths[`/show/${show.id}`] = { page: '/show/[id]', query: { id: show.id } };
});
return paths;
}
};
- set scripts in package. json like this:
"scripts": {
"build": "next build",
"export": "next export"
},
- npm run build
- npm run export
- cd out
- now
Expected behavior
static export should work fine on now platform
System information
- OS: macOS 10.13.14
- node v10.16.3
- npm 6.9.0
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 11
- Comments: 17 (5 by maintainers)
I solved it by adding
exportTrailingSlash: true,to the next.config.js file.Exports all pages to their own files as index.html files, which worked in my case.
This is caused by the necessity to publish “pages” to static accessible .html files.
When the “about” ‘view’ is published as a static ‘page’, it becomes “about.html”. If you request /about.html in the browser the page should load.
Because the dynamic router understands that “/about” means to show the about view, that works as expected. But the static system doesn’t ‘know’ what to do for “/about”, it can only respond to a request for “/about.html”.
The solution I implemented was to rename all internal links, including those in exportPathMap to have an “.html” extension.
Unfortunately, this does not work for dynamic operation in development mode, or dynamic production mode. That makes for a bunch of search and replace between development and exporting a static site.
It looks to me like the real solution is for the static export system to have a way to map all internal links to expect the .html extension.
In my case, I abandoned static export. All of my “static” sites are actually published as “dynamic” apps. 😕
How adout “NotFound page” ? I can’t redirect the page to “NotFound page” correctly when page not found. May you help me?
Is there any way to fix the issue that @DNature mentioned. I too get the same issue
Update:
When you want to use
next exporton ZEIT Now you can use the normal flow, but make sure to updatescriptsinpackage.jsonto includenext export:Outdated reply from earlier
So in the particular case outlined you're using `next export` which generates static html and can't be detected as a Next.js app by Now.In general we recommend using the zero config approach of just running
nowin the project itself and it’ll automatically be deployed with the right configuration including cache-control for static assets etc.In the case outlined (
next export) you’ll want to add the followingnow.jsonto set up the correct headers and serving of files:⚠ WARNING:: The above config is only useful for
next export. If you’re deploying withoutnext exportdo not use the above configuration. Now will take care of all the configuration automatically and set up dynamic routes etc if you have them.