3

We have a Next.js site on Netlify with static and server-rendered pages. We have links like https://example.com/locations/berlin and we would like to have them to always end with a trailing slash like https://example.com/locations/berlin/. To achieve this, we use the trailingSlash: true option of Next.js which is working fine locally.

The problem occurs when we deploy our site to Netlify. The pages are always redirected to the url without trailing slash, like https://example.com/locations/berlin with status code 301. We already enabled Pretty URLs in the Netlify Build Settings but that makes no difference.

When I understand it correctly, with trailingSlash: true, Next.js should create the static pages like that:

https://example.com/locations/berlin/index.html

But instead it creates pages like that:

https://example.com/locations/berlin.html

// next.config.js
module.exports = {
 target: "serverless",
 trailingSlash: true,
};

For our static pages, we use a pages/[...slug].js file that exports our static paths with fallback: false.

Since we use server rendered pages too, we can't use next export.

# netlify.toml
[build]
 command = "yarn build"
[[plugins]]
 package = "@netlify/plugin-nextjs"
// package.json
{
 ...
 "scripts": {
 "build": "next build",
 ...
 }
}

Versions

  • next: 10.2.0
  • @netlify/plugin-nextjs: 3.2.0

Expected behavior

  • Pages without trailing slash are redirected with status code 301 to the url with a trailing slash
  • Pages with trailing slash are loaded directly with status code 200
asked May 3, 2021 at 15:00

1 Answer 1

1

There doesn't seem to be an easy way to make trailing slashes work with Next.js on Neltify at the moment, so we decided to switch to Vercel. The platform seems to be better suited for Next.js overall, which is not surprising as it's developed by the same company.

answered May 5, 2021 at 20:25
Sign up to request clarification or add additional context in comments.

1 Comment

Here is the or a source for your statement answers.netlify.com/t/nextjs-trailing-slashes/38693

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.