Skip to content
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
6 changes: 6 additions & 0 deletions packages/vite/src/client/bundledDevClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ if (typeof DevRuntime !== 'undefined') {
override createModuleHotContext(moduleId: string) {
return new BundledDevHMRContext(bundledDevHmrClient, moduleId)
}

override loadLazyChunk(proxyModuleId: string): Promise<unknown> {
return import(
/* @vite-ignore */ `${base}@vite/lazy?id=${encodeURIComponent(proxyModuleId)}&clientId=${this.clientId}`
)
}
}

const clientId = nanoid()
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
})
})
5 changes: 5 additions & 0 deletions playground/lazy-compilation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ <h1>Lazy compilation</h1>
<div id="route-c-content">pending</div>
</section>

<section>
<button id="route-nested-btn">load nested</button>
<div id="route-nested-content">pending</div>
</section>

<script type="module" src="/main.js"></script>
6 changes: 5 additions & 1 deletion playground/lazy-compilation/main.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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()),
)
1 change: 1 addition & 0 deletions playground/lazy-compilation/page-nested-inner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 'nested-inner'
1 change: 1 addition & 0 deletions playground/lazy-compilation/page-nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const loadInner = () => import('./page-nested-inner.js')
7 changes: 7 additions & 0 deletions playground/lazy-compilation/vite.config-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import baseConfig from './vite.config.ts'

export default defineConfig({
...baseConfig,
base: '/foo/',
})
Loading