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'
511import { createError } from 'h3'
612import { 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
1922import { 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
180187function createCurrentLocation ( base : string , location : Location ) : string {
0 commit comments