Sandy
Sandy

Reputation: 2690

NextJS doesn't create index.html for subfolders in static export

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!

Upvotes: 10

Views: 4775

Answers (1)

Mark G
Mark G

Reputation: 2196

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.

Upvotes: 15

Related Questions