Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/nuxt/src/app/composables/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Router, RouteLocationNormalizedLoaded, NavigationGuard, RouteLocationNormalized, RouteLocationRaw, NavigationFailure } from 'vue-router'
import { sendRedirect } from 'h3'
import { useNuxtApp } from '#app'
import { joinURL } from 'ufo'
import { useNuxtApp, useRuntimeConfig } from '#app'

export const useRouter = () => {
return useNuxtApp()?.$router as Router
Expand Down Expand Up @@ -66,7 +67,7 @@ export const navigateTo = (to: RouteLocationRaw, options: NavigateToOptions = {}
if (process.server) {
const nuxtApp = useNuxtApp()
if (nuxtApp.ssrContext && nuxtApp.ssrContext.event) {
const redirectLocation = router.resolve(to).fullPath || '/'
const redirectLocation = joinURL(useRuntimeConfig().app.baseURL, router.resolve(to).fullPath || '/')
return nuxtApp.callHook('app:redirected').then(() => sendRedirect(nuxtApp.ssrContext.event, redirectLocation, options.redirectCode || 301))
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/nuxt/src/app/plugins/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { reactive, h } from 'vue'
import { parseURL, parseQuery, withoutBase, isEqual } from 'ufo'
import { parseURL, parseQuery, withoutBase, isEqual, joinURL } from 'ufo'
import { createError } from 'h3'
import { defineNuxtPlugin } from '..'
import { callWithNuxt } from '../nuxt'
Expand Down Expand Up @@ -102,6 +102,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
hooks[hook].push(guard)
return () => hooks[hook].splice(hooks[hook].indexOf(guard), 1)
}
const baseURL = useRuntimeConfig().app.baseURL

const route: Route = reactive(getRouteFromPath(initialURL))
async function handleNavigation (url: string, replace?: boolean): Promise<void> {
Expand All @@ -124,7 +125,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
// Perform navigation
Object.assign(route, to)
if (process.client) {
window.history[replace ? 'replaceState' : 'pushState']({}, '', url)
window.history[replace ? 'replaceState' : 'pushState']({}, '', joinURL(baseURL, url))
if (!nuxtApp.isHydrating) {
// Clear any existing errors
await callWithNuxt(nuxtApp, clearError)
Expand Down
16 changes: 11 additions & 5 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,9 @@ describe('head tags', () => {

describe('navigate', () => {
it('should redirect to index with navigateTo', async () => {
const html = await $fetch('/navigate-to/')
const { headers } = await fetch('/navigate-to/', { redirect: 'manual' })

// Snapshot
// expect(html).toMatchInlineSnapshot()

expect(html).toContain('Hello Nuxt 3!')
expect(headers.get('location')).toEqual('/')
})
})

Expand Down Expand Up @@ -368,6 +365,15 @@ describe('dynamic paths', () => {
}
})

it('should use baseURL when redirecting', async () => {
process.env.NUXT_APP_BUILD_ASSETS_DIR = '/_other/'
process.env.NUXT_APP_BASE_URL = '/foo/'
await startServer()
const { headers } = await fetch('/foo/navigate-to/', { redirect: 'manual' })

expect(headers.get('location')).toEqual('/foo/')
})

it('should allow setting CDN URL', async () => {
process.env.NUXT_APP_BASE_URL = '/foo/'
process.env.NUXT_APP_CDN_URL = 'https://example.com/'
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/basic/pages/navigate-to.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
</template>

<script setup>
navigateTo('/', { replace: true })
await navigateTo('/', { replace: true })
</script>