Skip to content

Commit d3e0cdd

Browse files
authored
fix: sync upstream router plugin from nuxt (#303)
1 parent 2e8a24d commit d3e0cdd

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

src/parts/router.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ export const setupRouter = () => {
2525
nuxt.options.vite.optimizeDeps.include = nuxt.options.vite.optimizeDeps.include || []
2626
nuxt.options.vite.optimizeDeps.include.push('@ionic/vue-router')
2727

28+
const ROUTER_PLUGIN_RE = /nuxt3?\/dist\/(app\/plugins|pages\/runtime)\/(plugins\/)?router/
29+
const ionicRouterPlugin = {
30+
src: resolve(runtimeDir, 'router'),
31+
mode: 'all',
32+
} as const
33+
2834
nuxt.hook('modules:done', () => {
2935
nuxt.hook('app:resolve', app => {
30-
app.plugins = app.plugins.filter(
31-
p => !p.src.match(/nuxt3?\/dist\/(app\/plugins|pages\/runtime)\/(plugins\/)?router/)
32-
)
33-
app.plugins.unshift({
34-
src: resolve(runtimeDir, 'router'),
35-
mode: 'all',
36-
})
36+
const routerPlugin = app.plugins.findIndex(p => ROUTER_PLUGIN_RE.test(p.src))
37+
if (routerPlugin !== -1) {
38+
app.plugins.splice(routerPlugin, 1, ionicRouterPlugin)
39+
} else {
40+
app.plugins.unshift(ionicRouterPlugin)
41+
}
3742
})
3843
})
3944

src/runtime/router.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
import { createRouter, createWebHistory, createMemoryHistory } from '@ionic/vue-router'
2-
3-
import { computed, ComputedRef, reactive, shallowRef } from 'vue'
4-
import { createWebHashHistory, NavigationGuard, RouteLocation } from 'vue-router'
1+
import {
2+
createRouter,
3+
createWebHistory,
4+
createWebHashHistory,
5+
createMemoryHistory,
6+
} from '@ionic/vue-router'
7+
8+
import { computed, isReadonly, reactive, shallowRef } from 'vue'
9+
import type { ComputedRef, Ref } from 'vue'
10+
import type { RouteLocation, Router } from 'vue-router'
511
import { createError } from 'h3'
612
import { withoutBase, isEqual } from 'ufo'
7-
import {
8-
callWithNuxt,
9-
defineNuxtPlugin,
10-
useRuntimeConfig,
11-
showError,
12-
clearError,
13-
navigateTo,
14-
useError,
15-
useState,
16-
} from '#app'
13+
14+
import type { PageMeta, RouteMiddleware, Plugin } from '#app'
15+
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app/nuxt'
16+
import { showError, clearError, useError } from '#app/composables/error'
17+
import { useRequestEvent } from '#app/composables/ssr'
18+
import { useState } from '#app/composables/state'
19+
import { navigateTo } from '#app/composables/router'
1720

1821
// @ts-expect-error virtual module
1922
import { globalMiddleware, namedMiddleware } from '#build/middleware'
@@ -89,12 +92,12 @@ export default defineNuxtPlugin(async nuxtApp => {
8992
const initialLayout = useState<string>('_layout')
9093
router.beforeEach(async (to, from) => {
9194
to.meta = reactive(to.meta)
92-
if (nuxtApp.isHydrating) {
93-
to.meta.layout = initialLayout.value ?? to.meta.layout
95+
if (nuxtApp.isHydrating && initialLayout.value && !isReadonly(to.meta.layout)) {
96+
to.meta.layout = initialLayout.value as Exclude<PageMeta['layout'], Ref | false>
9497
}
9598
nuxtApp._processingMiddleware = true
9699

97-
type MiddlewareDef = string | NavigationGuard
100+
type MiddlewareDef = string | RouteMiddleware
98101
const middlewareEntries = new Set<MiddlewareDef>([
99102
...globalMiddleware,
100103
...nuxtApp._middleware.global,
@@ -137,9 +140,11 @@ export default defineNuxtPlugin(async nuxtApp => {
137140
const error =
138141
result ||
139142
createError({
140-
statusMessage: `Route navigation aborted: ${initialURL}`,
143+
statusCode: 404,
144+
statusMessage: `Page Not Found: ${initialURL}`,
141145
})
142-
return callWithNuxt(nuxtApp, showError, [error])
146+
await callWithNuxt(nuxtApp, showError, [error])
147+
return false
143148
}
144149
}
145150
if (result || result === false) {
@@ -156,25 +161,27 @@ export default defineNuxtPlugin(async nuxtApp => {
156161
await callWithNuxt(nuxtApp, clearError)
157162
}
158163
if (to.matched.length === 0) {
159-
callWithNuxt(nuxtApp, showError, [
164+
await callWithNuxt(nuxtApp, showError, [
160165
createError({
161166
statusCode: 404,
162167
fatal: false,
163168
statusMessage: `Page not found: ${to.fullPath}`,
164169
}),
165170
])
166-
} else if (process.server && to.matched[0].name === '404' && nuxtApp.ssrContext) {
167-
nuxtApp.ssrContext.event.res.statusCode = 404
168171
} else if (process.server) {
169172
const currentURL = to.fullPath || '/'
170-
if (!isEqual(currentURL, initialURL)) {
171-
await callWithNuxt(nuxtApp, navigateTo, [currentURL])
173+
if (!isEqual(currentURL, initialURL, { trailingSlash: true })) {
174+
const event = await callWithNuxt(nuxtApp, useRequestEvent)
175+
const options = {
176+
redirectCode: event.node.res.statusCode !== 200 ? event.node.res.statusCode || 302 : 302,
177+
}
178+
await callWithNuxt(nuxtApp, navigateTo, [currentURL, options])
172179
}
173180
}
174181
})
175182

176183
return { provide: { router } }
177-
})
184+
}) as Plugin<{ router: Router }>
178185

179186
// https://github.com/vuejs/router/blob/4a0cc8b9c1e642cdf47cc007fa5bbebde70afc66/packages/router/src/history/html5.ts#L37
180187
function createCurrentLocation(base: string, location: Location): string {

0 commit comments

Comments
 (0)