Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html): fix inline proxy modules invalidation #18696

Merged
merged 4 commits into from
Nov 18, 2024
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
21 changes: 15 additions & 6 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
)
const styleUrl: AssetNode[] = []
const inlineStyles: InlineStyleAttribute[] = []
const inlineModulePaths: string[] = []

const addInlineModule = (
node: DefaultTreeAdapterMap['element'],
Expand Down Expand Up @@ -246,13 +247,8 @@ const devHtmlHook: IndexHtmlTransformHook = async (

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

// invalidate the module so the newly cached contents will be served
const clientModuleGraph = server?.environments.client.moduleGraph
const module = clientModuleGraph?.getModuleById(modulePath)
if (module) {
clientModuleGraph!.invalidateModule(module)
}
s.update(
node.sourceCodeLocation!.startOffset,
node.sourceCodeLocation!.endOffset,
Expand Down Expand Up @@ -350,6 +346,19 @@ const devHtmlHook: IndexHtmlTransformHook = async (
}
})

// invalidate the module so the newly cached contents will be served
const clientModuelGraph = server?.environments.client.moduleGraph
if (clientModuelGraph) {
await Promise.all(
inlineModulePaths.map(async (url) => {
const module = await clientModuelGraph.getModuleByUrl(url)
if (module) {
clientModuelGraph.invalidateModule(module)
}
}),
)
}

await Promise.all([
...styleUrl.map(async ({ start, end, code }, index) => {
const url = `${proxyModulePath}?html-proxy&direct&index=${index}.css`
Expand Down
9 changes: 9 additions & 0 deletions playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,12 @@ test('escape html attribute', async () => {
const el = await page.$('.unescape-div')
expect(el).toBeNull()
})

test('invalidate inline proxy module on reload', async () => {
await page.goto(`${viteTestUrl}/transform-inline-js`)
expect(await page.textContent('.test')).toContain('ok')
await page.reload()
expect(await page.textContent('.test')).toContain('ok')
await page.reload()
expect(await page.textContent('.test')).toContain('ok')
})
5 changes: 5 additions & 0 deletions playground/html/transform-inline-js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>id: {{ id }}</div>
<div class="test">test: <span id="{{ id }}">???</span></div>
<script type="module">
document.getElementById('{{ id }}').textContent = 'ok'
</script>
14 changes: 14 additions & 0 deletions playground/html/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default defineConfig({
serveBothFile: resolve(__dirname, 'serve/both.html'),
serveBothFolder: resolve(__dirname, 'serve/both/index.html'),
write: resolve(__dirname, 'write.html'),
'transform-inline-js': resolve(__dirname, 'transform-inline-js.html'),
relativeInput: relative(
process.cwd(),
resolve(__dirname, 'relative-input.html'),
Expand Down Expand Up @@ -249,6 +250,19 @@ ${
},
},
},
{
name: 'transform-inline-js',
transformIndexHtml: {
order: 'pre',
handler(html, ctx) {
if (!ctx.filename.endsWith('html/transform-inline-js.html')) return
return html.replaceAll(
'{{ id }}',
Math.random().toString(36).slice(2),
)
},
},
},
serveExternalPathPlugin(),
],
})
Expand Down