10

I made a fully static website using NextJS, exported it and I'm hosting it on S3 using static website hosting. I can click around and successfully view all the pages, including example.com/blog. However if in the browser I click refresh, or enter example.com/blog directly, I get a 404 Not Found error.

When viewing the exported files, I see that /blog/ has no index.html file, even though there should be (in my opinion) since in the original source files I have a /blog/index.ts file, and when in dev mode I can refresh localhost/blog or enter it directly and it works as expected.

In summary, I believe NextJS should create a /blog/index.html file but it doesn't. Is there any way to force this? Am I doing something wrong? Thank you!

1 Answer 1

15

To generate an index.html file when exporting to static HTML, enable the trailingSlash setting in your next.config.js:

module.exports = {
  trailingSlash: true,
}

./out/blog.html should now become ./out/blog/index.html after the export.

6
  • It's now loading the blog page, but the styles are being loaded from example.com/blog/_next/, which is wrong so they aren't loaded at all.
    – Sandy
    Commented May 28, 2022 at 20:20
  • @Sandy That issue is separate from your original question, so you might want to post a new one.
    – Mark G
    Commented May 28, 2022 at 20:23
  • Updated my comment, not sure if yours still applies but I believe this new issue is part of the same thing.
    – Sandy
    Commented May 28, 2022 at 20:25
  • @Sandy Is blog a page (i.e., ./pages/blog) or a basePath in your next.config.js? Are you using absolute or relative paths when importing CSS? Also, does a clean build (deleting the .next directory before re-running next build and next export) fix anything?
    – Mark G
    Commented May 28, 2022 at 20:28
  • 1
    Fixed! I believe the cause is that additionally I was using the assetPrefix config. Removing that and keeping trailingSlash fixed this. Thanks again.
    – Sandy
    Commented May 28, 2022 at 20:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.