Skip to content

ronyeh/next-js-trailing-slash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Next JS Trailing Slash Tests

Examples to test how Next.js handles trailing slashes in URLs. Especially check to see if the behavior is different during development and production.

Steps Taken

  1. yarn init the project.
  2. Read the Getting Started page at: https://nextjs.org/docs/getting-started
  3. Manual setup: yarn add next react react-dom
  4. 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
    
  5. 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>;
    };
    
  6. yarn run dev starts a development server on localhost port 3000.
  7. On the Getting Started page, it describes static file serving via a ./public/ folder. Add a public folder with some files.
  8. Click next to move on to Pages. According to the docs, our website should support the path /about/meet-the-team.
  9. 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
    
  10. 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:
    pages/index.js      → /
    pages/blog/index.js → /blog
    
    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      → /        This ends in a slash.
    pages/blog/index.js → /blog/   This ends in a slash.
    
  11. Add some Link components to do client-side route transitions. Add enough Link components to individual pages so we can navigate back and forth between the site.
  12. Build and test locally with yarn run dev.
  13. Deploy on Vercel using the default options.
  14. The site is deployed to https://next-js-trailing-slash.now.sh.

Test URLs

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

Notes

  • 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

About

Examples to test how Next.js handles trailing slashes in URLs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published