fix(nuxt): allow customising status code in validate method (#28612)

This commit is contained in:
felix-dolderer 2024-08-21 13:51:22 +02:00 committed by GitHub
parent 2ccdaa14cb
commit 85719f00aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 68 additions and 6 deletions

View File

@ -12,13 +12,10 @@ export default defineNuxtRouteMiddleware(async (to) => {
if (result === true) { if (result === true) {
return return
} }
if (import.meta.server) {
return result
}
const error = createError({ const error = createError({
statusCode: 404, statusCode: (result && result.statusCode) || 404,
statusMessage: `Page Not Found: ${to.fullPath}`, statusMessage: (result && result.statusMessage) || `Page Not Found: ${to.fullPath}`,
data: { data: {
path: to.fullPath, path: to.fullPath,
}, },
@ -32,7 +29,7 @@ export default defineNuxtRouteMiddleware(async (to) => {
// We pretend to have navigated to the invalid route so // We pretend to have navigated to the invalid route so
// that the user can return to the previous page with // that the user can return to the previous page with
// the back button. // the back button.
window.history.pushState({}, '', to.fullPath) window?.history.pushState({}, '', to.fullPath)
}) })
// We stop the navigation immediately before it resolves // We stop the navigation immediately before it resolves
// if there is no other route matching it. // if there is no other route matching it.

View File

@ -209,6 +209,45 @@ describe('pages', () => {
await page.close() await page.close()
}) })
it('validates routes with custom statusCode and statusMessage', async () => {
const CUSTOM_ERROR_CODE = 401
const CUSTOM_ERROR_MESSAGE = 'Custom error message'
const ERROR_PAGE_TEXT = 'This is the error page'
const PAGE_TEXT = 'You should not see this'
// Check status code and message on fetch
const res = await fetch('/validate-custom-error')
const serverText = await res.text()
expect(res.status).toEqual(CUSTOM_ERROR_CODE)
expect(serverText).toContain(CUSTOM_ERROR_MESSAGE)
expect(serverText).not.toContain(PAGE_TEXT)
// Client-side navigation
const { page } = await renderPage('/navigate-to-validate-custom-error')
await page.getByText('should throw a 401 error with custom message').click()
// error.vue has an h1 tag
await page.waitForSelector('h1')
const clientText = await page.innerText('body')
expect(clientText).toContain(CUSTOM_ERROR_MESSAGE)
expect(clientText).toContain(ERROR_PAGE_TEXT)
expect(clientText).not.toContain(PAGE_TEXT)
await page.close()
// Server-side navigation
const { page: serverPage } = await renderPage('/validate-custom-error')
const serverPageText = await serverPage.innerText('body')
expect(serverPageText).toContain(CUSTOM_ERROR_MESSAGE)
expect(serverPageText).toContain(ERROR_PAGE_TEXT)
expect(serverPageText).not.toContain(PAGE_TEXT)
await serverPage.close()
})
it('returns 500 when there is an infinite redirect', async () => { it('returns 500 when there is an infinite redirect', async () => {
const { status } = await fetch('/redirect-infinite', { redirect: 'manual' }) const { status } = await fetch('/redirect-infinite', { redirect: 'manual' })
expect(status).toEqual(500) expect(status).toEqual(500)

View File

@ -0,0 +1,14 @@
<template>
<div>
<div>navigate-to-validate-custom-error.vue</div>
<NuxtLink to="/validate-custom-error">
should throw a 401 error with custom message
</NuxtLink>
</div>
</template>
<script setup lang="ts">
definePageMeta({
middleware: ['override'],
})
</script>

View File

@ -0,0 +1,12 @@
<template>
<div>You should not see this</div>
</template>
<script setup lang="ts">
definePageMeta({
validate: to => ({
statusCode: 401,
statusMessage: 'Custom error message',
}),
})
</script>