diff --git a/packages/vite/src/client/bundledDevClient.ts b/packages/vite/src/client/bundledDevClient.ts index d13287550d201e..7dd2436355e22b 100644 --- a/packages/vite/src/client/bundledDevClient.ts +++ b/packages/vite/src/client/bundledDevClient.ts @@ -35,6 +35,12 @@ if (typeof DevRuntime !== 'undefined') { override createModuleHotContext(moduleId: string) { return new BundledDevHMRContext(bundledDevHmrClient, moduleId) } + + override loadLazyChunk(proxyModuleId: string): Promise { + return import( + /* @vite-ignore */ `${base}@vite/lazy?id=${encodeURIComponent(proxyModuleId)}&clientId=${this.clientId}` + ) + } } const clientId = nanoid() diff --git a/playground/lazy-compilation/__tests__/base/lazy-compilation-base.spec.ts b/playground/lazy-compilation/__tests__/base/lazy-compilation-base.spec.ts new file mode 100644 index 00000000000000..c53777b286b1e7 --- /dev/null +++ b/playground/lazy-compilation/__tests__/base/lazy-compilation-base.spec.ts @@ -0,0 +1,47 @@ +import type { Response } from 'playwright-chromium' +import { describe, expect, onTestFinished, test } from 'vitest' +import { isBundledDev, page } from '~utils' + +// Regression test for https://github.com/vitejs/vite/issues/23216 + +const lazyIdOf = (url: string) => new URL(url).searchParams.get('id') ?? '' + +describe.runIf(isBundledDev)('lazy compilation with a non-root base', () => { + test('lazy imports are requested under base', async () => { + const lazyResponses: Response[] = [] + const onResponse = (res: Response) => { + if (res.url().includes('/@vite/lazy?')) lazyResponses.push(res) + } + page.on('response', onResponse) + onTestFinished(() => { + page.off('response', onResponse) + }) + + // A lazy import from the initial chunk + await page.click('#route-a-btn') + await expect + .poll(() => page.textContent('#route-a-content')) + .toBe('A:shared-value') + + // A lazy import from inside a lazy chunk + await page.click('#route-nested-btn') + await expect + .poll(() => page.textContent('#route-nested-content')) + .toBe('nested-inner') + + for (const file of [ + 'page-a.js', + 'page-nested.js', + 'page-nested-inner.js', + ]) { + expect( + lazyResponses.some((res) => lazyIdOf(res.url()).includes(file)), + file, + ).toBe(true) + } + for (const res of lazyResponses) { + expect(new URL(res.url()).pathname).toBe('/foo/@vite/lazy') + expect(res.status()).toBe(200) + } + }) +}) diff --git a/playground/lazy-compilation/index.html b/playground/lazy-compilation/index.html index cad97363c07c28..b368b2e068e303 100644 --- a/playground/lazy-compilation/index.html +++ b/playground/lazy-compilation/index.html @@ -15,4 +15,9 @@

Lazy compilation

pending
+
+ +
pending
+
+ diff --git a/playground/lazy-compilation/main.js b/playground/lazy-compilation/main.js index 2f8f1cf84c2a11..117897b8288393 100644 --- a/playground/lazy-compilation/main.js +++ b/playground/lazy-compilation/main.js @@ -1,4 +1,4 @@ -// Three lazy "routes" that all statically import shared.js. Each button starts +// Lazy "routes". A, B and C all statically import shared.js. Each button starts // its import right away and never waits for the others, so a spec can hold // A's response in the browser while B is requested and evaluated. // A failed import is written to the DOM so the spec can read the error. @@ -19,3 +19,7 @@ function load(name, importer) { load('route-a', () => import('./page-a.js')) load('route-b', () => import('./page-b.js')) load('route-c', () => import('./page-c.js')) +// A route whose lazy chunk has its own lazy import +load('route-nested', () => + import('./page-nested.js').then((mod) => mod.loadInner()), +) diff --git a/playground/lazy-compilation/page-nested-inner.js b/playground/lazy-compilation/page-nested-inner.js new file mode 100644 index 00000000000000..4cececb7b28129 --- /dev/null +++ b/playground/lazy-compilation/page-nested-inner.js @@ -0,0 +1 @@ +export const value = 'nested-inner' diff --git a/playground/lazy-compilation/page-nested.js b/playground/lazy-compilation/page-nested.js new file mode 100644 index 00000000000000..2613ef2d6f2b81 --- /dev/null +++ b/playground/lazy-compilation/page-nested.js @@ -0,0 +1 @@ +export const loadInner = () => import('./page-nested-inner.js') diff --git a/playground/lazy-compilation/vite.config-base.js b/playground/lazy-compilation/vite.config-base.js new file mode 100644 index 00000000000000..0529d7933162f4 --- /dev/null +++ b/playground/lazy-compilation/vite.config-base.js @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite' +import baseConfig from './vite.config.ts' + +export default defineConfig({ + ...baseConfig, + base: '/foo/', +})