Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

next.js – directory structure of exported site

hoping a Next.js magician could help me with this, or at least just tell me flat out if it isn’t possible.

We have a site which is compiled down into static files. These will be served from S3.

When I run next export, everything builds correctly, but I had hoped to be able to directly access subpages without adding the .html extension. I assumed (wrongly) that next built pages into directories, and dropped an index.html into that directory.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The setup on the dev side is fairly simple. Each component has a directory, eg "some-component", with an index.js which exports the named component from an adjacent file like "some-component.js".

This all works if I start at the site root – I can navigate to, eg, /components/some-component without worrying about the extension.

Next has created a some-component directory, but the file inside that is still just called some-component.html. So if I hit that url from the browser, it obviously 404s.

Is there any way to get next to spit out index.html’s into the directories, so browsing directly to the directory (try saying that after a drink) works? Or am I barking up the wrong tree?

I know there are other solutions to this, particularly for s3, but figured it was worth asking about.

Cheers!

>Solution :

Next.js has a trailingSlash setting that should work for you:

// next.config.js
module.exports = {
  trailingSlash: true,
}

When this setting is true, and using something like ./pages/products/shoes.jsx as an example, a directory named after the .jsx file (shoes) is created and that .jsx file then becomes an index.html file inside that directory, i.e., href="/products/shoes/". Your server will send that path’s index.html (or can be configured to if it doesn’t already) even when index.html isn’t specified in the href.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading