Skip to content

Commit 8ab04b7

Browse files
authored
fix(html): fix inline proxy modules invalidation (#18696)
1 parent f731ca2 commit 8ab04b7

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

packages/vite/src/node/server/middlewares/indexHtml.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
218218
)
219219
const styleUrl: AssetNode[] = []
220220
const inlineStyles: InlineStyleAttribute[] = []
221+
const inlineModulePaths: string[] = []
221222

222223
const addInlineModule = (
223224
node: DefaultTreeAdapterMap['element'],
@@ -246,13 +247,8 @@ const devHtmlHook: IndexHtmlTransformHook = async (
246247

247248
// inline js module. convert to src="proxy" (dev only, base is never relative)
248249
const modulePath = `${proxyModuleUrl}?html-proxy&index=${inlineModuleIndex}.${ext}`
250+
inlineModulePaths.push(modulePath)
249251

250-
// invalidate the module so the newly cached contents will be served
251-
const clientModuleGraph = server?.environments.client.moduleGraph
252-
const module = clientModuleGraph?.getModuleById(modulePath)
253-
if (module) {
254-
clientModuleGraph!.invalidateModule(module)
255-
}
256252
s.update(
257253
node.sourceCodeLocation!.startOffset,
258254
node.sourceCodeLocation!.endOffset,
@@ -350,6 +346,19 @@ const devHtmlHook: IndexHtmlTransformHook = async (
350346
}
351347
})
352348

349+
// invalidate the module so the newly cached contents will be served
350+
const clientModuelGraph = server?.environments.client.moduleGraph
351+
if (clientModuelGraph) {
352+
await Promise.all(
353+
inlineModulePaths.map(async (url) => {
354+
const module = await clientModuelGraph.getModuleByUrl(url)
355+
if (module) {
356+
clientModuelGraph.invalidateModule(module)
357+
}
358+
}),
359+
)
360+
}
361+
353362
await Promise.all([
354363
...styleUrl.map(async ({ start, end, code }, index) => {
355364
const url = `${proxyModulePath}?html-proxy&direct&index=${index}.css`

playground/html/__tests__/html.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,12 @@ test('escape html attribute', async () => {
510510
const el = await page.$('.unescape-div')
511511
expect(el).toBeNull()
512512
})
513+
514+
test('invalidate inline proxy module on reload', async () => {
515+
await page.goto(`${viteTestUrl}/transform-inline-js`)
516+
expect(await page.textContent('.test')).toContain('ok')
517+
await page.reload()
518+
expect(await page.textContent('.test')).toContain('ok')
519+
await page.reload()
520+
expect(await page.textContent('.test')).toContain('ok')
521+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div>id: {{ id }}</div>
2+
<div class="test">test: <span id="{{ id }}">???</span></div>
3+
<script type="module">
4+
document.getElementById('{{ id }}').textContent = 'ok'
5+
</script>

playground/html/vite.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default defineConfig({
3535
serveBothFile: resolve(__dirname, 'serve/both.html'),
3636
serveBothFolder: resolve(__dirname, 'serve/both/index.html'),
3737
write: resolve(__dirname, 'write.html'),
38+
'transform-inline-js': resolve(__dirname, 'transform-inline-js.html'),
3839
relativeInput: relative(
3940
process.cwd(),
4041
resolve(__dirname, 'relative-input.html'),
@@ -249,6 +250,19 @@ ${
249250
},
250251
},
251252
},
253+
{
254+
name: 'transform-inline-js',
255+
transformIndexHtml: {
256+
order: 'pre',
257+
handler(html, ctx) {
258+
if (!ctx.filename.endsWith('html/transform-inline-js.html')) return
259+
return html.replaceAll(
260+
'{{ id }}',
261+
Math.random().toString(36).slice(2),
262+
)
263+
},
264+
},
265+
},
252266
serveExternalPathPlugin(),
253267
],
254268
})

0 commit comments

Comments
 (0)