Examples to test how Next.js handles trailing slashes in URLs. Especially check to see if the behavior is different during development and production.
yarn init
the project.- Read the Getting Started page at: https://nextjs.org/docs/getting-started
- Manual setup:
yarn add next react react-dom
- Make the following folder structure for a fictional restaurant website:
pages/ about/ contact-us/ facebook/ business-page.js instagram.js messenger.js email.js index.js twitter.js index.js meet-the-team.js locations/ mountain-view/ castro-street.js shoreline.js san-francisco/ fillmore.js union-square.js index.js san-francisco.js menu/ dinner/ index.js lunch/ index.js sunday-brunch.js index.js
- Populate all
*.js
files with a function that returns a single React component (<div></div>
with text). For example:export default () => { return <div>About</div>; };
yarn run dev
starts a development server on localhost port 3000.- On the Getting Started page, it describes static file serving via a
./public/
folder. Add a public folder with some files. - Click next to move on to Pages. According to the docs, our website should support the path
/about/meet-the-team
. - Read the section Pages with Dynamic Routes and add
[id].js
and two*.js
files in the/posts/
folder.pages/ posts/ [id].js 1.js 2.js
- Read the intro to Routing. The docs say that the "router will automatically route files named `index` to the root of the directory." The docs provide these two examples:
Depending on the web development background of the person reading this page, the "root of the directory" might be interpreted as including a trailing slash. This could be a source of developer confusion.
pages/index.js → / pages/blog/index.js → /blog
pages/index.js → / This ends in a slash. pages/blog/index.js → /blog/ This ends in a slash.
- Add some
Link
components to do client-side route transitions. Add enoughLink
components to individual pages so we can navigate back and forth between the site. - Build and test locally with
yarn run dev
. - Deploy on Vercel using the default options.
- The site is deployed to https://next-js-trailing-slash.now.sh.
Path | Development | Production |
---|---|---|
/ | https://localhost:3000/ | https://next-js-trailing-slash.now.sh |
/about | DEV/about | PROD/about |
/about/contact-us/facebook/business-page | DEV/.../business-page | PROD/.../business-page |
- Dynamic routing with a file has interesting edge case. It works differently in production on Vercel vs in development. Not all cases where it differs with a component!
- XXX
- XXX