fix(nuxt): provide fallback route (#22580)

This commit is contained in:
Daniel Roe 2023-08-10 13:36:38 +01:00 committed by GitHub
parent 758ae76709
commit 99375d688c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,13 +14,13 @@ export const RouteInjectionPlugin = (nuxt: Nuxt) => createUnplugin(() => {
return isVue(id, { type: ['template', 'script'] })
},
transform (code) {
if (!INJECTION_SINGLE_RE.test(code)) { return }
if (!INJECTION_SINGLE_RE.test(code) || code.includes('_ctx._.provides[__nuxt_route_symbol')) { return }
let replaced = false
const s = new MagicString(code)
s.replace(INJECTION_RE, () => {
replaced = true
return '_ctx._.provides[__nuxt_route_symbol]'
return '(_ctx._.provides[__nuxt_route_symbol] || _ctx.$route)'
})
if (replaced) {
s.prepend('import { PageRouteSymbol as __nuxt_route_symbol } from \'#app/components/injections\';\n')