mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): handle external navigation to api routes (#19829)
This commit is contained in:
parent
cebfcb3dad
commit
ebd7dcd4ba
@ -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 })`.')
|
||||
}
|
||||
|
@ -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', () => {
|
||||
|
@ -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",
|
||||
|
7
test/fixtures/basic/pages/navigate-to-api.vue
vendored
Normal file
7
test/fixtures/basic/pages/navigate-to-api.vue
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
<template>
|
||||
<div>You should not see me</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
await navigateTo('/api/test', { external: true })
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user