fix(nuxt): do not redirect when `vue-router` normalises url (#20247)

This commit is contained in:
Daniel Roe 2023-04-13 11:14:44 +01:00 committed by GitHub
parent 295473f924
commit 9110d41628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 6 deletions

View File

@ -8,7 +8,7 @@ import {
createWebHistory
} from 'vue-router'
import { createError } from 'h3'
import { isEqual, withoutBase } from 'ufo'
import { withoutBase } from 'ufo'
import type { PageMeta, Plugin, RouteMiddleware } from '../../../app/index'
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app/nuxt'
@ -181,11 +181,8 @@ export default defineNuxtPlugin({
fatal: false,
statusMessage: `Page not found: ${to.fullPath}`
})])
} else if (process.server) {
const currentURL = to.fullPath || '/'
if (!isEqual(currentURL, initialURL, { trailingSlash: true })) {
await callWithNuxt(nuxtApp, navigateTo, [currentURL])
}
} else if (process.server && to.redirectedFrom) {
await callWithNuxt(nuxtApp, navigateTo, [to.fullPath || '/'])
}
})

View File

@ -130,6 +130,11 @@ describe('pages', () => {
await page.close()
})
it('returns 500 when there is an infinite redirect', async () => {
const { status } = await fetch('/redirect-infinite', { redirect: 'manual' })
expect(status).toEqual(500)
})
it('render 404', async () => {
const html = await $fetch('/not-found')

View File

@ -9,6 +9,10 @@ export default defineNuxtRouteMiddleware(async (to) => {
await new Promise(resolve => setTimeout(resolve, 100))
return navigateTo(to.path.slice('/redirect/'.length - 1))
}
if (to.path === '/redirect-infinite') {
// the path will be the same in this new route and vue-router should send a 500 response
return navigateTo('/redirect-infinite?test=true')
}
if (to.path === '/navigate-to-external') {
return navigateTo('/', { external: true })
}