From 33ed3935a37bd4580189b46844d0baf5cbce12d1 Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Fri, 7 Mar 2025 15:00:10 +0100 Subject: [PATCH] fix(vue): stub `vue-router` --- src/runtime/vue/stubs.ts | 47 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/runtime/vue/stubs.ts b/src/runtime/vue/stubs.ts index a5b099c7..90452fd1 100644 --- a/src/runtime/vue/stubs.ts +++ b/src/runtime/vue/stubs.ts @@ -6,7 +6,52 @@ import type { NuxtApp } from '#app' import { useColorMode as useColorModeVueUse } from '@vueuse/core' export { useHead } from '@unhead/vue' -export { useRoute, useRouter } from 'vue-router' + +// Create stub implementations for vue-router +export const useRouteStub = () => ({ + path: '', + name: null, + params: {}, + query: {}, + hash: '', + fullPath: '', + matched: [], + meta: {}, + redirectedFrom: undefined +}) + +export const useRouterStub = () => ({ + push: () => Promise.resolve(), + replace: () => Promise.resolve(), + go: () => Promise.resolve(), + back: () => Promise.resolve(), + forward: () => Promise.resolve(), + beforeEach: () => () => {}, + afterEach: () => () => {}, + getRoutes: () => [], + hasRoute: () => false, + currentRoute: ref({}) +}) + +// Use a runtime check to determine which implementation to use +let _useRoute = useRouteStub +let _useRouter = useRouterStub + +// Try to import from vue-router +try { + // This will throw if vue-router is not available + const vueRouter = await import('vue-router') + if (vueRouter.useRoute && vueRouter.useRouter) { + // Type assertion with unknown to fix type compatibility issues + _useRoute = vueRouter.useRoute as unknown as typeof _useRoute + _useRouter = vueRouter.useRouter as unknown as typeof _useRouter + } +} catch { + // Use the stubs if vue-router is not available +} + +export const useRoute = _useRoute +export const useRouter = _useRouter export { defineShortcuts } from '../composables/defineShortcuts' export { useLocale } from '../composables/useLocale'