Last active
August 15, 2021 09:26
-
-
Save ghawthorne66/4eb119f6233adb5a0d03f6e3867ff98d to your computer and use it in GitHub Desktop.
nuxt remove trailing slashes /
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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