Skip to content

Instantly share code, notes, and snippets.

@ghawthorne66
Last active August 15, 2021 09:26
Show Gist options
  • Save ghawthorne66/4eb119f6233adb5a0d03f6e3867ff98d to your computer and use it in GitHub Desktop.
Save ghawthorne66/4eb119f6233adb5a0d03f6e3867ff98d to your computer and use it in GitHub Desktop.
nuxt remove trailing slashes /
//Create src/middleware/trailingSlashRedirect.js//
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);
}
}
//Register it in nuxt.config.js://
export default {
...
router: {
middleware: 'trailingSlashRedirect',
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment