fix(nuxt): handle external navigation to api routes (#19829)

This commit is contained in:
Daniel Roe 2023-03-20 17:15:01 +00:00 committed by GitHub
parent cebfcb3dad
commit ebd7dcd4ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 2 deletions

View File

@ -90,7 +90,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
}
const toPath = typeof to === 'string' ? to : ((to as RouteLocationPathRaw).path || '/')
const isExternal = hasProtocol(toPath, { acceptRelative: true })
const isExternal = options?.external || hasProtocol(toPath, { acceptRelative: true })
if (isExternal && !options?.external) {
throw new Error('Navigating to external URL is not allowed by default. Use `navigateTo (url, { external: true })`.')
}

View File

@ -528,6 +528,12 @@ describe('navigate external', () => {
expect(headers.get('location')).toEqual('https://example.com/')
})
it('should redirect to api endpoint', async () => {
const { headers } = await fetch('/navigate-to-api', { redirect: 'manual' })
expect(headers.get('location')).toEqual('/api/test')
})
})
describe('middlewares', () => {

View File

@ -26,7 +26,7 @@ describe.skipIf(isWindows)('minimal nuxt application', () => {
it('default client bundle size', async () => {
stats.client = await analyzeSizes('**/*.js', publicDir)
expect(stats.client.totalBytes).toBeLessThan(106500)
expect(stats.client.totalBytes).toBeLessThan(106600)
expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
[
"_nuxt/_plugin-vue_export-helper.js",

View File

@ -0,0 +1,7 @@
<template>
<div>You should not see me</div>
</template>
<script setup>
await navigateTo('/api/test', { external: true })
</script>