Netlify + Nuxt - Remove trailing slash

I am generating static pages from nuxt on netlify. We have the structure of url without trailing slashes. For SSR we had trailingslash option set to false and custom redirect with redirect module. But for ‘nuxt generate’ option this is no longer working

I can’t seem to setup 301 redirects for url’s ending with trailing slash.

Eg.
https://example.com/blog - works
https://example.com/blog/ - throws 404

in nuxt.config we got

router: {
  trailingSlash: false
},

redirect: [{
  from: '^/(.*)/$',
  to: (from, req) => {
  let trailingUrl = req.url.endsWith('/') ? req.url.slice(0, -1) : req.url
  return trailingUrl
},
}],

All works on local server but not on netlify. Any ideas?

You cannot use any server middleware when using nuxt generate.
So your redirect module is server middleware and will thus never be called.

You could use Netlify redirects instead: https://docs.netlify.com/routing/redirects

Ok thanks for the answer. The problem with _redirects is that you’re not allowed to use regex there - so imagine we got over 100 pages and we deploy multiple of them everyday . This way we will have to add manually every single redirection.

Hey @arekk,

I understand the frustration here and I can sympathise! There isn’t a clean and preferred way of achieving this. Every framework, person, will handle trailing slashes a little differently.

We have an open feature request for exactly this – allowing you to decide whether to opt in or out of trailing slashes – and I’ve added this conversation to the request list. Though, I can’t promise if/when this is delivered.

There are some methods of achieving this though they’re not very intelligent (such as this).