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,
|
|
|
|
RouterLink
|
|
|
|
} from 'vue-router'
|
2021-10-21 19:16:52 +00:00
|
|
|
import NuxtChild from './child.vue'
|
2021-05-24 11:14:10 +00:00
|
|
|
import NuxtPage from './page.vue'
|
2021-06-30 16:32:22 +00:00
|
|
|
import NuxtLayout from './layout'
|
2021-08-11 20:28:38 +00:00
|
|
|
import { defineNuxtPlugin } 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'
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-12-10 13:46:53 +00:00
|
|
|
declare module 'vue' {
|
|
|
|
export interface GlobalComponents {
|
|
|
|
NuxtChild: typeof NuxtChild
|
|
|
|
NuxtPage: typeof NuxtPage
|
|
|
|
NuxtLayout: typeof NuxtLayout
|
|
|
|
NuxtLink: typeof RouterLink
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 18:31:37 +00:00
|
|
|
export default defineNuxtPlugin((nuxtApp) => {
|
2021-10-21 19:16:52 +00:00
|
|
|
nuxtApp.vueApp.component('NuxtChild', NuxtChild)
|
2021-10-18 18:31:37 +00:00
|
|
|
nuxtApp.vueApp.component('NuxtPage', NuxtPage)
|
|
|
|
nuxtApp.vueApp.component('NuxtLayout', NuxtLayout)
|
|
|
|
nuxtApp.vueApp.component('NuxtLink', RouterLink)
|
2021-01-18 12:46:19 +00:00
|
|
|
|
|
|
|
const routerHistory = process.client
|
|
|
|
? createWebHistory()
|
|
|
|
: createMemoryHistory()
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
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)
|
|
|
|
|
2021-10-18 18:31:37 +00:00
|
|
|
nuxtApp.hook('app:created', async () => {
|
2021-01-18 12:46:19 +00:00
|
|
|
if (process.server) {
|
2021-10-18 18:31:37 +00:00
|
|
|
router.push(nuxtApp.ssrContext.url)
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
2021-07-15 09:47:15 +00:00
|
|
|
|
|
|
|
await router.isReady()
|
|
|
|
|
|
|
|
const is404 = router.currentRoute.value.matched.length === 0
|
|
|
|
if (process.server && is404) {
|
2021-10-18 18:31:37 +00:00
|
|
|
const error = new Error(`Page not found: ${nuxtApp.ssrContext.url}`)
|
2021-07-15 09:47:15 +00:00
|
|
|
// @ts-ignore
|
|
|
|
error.statusCode = 404
|
2021-10-18 18:31:37 +00:00
|
|
|
nuxtApp.ssrContext.error = 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
|
|
|
})
|