fix(nuxt): update default redirect code of navigateTo to 302 Found (#7189)

This commit is contained in:
Damian Głowala 2022-09-03 14:30:03 +02:00 committed by GitHub
parent 0f1a746825
commit 3e9f1efb1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -62,14 +62,14 @@ export interface NavigateToOptions {
external?: boolean
}
export const navigateTo = (to: RouteLocationRaw | undefined | null, options: NavigateToOptions = {}): Promise<void | NavigationFailure> | RouteLocationRaw => {
export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: NavigateToOptions): Promise<void | NavigationFailure> | RouteLocationRaw => {
if (!to) {
to = '/'
}
const toPath = typeof to === 'string' ? to : ((to as RouteLocationPathRaw).path || '/')
const isExternal = hasProtocol(toPath, true)
if (isExternal && !options.external) {
if (isExternal && !options?.external) {
throw new Error('Navigating to external URL is not allowed by default. Use `nagivateTo (url, { external: true })`.')
}
if (isExternal && parseURL(toPath).protocol === 'script:') {
@ -87,13 +87,13 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options: Nav
const nuxtApp = useNuxtApp()
if (nuxtApp.ssrContext && nuxtApp.ssrContext.event) {
const redirectLocation = isExternal ? toPath : joinURL(useRuntimeConfig().app.baseURL, router.resolve(to).fullPath || '/')
return nuxtApp.callHook('app:redirected').then(() => sendRedirect(nuxtApp.ssrContext!.event, redirectLocation, options.redirectCode || 301))
return nuxtApp.callHook('app:redirected').then(() => sendRedirect(nuxtApp.ssrContext!.event, redirectLocation, options?.redirectCode || 302))
}
}
// Client-side redirection using vue-router
if (isExternal) {
if (options.replace) {
if (options?.replace) {
location.replace(toPath)
} else {
location.href = toPath
@ -101,7 +101,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options: Nav
return Promise.resolve()
}
return options.replace ? router.replace(to) : router.push(to)
return options?.replace ? router.replace(to) : router.push(to)
}
/** This will abort navigation within a Nuxt route middleware handler. */