mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
fix(nuxt3): improve redirect lifecycle (#4430)
* fix(nuxt3): improve redirect lifecycle * fix: check against existing cookie value in header
This commit is contained in:
parent
71b808c8e9
commit
ea47e56540
@ -32,13 +32,14 @@ export function useCookie <T=string> (name: string, _opts?: CookieOptions<T>): C
|
|||||||
if (process.client) {
|
if (process.client) {
|
||||||
watch(cookie, () => { writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) })
|
watch(cookie, () => { writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) })
|
||||||
} else if (process.server) {
|
} else if (process.server) {
|
||||||
const initialValue = cookie.value
|
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
nuxtApp.hooks.hookOnce('app:rendered', () => {
|
const writeFinalCookieValue = () => {
|
||||||
if (cookie.value !== initialValue) {
|
if (cookie.value !== cookies[name]) {
|
||||||
writeServerCookie(useRequestEvent(nuxtApp), name, cookie.value, opts)
|
writeServerCookie(useRequestEvent(nuxtApp), name, cookie.value, opts)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
nuxtApp.hooks.hookOnce('app:rendered', writeFinalCookieValue)
|
||||||
|
nuxtApp.hooks.hookOnce('app:redirected', writeFinalCookieValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cookie as CookieRef<T>
|
return cookie as CookieRef<T>
|
||||||
|
@ -52,6 +52,7 @@ const isProcessingMiddleware = () => {
|
|||||||
|
|
||||||
export interface NavigateToOptions {
|
export interface NavigateToOptions {
|
||||||
replace?: boolean
|
replace?: boolean
|
||||||
|
redirectCode?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export const navigateTo = (to: RouteLocationRaw, options: NavigateToOptions = {}): Promise<void | NavigationFailure> | RouteLocationRaw => {
|
export const navigateTo = (to: RouteLocationRaw, options: NavigateToOptions = {}): Promise<void | NavigationFailure> | RouteLocationRaw => {
|
||||||
@ -59,11 +60,11 @@ export const navigateTo = (to: RouteLocationRaw, options: NavigateToOptions = {}
|
|||||||
return to
|
return to
|
||||||
}
|
}
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
if (process.server && useNuxtApp().ssrContext) {
|
if (process.server) {
|
||||||
const { ssrContext } = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
if (ssrContext && ssrContext.event) {
|
if (nuxtApp.ssrContext && nuxtApp.ssrContext.event) {
|
||||||
const redirectLocation = router.resolve(to).fullPath
|
const redirectLocation = router.resolve(to).fullPath
|
||||||
return sendRedirect(ssrContext.event, redirectLocation)
|
return nuxtApp.callHook('app:redirected').then(() => sendRedirect(nuxtApp.ssrContext.event, redirectLocation, options.redirectCode || 301))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Client-side redirection using vue-router
|
// Client-side redirection using vue-router
|
||||||
|
@ -23,6 +23,7 @@ export interface RuntimeNuxtHooks {
|
|||||||
'app:beforeMount': (app: App<Element>) => HookResult
|
'app:beforeMount': (app: App<Element>) => HookResult
|
||||||
'app:mounted': (app: App<Element>) => HookResult
|
'app:mounted': (app: App<Element>) => HookResult
|
||||||
'app:rendered': () => HookResult
|
'app:rendered': () => HookResult
|
||||||
|
'app:redirected': () => HookResult
|
||||||
'app:suspense:resolve': (Component?: VNode) => HookResult
|
'app:suspense:resolve': (Component?: VNode) => HookResult
|
||||||
'app:error': (err: any) => HookResult
|
'app:error': (err: any) => HookResult
|
||||||
'app:error:cleared': (options: { redirect?: string }) => HookResult
|
'app:error:cleared': (options: { redirect?: string }) => HookResult
|
||||||
|
@ -3,7 +3,7 @@ import { parseURL, parseQuery } from 'ufo'
|
|||||||
import { createError } from 'h3'
|
import { createError } from 'h3'
|
||||||
import { defineNuxtPlugin } from '..'
|
import { defineNuxtPlugin } from '..'
|
||||||
import { callWithNuxt } from '../nuxt'
|
import { callWithNuxt } from '../nuxt'
|
||||||
import { clearError, throwError } from '#app'
|
import { clearError, navigateTo, throwError } from '#app'
|
||||||
|
|
||||||
interface Route {
|
interface Route {
|
||||||
/** Percentage encoded pathname section of the URL. */
|
/** Percentage encoded pathname section of the URL. */
|
||||||
@ -221,9 +221,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
|
|||||||
nuxtApp.hooks.hookOnce('app:created', async () => {
|
nuxtApp.hooks.hookOnce('app:created', async () => {
|
||||||
await router.push(nuxtApp.ssrContext.url)
|
await router.push(nuxtApp.ssrContext.url)
|
||||||
if (route.fullPath !== nuxtApp.ssrContext.url) {
|
if (route.fullPath !== nuxtApp.ssrContext.url) {
|
||||||
nuxtApp.ssrContext.res.setHeader('Location', route.fullPath)
|
await navigateTo(route.fullPath)
|
||||||
nuxtApp.ssrContext.res.statusCode = 301
|
|
||||||
nuxtApp.ssrContext.res.end()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
import { createError } from 'h3'
|
import { createError } from 'h3'
|
||||||
import { withoutBase } from 'ufo'
|
import { withoutBase } from 'ufo'
|
||||||
import NuxtPage from './page'
|
import NuxtPage from './page'
|
||||||
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, throwError, clearError } from '#app'
|
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, throwError, clearError, navigateTo } from '#app'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import routes from '#build/routes'
|
import routes from '#build/routes'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@ -167,11 +167,9 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
if (process.server) {
|
if (process.server) {
|
||||||
router.push(nuxtApp.ssrContext.url)
|
router.push(nuxtApp.ssrContext.url)
|
||||||
|
|
||||||
router.afterEach((to) => {
|
router.afterEach(async (to) => {
|
||||||
if (to.fullPath !== nuxtApp.ssrContext.url) {
|
if (to.fullPath !== nuxtApp.ssrContext.url) {
|
||||||
nuxtApp.ssrContext.res.setHeader('Location', to.fullPath)
|
await navigateTo(to.fullPath)
|
||||||
nuxtApp.ssrContext.res.statusCode = 301
|
|
||||||
nuxtApp.ssrContext.res.end()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user