Skip to content

Commit 5e5fa50

Browse files
authored
feat!: use native vue-router composables (#533)
1 parent 74c3f4f commit 5e5fa50

6 files changed

Lines changed: 28 additions & 35 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"unbuild": "0.8.11",
9696
"vitest": "^0.21.1",
9797
"vue": "^2.7.10",
98-
"vue-router": "^3"
98+
"vue-router": "^3.6.5"
9999
},
100100
"engines": {
101101
"node": "^14.16.0 || ^16.11.0 || ^17.0.0 || ^18.0.0"

src/capi.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export function setupCAPIBridge (options: Exclude<BridgeConfig['capi'], boolean>
2222
nuxt.options.plugins.unshift(appPlugin)
2323
})
2424

25+
// Transpile vue-router to ensure single vue instance
26+
nuxt.options.build.transpile.push('vue-router')
27+
2528
if (options.legacy === false) {
2629
// Skip adding `@nuxtjs/composition-api` handlers if legacy support is disabled
2730
return

src/imports/presets.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ export const appPreset = defineUnimportPreset({
3333
'useCookie',
3434
'useRequestHeaders',
3535
'useRequestEvent',
36-
'useRouter',
37-
'useRoute',
3836
'defineNuxtRouteMiddleware',
3937
'navigateTo',
4038
'abortNavigation',
@@ -108,8 +106,21 @@ export const vuePreset = defineUnimportPreset({
108106
imports: vueKeys
109107
})
110108

109+
// vue-router
110+
const vueRouterPreset = defineUnimportPreset({
111+
from: 'vue-router/composables',
112+
imports: [
113+
'useRoute',
114+
'onBeforeRouteLeave',
115+
'onBeforeRouteUpdate',
116+
'useLink',
117+
'useRouter'
118+
]
119+
})
120+
111121
export const defaultPresets = [
112122
...commonPresets,
113123
appPreset,
124+
vueRouterPreset,
114125
vuePreset
115126
]

src/runtime/capi.legacy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { defu } from 'defu'
22
import { computed, getCurrentInstance as getVM, isReactive, isRef, onBeforeMount, onServerPrefetch, reactive, ref, set, shallowRef, toRaw, toRefs, watch } from 'vue'
3+
import { useRouter as _useRouter, useRoute as _useRoute } from 'vue-router/composables'
34
import { useNuxtApp } from './app'
4-
import { useRouter as _useRouter, useState } from './composables'
5+
import { useState } from './composables'
56

67
// Vue composition API export
78
export {
@@ -254,8 +255,7 @@ export const useRouter = () => {
254255

255256
export const useRoute = () => {
256257
warnOnce('useRoute', 'You are using `useRoute`, which has a Nuxt 3-compatible replacement.')
257-
const vm = getCurrentInstance()
258-
return computed(() => vm.$route)
258+
return _useRoute()
259259
}
260260

261261
export const useStore = () => {

src/runtime/composables.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { getCurrentInstance, onBeforeUnmount, isRef, watch, reactive, toRef, isReactive, Ref, set } from 'vue'
22
import type { CombinedVueInstance } from 'vue/types/vue'
33
import type { MetaInfo } from 'vue-meta'
4-
import type VueRouter from 'vue-router'
5-
import type { Location, Route } from 'vue-router'
4+
import type { Location, RawLocation, Route } from 'vue-router'
65
import type { RuntimeConfig } from '@nuxt/schema'
76
import { sendRedirect } from 'h3'
87
import { defu } from 'defu'
8+
import { useRouter } from 'vue-router/composables'
99
import { useNuxtApp } from './app'
1010

1111
export { useLazyAsyncData, refreshNuxtData } from './asyncData'
@@ -48,29 +48,8 @@ export const useRuntimeConfig = () => {
4848
return nuxtApp._config as RuntimeConfig
4949
}
5050

51-
// Auto-import equivalents for `vue-router`
52-
export const useRouter = () => {
53-
return useNuxtApp()?.nuxt2Context.app.router as VueRouter
54-
}
55-
56-
// This provides an equivalent interface to `vue-router` (unlike legacy implementation)
57-
export const useRoute = () => {
58-
const nuxtApp = useNuxtApp()
59-
60-
if (!nuxtApp._route) {
61-
Object.defineProperty(nuxtApp, '__route', {
62-
get: () => nuxtApp.nuxt2Context.app.context.route
63-
})
64-
nuxtApp._route = reactive(nuxtApp.__route)
65-
const router = useRouter()
66-
router.afterEach(route => Object.assign(nuxtApp._route, route))
67-
}
68-
69-
return nuxtApp._route as Route
70-
}
71-
7251
// payload.state is used for vuex by nuxt 2
73-
export const useState = <T>(key: string, init?: (() => T)): Ref<T> => {
52+
export const useState = <T> (key: string, init?: (() => T)): Ref<T> => {
7453
const nuxtApp = useNuxtApp()
7554
if (!nuxtApp.payload.useState) {
7655
nuxtApp.payload.useState = {}
@@ -195,7 +174,7 @@ export interface NavigateToOptions {
195174
replace?: boolean
196175
}
197176

198-
export const navigateTo = (to: Route, options: NavigateToOptions = {}): Promise<Route | void> | Route => {
177+
export const navigateTo = (to: RawLocation, options: NavigateToOptions = {}): Promise<RawLocation | Route | void> | RawLocation | Route => {
199178
if (isProcessingMiddleware()) {
200179
return to
201180
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11736,10 +11736,10 @@ vue-no-ssr@^1.1.1:
1173611736
resolved "https://registry.yarnpkg.com/vue-no-ssr/-/vue-no-ssr-1.1.1.tgz#875f3be6fb0ae41568a837f3ac1a80eaa137b998"
1173711737
integrity sha512-ZMjqRpWabMPqPc7gIrG0Nw6vRf1+itwf0Itft7LbMXs2g3Zs/NFmevjZGN1x7K3Q95GmIjWbQZTVerxiBxI+0g==
1173811738

11739-
vue-router@^3, vue-router@^3.5.1:
11740-
version "3.5.4"
11741-
resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-3.5.4.tgz#c453c0b36bc75554de066fefc3f2a9c3212aca70"
11742-
integrity sha512-x+/DLAJZv2mcQ7glH2oV9ze8uPwcI+H+GgTgTmb5I55bCgY3+vXWIsqbYUzbBSZnwFHEJku4eoaH/x98veyymQ==
11739+
vue-router@^3.5.1, vue-router@^3.6.5:
11740+
version "3.6.5"
11741+
resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-3.6.5.tgz#95847d52b9a7e3f1361cb605c8e6441f202afad8"
11742+
integrity sha512-VYXZQLtjuvKxxcshuRAwjHnciqZVoXAjTjcqBTz4rKc8qih9g9pI3hbDjmqXaHdgL3v8pV6P8Z335XvHzESxLQ==
1174311743

1174411744
vue-server-renderer@^2.6.12:
1174511745
version "2.7.10"

0 commit comments

Comments
 (0)