Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/bridge/src/auto-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Preset } from 'unimport'
import autoImports from '../../nuxt3/src/auto-imports/module'
import { vuePreset } from '../../nuxt3/src/auto-imports/presets'

const UnsupportedImports = new Set(['useAsyncData', 'useFetch', 'useError', 'throwError', 'clearError', 'defineNuxtLink'])
const UnsupportedImports = new Set(['useAsyncData', 'useFetch', 'useError', 'throwError', 'clearError', 'defineNuxtLink', 'useActiveRoute'])
const CapiHelpers = new Set(Object.keys(CompositionApi))

export function setupAutoImports () {
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt3/src/app/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export type { FetchResult, UseFetchOptions } from './fetch'
export { useCookie } from './cookie'
export type { CookieOptions, CookieRef } from './cookie'
export { useRequestHeaders } from './ssr'
export { abortNavigation, addRouteMiddleware, defineNuxtRouteMiddleware, navigateTo, useRoute, useRouter } from './router'
export { abortNavigation, addRouteMiddleware, defineNuxtRouteMiddleware, navigateTo, useRoute, useActiveRoute, useRouter } from './router'
export type { AddRouteMiddlewareOptions, RouteMiddleware } from './router'
4 changes: 4 additions & 0 deletions packages/nuxt3/src/app/composables/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export const useRoute = () => {
return useNuxtApp()._route as RouteLocationNormalizedLoaded
}

export const useActiveRoute = () => {
return useNuxtApp()._activeRoute as RouteLocationNormalizedLoaded
}

export interface RouteMiddleware {
(to: RouteLocationNormalized, from: RouteLocationNormalized): ReturnType<NavigationGuard>
}
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt3/src/auto-imports/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const appPreset = defineUnimportPreset({
'useRequestHeaders',
'useRouter',
'useRoute',
'useActiveRoute',
'defineNuxtRouteMiddleware',
'navigateTo',
'abortNavigation',
Expand Down
17 changes: 12 additions & 5 deletions packages/nuxt3/src/pages/runtime/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,16 @@ export default defineNuxtPlugin((nuxtApp) => {
get: () => previousRoute.value
})

// https://github.com/vuejs/vue-router-next/blob/master/src/router.ts#L1192-L1200
const route = {}
for (const key in router.currentRoute.value) {
route[key] = computed(() => router.currentRoute.value[key])
}

// Allows suspending the route object until page navigation completes
const path = process.server ? nuxtApp.ssrContext.req.url : createCurrentLocation(baseURL, window.location)
const currentRoute = shallowRef(router.resolve(path) as RouteLocation)
const syncCurrentRoute = () => { currentRoute.value = router.currentRoute.value }
const _activeRoute = shallowRef(router.resolve(path) as RouteLocation)
const syncCurrentRoute = () => { _activeRoute.value = router.currentRoute.value }
nuxtApp.hook('page:finish', syncCurrentRoute)
router.afterEach((to, from) => {
// We won't trigger suspense if the component is reused between routes
Expand All @@ -88,12 +94,13 @@ export default defineNuxtPlugin((nuxtApp) => {
}
})
// https://github.com/vuejs/vue-router-next/blob/master/src/router.ts#L1192-L1200
const route = {}
for (const key in currentRoute.value) {
route[key] = computed(() => currentRoute.value[key])
const activeRoute = {}
for (const key in _activeRoute.value) {
activeRoute[key] = computed(() => _activeRoute.value[key])
}

nuxtApp._route = reactive(route)
nuxtApp._activeRoute = reactive(activeRoute)

nuxtApp._middleware = nuxtApp._middleware || {
global: [],
Expand Down