Skip to content

Commit

Permalink
feat!: use native vue-router composables (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Sep 14, 2022
1 parent 74c3f4f commit 5e5fa50
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"unbuild": "0.8.11",
"vitest": "^0.21.1",
"vue": "^2.7.10",
"vue-router": "^3"
"vue-router": "^3.6.5"
},
"engines": {
"node": "^14.16.0 || ^16.11.0 || ^17.0.0 || ^18.0.0"
Expand Down
3 changes: 3 additions & 0 deletions src/capi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export function setupCAPIBridge (options: Exclude<BridgeConfig['capi'], boolean>
nuxt.options.plugins.unshift(appPlugin)
})

// Transpile vue-router to ensure single vue instance
nuxt.options.build.transpile.push('vue-router')

if (options.legacy === false) {
// Skip adding `@nuxtjs/composition-api` handlers if legacy support is disabled
return
Expand Down
15 changes: 13 additions & 2 deletions src/imports/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ export const appPreset = defineUnimportPreset({
'useCookie',
'useRequestHeaders',
'useRequestEvent',
'useRouter',
'useRoute',
'defineNuxtRouteMiddleware',
'navigateTo',
'abortNavigation',
Expand Down Expand Up @@ -108,8 +106,21 @@ export const vuePreset = defineUnimportPreset({
imports: vueKeys
})

// vue-router
const vueRouterPreset = defineUnimportPreset({
from: 'vue-router/composables',
imports: [
'useRoute',
'onBeforeRouteLeave',
'onBeforeRouteUpdate',
'useLink',
'useRouter'
]
})

export const defaultPresets = [
...commonPresets,
appPreset,
vueRouterPreset,
vuePreset
]
6 changes: 3 additions & 3 deletions src/runtime/capi.legacy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { defu } from 'defu'
import { computed, getCurrentInstance as getVM, isReactive, isRef, onBeforeMount, onServerPrefetch, reactive, ref, set, shallowRef, toRaw, toRefs, watch } from 'vue'
import { useRouter as _useRouter, useRoute as _useRoute } from 'vue-router/composables'
import { useNuxtApp } from './app'
import { useRouter as _useRouter, useState } from './composables'
import { useState } from './composables'

// Vue composition API export
export {
Expand Down Expand Up @@ -254,8 +255,7 @@ export const useRouter = () => {

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

export const useStore = () => {
Expand Down
29 changes: 4 additions & 25 deletions src/runtime/composables.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getCurrentInstance, onBeforeUnmount, isRef, watch, reactive, toRef, isReactive, Ref, set } from 'vue'
import type { CombinedVueInstance } from 'vue/types/vue'
import type { MetaInfo } from 'vue-meta'
import type VueRouter from 'vue-router'
import type { Location, Route } from 'vue-router'
import type { Location, RawLocation, Route } from 'vue-router'
import type { RuntimeConfig } from '@nuxt/schema'
import { sendRedirect } from 'h3'
import { defu } from 'defu'
import { useRouter } from 'vue-router/composables'
import { useNuxtApp } from './app'

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

// Auto-import equivalents for `vue-router`
export const useRouter = () => {
return useNuxtApp()?.nuxt2Context.app.router as VueRouter
}

// This provides an equivalent interface to `vue-router` (unlike legacy implementation)
export const useRoute = () => {
const nuxtApp = useNuxtApp()

if (!nuxtApp._route) {
Object.defineProperty(nuxtApp, '__route', {
get: () => nuxtApp.nuxt2Context.app.context.route
})
nuxtApp._route = reactive(nuxtApp.__route)
const router = useRouter()
router.afterEach(route => Object.assign(nuxtApp._route, route))
}

return nuxtApp._route as Route
}

// payload.state is used for vuex by nuxt 2
export const useState = <T>(key: string, init?: (() => T)): Ref<T> => {
export const useState = <T> (key: string, init?: (() => T)): Ref<T> => {
const nuxtApp = useNuxtApp()
if (!nuxtApp.payload.useState) {
nuxtApp.payload.useState = {}
Expand Down Expand Up @@ -195,7 +174,7 @@ export interface NavigateToOptions {
replace?: boolean
}

export const navigateTo = (to: Route, options: NavigateToOptions = {}): Promise<Route | void> | Route => {
export const navigateTo = (to: RawLocation, options: NavigateToOptions = {}): Promise<RawLocation | Route | void> | RawLocation | Route => {
if (isProcessingMiddleware()) {
return to
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11736,10 +11736,10 @@ vue-no-ssr@^1.1.1:
resolved "https://registry.yarnpkg.com/vue-no-ssr/-/vue-no-ssr-1.1.1.tgz#875f3be6fb0ae41568a837f3ac1a80eaa137b998"
integrity sha512-ZMjqRpWabMPqPc7gIrG0Nw6vRf1+itwf0Itft7LbMXs2g3Zs/NFmevjZGN1x7K3Q95GmIjWbQZTVerxiBxI+0g==

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

vue-server-renderer@^2.6.12:
version "2.7.10"
Expand Down

3 comments on commit 5e5fa50

@dargmuesli
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit or the one before broke my CI: https://github.com/maevsi/maevsi/actions/runs/3053664838/jobs/4924575770#step:5:2213 in maevsi/maevsi#843

#29 7.221 [nuxt] [request error] Cannot read properties of null (reading 'proxy')
#29 7.221   at useRouter (./.output/server/chunks/app/server.mjs:1392:62)  
#29 7.221   at ./.output/server/chunks/app/server.mjs:23140:32  
#29 7.221   at callWithNuxt (./.output/server/chunks/app/server.mjs:602:25)  
#29 7.221   at ./.output/server/chunks/app/server.mjs:610:31  
#29 7.221   at createApp (./.output/server/chunks/app/server.mjs:30595:16)  
#29 7.221   at async __webpack_exports__.default (./.output/server/chunks/app/server.mjs:30720:12)  
#29 7.221   at async Object.renderToString (./.output/server/chunks/handlers/renderer.mjs:180:19)  
#29 7.221   at async ./.output/server/chunks/handlers/renderer.mjs:571:20  
#29 7.221   at async ./.output/server/node_modules/h3/dist/index.mjs:592:19  
#29 7.221   at async Server.nodeHandler (./.output/server/node_modules/h3/dist/index.mjs:538:7)
#29 7.284 [nuxt] [request error] Cannot read properties of null (reading 'proxy')
#29 7.284   at useRouter (./.output/server/chunks/app/server.mjs:1392:62)  
#29 7.284   at ./.output/server/chunks/app/server.mjs:23140:32  
#29 7.284   at callWithNuxt (./.output/server/chunks/app/server.mjs:602:25)  
#29 7.284   at ./.output/server/chunks/app/server.mjs:610:31  
#29 7.284   at createApp (./.output/server/chunks/app/server.mjs:30595:16)  
#29 7.284   at process.processTicksAndRejections (node:internal/process/task_queues:95:5)  
#29 7.284   at async __webpack_exports__.default (./.output/server/chunks/app/server.mjs:30720:12)  
#29 7.284   at async Object.renderToString (./.output/server/chunks/handlers/renderer.mjs:180:19)  
#29 7.284   at async ./.output/server/chunks/handlers/renderer.mjs:571:20  
#29 7.284   at async ./.output/server/node_modules/h3/dist/index.mjs:592:19

@danielroe
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do open an issue with a minimal repro. It is probably that Vue router is pulling in a different version of Vue. You might need to update your lockfile, for example.

@dargmuesli
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright! I just commented on the commit for now to give direct feedback about that break. I'll of course create a minrep for that. But currently #530 (comment) keeps me from being able to do so.
I hope I'll soon be able to finish nuxt 3 migration with i18n and storybook, so that I don't need to bother you with all those issue reports for bridge too. I hope they are somewhat welcome nevertheless 🙈

Please sign in to comment.