2021-12-20 10:37:00 +00:00
|
|
|
import { computed, reactive, shallowRef } from 'vue'
|
2021-01-18 12:46:19 +00:00
|
|
|
import {
|
|
|
|
createRouter,
|
|
|
|
createWebHistory,
|
|
|
|
createMemoryHistory,
|
2022-01-25 12:29:11 +00:00
|
|
|
NavigationGuard
|
2021-01-18 12:46:19 +00:00
|
|
|
} from 'vue-router'
|
2022-02-14 14:18:07 +00:00
|
|
|
import { createError } from 'h3'
|
2022-01-25 14:32:09 +00:00
|
|
|
import NuxtPage from './page'
|
2022-03-11 08:22:16 +00:00
|
|
|
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, NuxtApp, throwError, clearError } from '#app'
|
2021-04-09 13:48:39 +00:00
|
|
|
// @ts-ignore
|
2021-04-29 11:51:54 +00:00
|
|
|
import routes from '#build/routes'
|
2022-01-25 12:29:11 +00:00
|
|
|
// @ts-ignore
|
2022-03-15 16:57:41 +00:00
|
|
|
import routerOptions from '#build/router.options'
|
|
|
|
// @ts-ignore
|
2022-01-25 12:29:11 +00:00
|
|
|
import { globalMiddleware, namedMiddleware } from '#build/middleware'
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-12-10 13:46:53 +00:00
|
|
|
declare module 'vue' {
|
|
|
|
export interface GlobalComponents {
|
|
|
|
NuxtPage: typeof NuxtPage
|
2022-02-07 11:32:04 +00:00
|
|
|
/** @deprecated */
|
|
|
|
NuxtNestedPage: typeof NuxtPage
|
|
|
|
/** @deprecated */
|
|
|
|
NuxtChild: typeof NuxtPage
|
2021-12-10 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 18:31:37 +00:00
|
|
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
|
|
nuxtApp.vueApp.component('NuxtPage', NuxtPage)
|
2021-12-20 10:37:58 +00:00
|
|
|
// TODO: remove before release - present for backwards compatibility & intentionally undocumented
|
2022-02-07 11:32:04 +00:00
|
|
|
nuxtApp.vueApp.component('NuxtNestedPage', NuxtPage)
|
|
|
|
nuxtApp.vueApp.component('NuxtChild', NuxtPage)
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2022-01-18 16:59:14 +00:00
|
|
|
const { baseURL } = useRuntimeConfig().app
|
2021-01-18 12:46:19 +00:00
|
|
|
const routerHistory = process.client
|
2022-01-18 16:59:14 +00:00
|
|
|
? createWebHistory(baseURL)
|
|
|
|
: createMemoryHistory(baseURL)
|
2021-01-18 12:46:19 +00:00
|
|
|
|
|
|
|
const router = createRouter({
|
2022-03-15 16:57:41 +00:00
|
|
|
...routerOptions,
|
2021-01-18 12:46:19 +00:00
|
|
|
history: routerHistory,
|
|
|
|
routes
|
|
|
|
})
|
2021-10-18 18:31:37 +00:00
|
|
|
nuxtApp.vueApp.use(router)
|
2021-01-18 12:46:19 +00:00
|
|
|
|
|
|
|
const previousRoute = shallowRef(router.currentRoute.value)
|
|
|
|
router.afterEach((_to, from) => {
|
|
|
|
previousRoute.value = from
|
|
|
|
})
|
|
|
|
|
2021-10-18 18:31:37 +00:00
|
|
|
Object.defineProperty(nuxtApp.vueApp.config.globalProperties, 'previousRoute', {
|
2021-01-18 12:46:19 +00:00
|
|
|
get: () => previousRoute.value
|
|
|
|
})
|
|
|
|
|
2021-12-20 10:37:00 +00:00
|
|
|
// 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])
|
|
|
|
}
|
|
|
|
|
|
|
|
nuxtApp._route = reactive(route)
|
|
|
|
|
2022-01-25 12:29:11 +00:00
|
|
|
nuxtApp._middleware = nuxtApp._middleware || {
|
|
|
|
global: [],
|
|
|
|
named: {}
|
|
|
|
}
|
|
|
|
|
|
|
|
router.beforeEach(async (to, from) => {
|
2022-01-26 11:56:24 +00:00
|
|
|
to.meta = reactive(to.meta)
|
2022-01-25 12:29:11 +00:00
|
|
|
nuxtApp._processingMiddleware = true
|
|
|
|
|
|
|
|
type MiddlewareDef = string | NavigationGuard
|
|
|
|
const middlewareEntries = new Set<MiddlewareDef>([...globalMiddleware, ...nuxtApp._middleware.global])
|
|
|
|
for (const component of to.matched) {
|
|
|
|
const componentMiddleware = component.meta.middleware as MiddlewareDef | MiddlewareDef[]
|
|
|
|
if (!componentMiddleware) { continue }
|
|
|
|
if (Array.isArray(componentMiddleware)) {
|
|
|
|
for (const entry of componentMiddleware) {
|
|
|
|
middlewareEntries.add(entry)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
middlewareEntries.add(componentMiddleware)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 08:22:16 +00:00
|
|
|
if (process.client && !nuxtApp.isHydrating) {
|
|
|
|
// Clear any existing errors
|
|
|
|
await callWithNuxt(nuxtApp as NuxtApp, clearError)
|
|
|
|
}
|
|
|
|
|
2022-01-25 12:29:11 +00:00
|
|
|
for (const entry of middlewareEntries) {
|
|
|
|
const middleware = typeof entry === 'string' ? nuxtApp._middleware.named[entry] || await namedMiddleware[entry]?.().then(r => r.default || r) : entry
|
|
|
|
|
|
|
|
if (process.dev && !middleware) {
|
|
|
|
console.warn(`Unknown middleware: ${entry}. Valid options are ${Object.keys(namedMiddleware).join(', ')}.`)
|
|
|
|
}
|
|
|
|
|
2022-02-15 09:50:11 +00:00
|
|
|
const result = await callWithNuxt(nuxtApp as NuxtApp, middleware, [to, from])
|
2022-02-14 14:18:07 +00:00
|
|
|
if (process.server) {
|
|
|
|
if (result === false || result instanceof Error) {
|
2022-03-11 08:22:16 +00:00
|
|
|
const error = result || createError({
|
2022-02-14 14:18:07 +00:00
|
|
|
statusMessage: `Route navigation aborted: ${nuxtApp.ssrContext.url}`
|
|
|
|
})
|
2022-03-11 08:22:16 +00:00
|
|
|
return callWithNuxt(nuxtApp, throwError, [error])
|
2022-02-14 14:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-25 12:29:11 +00:00
|
|
|
if (result || result === false) { return result }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
|
delete nuxtApp._processingMiddleware
|
|
|
|
})
|
|
|
|
|
2021-10-18 18:31:37 +00:00
|
|
|
nuxtApp.hook('app:created', async () => {
|
2022-03-11 08:22:16 +00:00
|
|
|
router.afterEach((to) => {
|
|
|
|
if (to.matched.length === 0) {
|
|
|
|
callWithNuxt(nuxtApp, throwError, [createError({
|
|
|
|
statusCode: 404,
|
|
|
|
statusMessage: `Page not found: ${to.fullPath}`
|
|
|
|
})])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-01-18 12:46:19 +00:00
|
|
|
if (process.server) {
|
2021-10-18 18:31:37 +00:00
|
|
|
router.push(nuxtApp.ssrContext.url)
|
2022-01-25 12:29:11 +00:00
|
|
|
|
|
|
|
router.afterEach((to) => {
|
|
|
|
if (to.fullPath !== nuxtApp.ssrContext.url) {
|
|
|
|
nuxtApp.ssrContext.res.setHeader('Location', to.fullPath)
|
2022-02-21 13:03:42 +00:00
|
|
|
nuxtApp.ssrContext.res.statusCode = 301
|
|
|
|
nuxtApp.ssrContext.res.end()
|
2022-01-25 12:29:11 +00:00
|
|
|
}
|
|
|
|
})
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
2021-07-15 09:47:15 +00:00
|
|
|
|
2022-02-14 14:18:07 +00:00
|
|
|
try {
|
|
|
|
await router.isReady()
|
|
|
|
} catch (error) {
|
2022-03-11 08:22:16 +00:00
|
|
|
callWithNuxt(nuxtApp, throwError, [error])
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
|
|
|
})
|
2021-11-18 13:11:34 +00:00
|
|
|
|
|
|
|
return { provide: { router } }
|
2021-06-18 17:16:51 +00:00
|
|
|
})
|