Skip to content

Instantly share code, notes, and snippets.

@adamdehaven
Created April 30, 2026 17:09
Show Gist options
  • Select an option

  • Save adamdehaven/b7ea6577a8285c531603bdfc4a0bcf52 to your computer and use it in GitHub Desktop.

Select an option

Save adamdehaven/b7ea6577a8285c531603bdfc4a0bcf52 to your computer and use it in GitHub Desktop.
Mutate route params
/** Pre-compiled regex for stripping .md extension (case-insensitive) */
const MD_SUFFIX_REGEX = /\.md$/i
/**
* Server-side plugin that strips `.md` extensions from Vue Router params during SSR.
*
* When a request like `/public-page.md` is received, the Nitro layer sets
* `event.context.markdownRequested` to signal markdown output preference. However,
* Vue Router still sees the `.md` suffix in route params, which breaks data fetching
* that depends on clean param values. This plugin strips the suffix from all params.
*
* Note: This only affects params, not `route.path`. Code checking `route.path` may
* still see the `.md` suffix.
*/
export default defineNuxtPlugin({
name: 'remove-markdown-extension',
parallel: true,
setup(nuxtApp) {
if (!nuxtApp.ssrContext?.event?.context?.markdownRequested) return
const router = useRouter()
router.beforeEach((to) => {
const params = to.params as Record<string, string | string[] | undefined>
for (const key in params) {
const value = params[key]
if (typeof value === 'string') {
// Fast path: check suffix before invoking regex
if (value.length > 3 && MD_SUFFIX_REGEX.test(value)) {
params[key] = value.slice(0, -3)
}
} else if (Array.isArray(value)) {
let modified = false
const newValue = value.map((v) => {
if (typeof v === 'string' && v.length > 3 && MD_SUFFIX_REGEX.test(v)) {
modified = true
return v.slice(0, -3)
}
return v
})
if (modified) params[key] = newValue
}
}
})
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment