mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-16 21:58:19 +00:00
fix(nuxt): handle page rendering on different path (#21408)
This commit is contained in:
parent
370653ac39
commit
370b84e909
@ -119,6 +119,7 @@ interface _NuxtApp {
|
|||||||
|
|
||||||
ssrContext?: NuxtSSRContext
|
ssrContext?: NuxtSSRContext
|
||||||
payload: {
|
payload: {
|
||||||
|
path?: string
|
||||||
serverRendered?: boolean
|
serverRendered?: boolean
|
||||||
prerenderedAt?: number
|
prerenderedAt?: number
|
||||||
data: Record<string, any>
|
data: Record<string, any>
|
||||||
@ -255,13 +256,13 @@ export function createNuxtApp (options: CreateOptions) {
|
|||||||
defineGetter(nuxtApp.vueApp.config.globalProperties, '$nuxt', nuxtApp)
|
defineGetter(nuxtApp.vueApp.config.globalProperties, '$nuxt', nuxtApp)
|
||||||
|
|
||||||
if (process.server) {
|
if (process.server) {
|
||||||
|
if (nuxtApp.ssrContext) {
|
||||||
// Expose nuxt to the renderContext
|
// Expose nuxt to the renderContext
|
||||||
if (nuxtApp.ssrContext) {
|
|
||||||
nuxtApp.ssrContext.nuxt = nuxtApp
|
nuxtApp.ssrContext.nuxt = nuxtApp
|
||||||
}
|
|
||||||
// Expose payload types
|
// Expose payload types
|
||||||
if (nuxtApp.ssrContext) {
|
|
||||||
nuxtApp.ssrContext._payloadReducers = {}
|
nuxtApp.ssrContext._payloadReducers = {}
|
||||||
|
// Expose current path
|
||||||
|
nuxtApp.payload.path = nuxtApp.ssrContext.event.path
|
||||||
}
|
}
|
||||||
// Expose to server renderer to create payload
|
// Expose to server renderer to create payload
|
||||||
nuxtApp.ssrContext = nuxtApp.ssrContext || {} as any
|
nuxtApp.ssrContext = nuxtApp.ssrContext || {} as any
|
||||||
|
@ -122,6 +122,7 @@ const getSPARenderer = lazyCachedFunction(async () => {
|
|||||||
const renderToString = (ssrContext: NuxtSSRContext) => {
|
const renderToString = (ssrContext: NuxtSSRContext) => {
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
ssrContext!.payload = {
|
ssrContext!.payload = {
|
||||||
|
path: ssrContext.event.path,
|
||||||
_errors: {},
|
_errors: {},
|
||||||
serverRendered: false,
|
serverRendered: false,
|
||||||
data: {},
|
data: {},
|
||||||
|
@ -27,7 +27,8 @@ import { globalMiddleware, namedMiddleware } from '#build/middleware'
|
|||||||
// https://github.com/vuejs/router/blob/4a0cc8b9c1e642cdf47cc007fa5bbebde70afc66/packages/router/src/history/html5.ts#L37
|
// https://github.com/vuejs/router/blob/4a0cc8b9c1e642cdf47cc007fa5bbebde70afc66/packages/router/src/history/html5.ts#L37
|
||||||
function createCurrentLocation (
|
function createCurrentLocation (
|
||||||
base: string,
|
base: string,
|
||||||
location: Location
|
location: Location,
|
||||||
|
renderedPath?: string
|
||||||
): string {
|
): string {
|
||||||
const { pathname, search, hash } = location
|
const { pathname, search, hash } = location
|
||||||
// allows hash bases like #, /#, #/, #!, #!/, /#!/, or even /folder#end
|
// allows hash bases like #, /#, #/, #!, #!/, /#!/, or even /folder#end
|
||||||
@ -41,8 +42,8 @@ function createCurrentLocation (
|
|||||||
if (pathFromHash[0] !== '/') { pathFromHash = '/' + pathFromHash }
|
if (pathFromHash[0] !== '/') { pathFromHash = '/' + pathFromHash }
|
||||||
return withoutBase(pathFromHash, '')
|
return withoutBase(pathFromHash, '')
|
||||||
}
|
}
|
||||||
const path = withoutBase(pathname, base)
|
const path = renderedPath || withoutBase(pathname, base)
|
||||||
return path + search + hash
|
return path + (path.includes('?') ? '' : search) + hash
|
||||||
}
|
}
|
||||||
|
|
||||||
const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
|
const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
|
||||||
@ -63,7 +64,10 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
|
|||||||
const routes = routerOptions.routes?.(_routes) ?? _routes
|
const routes = routerOptions.routes?.(_routes) ?? _routes
|
||||||
|
|
||||||
let startPosition: Parameters<RouterScrollBehavior>[2] | null
|
let startPosition: Parameters<RouterScrollBehavior>[2] | null
|
||||||
const initialURL = process.server ? nuxtApp.ssrContext!.url : createCurrentLocation(routerBase, window.location)
|
const initialURL = process.server
|
||||||
|
? nuxtApp.ssrContext!.url
|
||||||
|
: createCurrentLocation(routerBase, window.location, nuxtApp.payload.path)
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
...routerOptions,
|
...routerOptions,
|
||||||
scrollBehavior: (to, from, savedPosition) => {
|
scrollBehavior: (to, from, savedPosition) => {
|
||||||
|
@ -162,6 +162,15 @@ describe('pages', () => {
|
|||||||
await expectNoClientErrors('/not-found')
|
await expectNoClientErrors('/not-found')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should render correctly when loaded on a different path', async () => {
|
||||||
|
const page = await createPage('/proxy')
|
||||||
|
|
||||||
|
await page.waitForLoadState('networkidle')
|
||||||
|
expect(await page.innerText('body')).toContain('Composable | foo: auto imported from ~/composables/foo.ts')
|
||||||
|
|
||||||
|
await expectNoClientErrors('/proxy')
|
||||||
|
})
|
||||||
|
|
||||||
it('preserves query', async () => {
|
it('preserves query', async () => {
|
||||||
const html = await $fetch('/?test=true')
|
const html = await $fetch('/?test=true')
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ describe.skipIf(isWindows || process.env.TEST_BUILDER === 'webpack' || process.e
|
|||||||
|
|
||||||
it('default server bundle size', async () => {
|
it('default server bundle size', async () => {
|
||||||
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
|
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
|
||||||
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.4k"')
|
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.5k"')
|
||||||
|
|
||||||
const modules = await analyzeSizes('node_modules/**/*', serverDir)
|
const modules = await analyzeSizes('node_modules/**/*', serverDir)
|
||||||
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2284k"')
|
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2284k"')
|
||||||
|
3
test/fixtures/basic/server/routes/proxy.ts
vendored
Normal file
3
test/fixtures/basic/server/routes/proxy.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default defineEventHandler(async () => {
|
||||||
|
return await $fetch<string>('/')
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user