Trailing slash redirect for Nuxt

updated: 23.09.2021
  1. Create middleware ~/middleware/trailingSlashRedirect.js with:
export default function ({ route, redirect }) {
  if (route.path !== '/' && route.path.endsWith('/')) {
    const { path, query, hash } = route;
    const nextPath = path.replace(/\/+$/, '') || '/';
    const nextRoute = { path: nextPath, query, hash };

    redirect(nextRoute);
  }
}

  1. Add this middleware for router section to nuxt.config.js.
    export default {

router: {
middleware: ‘trailingSlashRedirect’
},

}