From 05a8c2d955b9dc7f13e0bc6ad6e347f2a9d40f23 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 11 Jun 2023 22:27:02 +0100 Subject: [PATCH] fix(nuxt): respect query/hash for external routes in `navigateTo` (#21500) --- packages/nuxt/src/app/composables/router.ts | 4 ++-- test/basic.test.ts | 2 +- test/bundle.test.ts | 2 +- test/fixtures/basic/pages/navigate-to-external.vue | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/app/composables/router.ts b/packages/nuxt/src/app/composables/router.ts index 75bb4cf38d..a0d9f8b39a 100644 --- a/packages/nuxt/src/app/composables/router.ts +++ b/packages/nuxt/src/app/composables/router.ts @@ -2,7 +2,7 @@ import { getCurrentInstance, hasInjectionContext, inject, onUnmounted } from 'vu import type { Ref } from 'vue' import type { NavigationFailure, NavigationGuard, RouteLocationNormalized, RouteLocationPathRaw, RouteLocationRaw, Router, useRoute as _useRoute, useRouter as _useRouter } from '#vue-router' import { sanitizeStatusCode } from 'h3' -import { hasProtocol, joinURL, parseURL } from 'ufo' +import { hasProtocol, joinURL, parseURL, withQuery } from 'ufo' import { useNuxtApp, useRuntimeConfig } from '../nuxt' import type { NuxtError } from './error' @@ -113,7 +113,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na to = '/' } - const toPath = typeof to === 'string' ? to : ((to as RouteLocationPathRaw).path || '/') + const toPath = typeof to === 'string' ? to : (withQuery((to as RouteLocationPathRaw).path || '/', to.query || {}) + (to.hash || '')) // Early open handler if (options?.open) { diff --git a/test/basic.test.ts b/test/basic.test.ts index 594206ca0c..c296e821b8 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -788,7 +788,7 @@ describe('navigate external', () => { it('should redirect to example.com', async () => { const { headers } = await fetch('/navigate-to-external/', { redirect: 'manual' }) - expect(headers.get('location')).toEqual('https://example.com/') + expect(headers.get('location')).toEqual('https://example.com/?redirect=false#test') }) it('should redirect to api endpoint', async () => { diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 427b1aa80e..0b302f3cda 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -25,7 +25,7 @@ describe.skipIf(process.env.SKIP_BUNDLE_SIZE === 'true' || process.env.ECOSYSTEM it('default client bundle size', async () => { stats.client = await analyzeSizes('**/*.js', publicDir) - expect(roundToKilobytes(stats.client.totalBytes)).toMatchInlineSnapshot('"97.1k"') + expect(roundToKilobytes(stats.client.totalBytes)).toMatchInlineSnapshot('"97.2k"') expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(` [ "_nuxt/entry.js", diff --git a/test/fixtures/basic/pages/navigate-to-external.vue b/test/fixtures/basic/pages/navigate-to-external.vue index f18ac00b50..2853412703 100644 --- a/test/fixtures/basic/pages/navigate-to-external.vue +++ b/test/fixtures/basic/pages/navigate-to-external.vue @@ -8,5 +8,5 @@ if (useRoute().path === '/navigate-to-external') { throw new Error('this should not run') }) } -await navigateTo('https://example.com/', { external: true, replace: true }) +await navigateTo({ path: 'https://example.com/', query: { redirect: false }, hash: '#test' }, { external: true, replace: true })