Skip to content

Commit 9eea422

Browse files
committed
fix(hmr): don't fallback to full reload if the HTML is imported from JS
1 parent 30fa462 commit 9eea422

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

packages/vite/src/node/server/hmr.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,12 @@ export function updateModules(
690690

691691
// html file cannot be hot updated
692692
const isClientHtmlChange =
693-
file.endsWith('.html') && environment.name === 'client'
693+
file.endsWith('.html') &&
694+
environment.name === 'client' &&
695+
// if the html file is imported as a module, we assume that this file is
696+
// not used as the template for top-level request response
697+
// (i.e. not used by the middleware).
698+
modules.every((mod) => mod.type !== 'js')
694699

695700
if (needFullReload || isClientHtmlChange) {
696701
const reason =

playground/assets/__tests__/assets.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path'
2+
import { stripVTControlCharacters } from 'node:util'
23
import { describe, expect, test } from 'vitest'
34
import {
45
browserLogs,
@@ -463,6 +464,20 @@ test('Unknown extension assets import', async () => {
463464

464465
test('?raw import', async () => {
465466
expect(await page.textContent('.raw')).toMatch('SVG')
467+
expect(await page.textContent('.raw-html')).toBe('<div>partial</div>\n')
468+
469+
if (isBuild) return
470+
editFile('nested/partial.html', (code) =>
471+
code.replace('<div>partial</div>', '<div>partial updated</div>'),
472+
)
473+
await expect
474+
.poll(() => page.textContent('.raw-html'))
475+
.toBe('<div>partial updated</div>\n')
476+
expect(browserLogs).toStrictEqual(
477+
expect.arrayContaining([
478+
expect.stringContaining('hot updated: /nested/partial.html?raw via'),
479+
]),
480+
)
466481
})
467482

468483
test('?no-inline svg import', async () => {

playground/assets/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ <h2>Unknown extension assets import</h2>
265265

266266
<h2>?raw import</h2>
267267
<code class="raw"></code>
268+
<code class="raw-html"></code>
268269

269270
<h2>?no-inline svg import</h2>
270271
<code class="no-inline-svg"></code>
@@ -546,6 +547,12 @@ <h3>assets in template</h3>
546547
import rawSvg from './nested/fragment.svg?raw'
547548
text('.raw', rawSvg)
548549

550+
import rawHtml from './nested/partial.html?raw'
551+
text('.raw-html', rawHtml)
552+
import.meta.hot?.accept('./nested/partial.html?raw', (m) => {
553+
text('.raw-html', m.default)
554+
})
555+
549556
import noInlineSvg from './nested/fragment.svg?no-inline'
550557
text('.no-inline-svg', noInlineSvg)
551558

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>partial</div>

0 commit comments

Comments
 (0)