Skip to content

Commit 3e4cb8e

Browse files
authored
fix: fix HMR for styles (#14199)
* fix: fix HMR for styles * Move check for inline and add test * Format * Improve test * Tidy
1 parent 5a88918 commit 3e4cb8e

5 files changed

Lines changed: 45 additions & 2 deletions

File tree

.changeset/rare-suns-read.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a bug that prevented HMR from working with inline styles
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html>
2+
<head>
3+
<title>Test</title>
4+
</head>
5+
<body>
6+
<h1 class="css-inline">This is coloured</h1>
7+
<style>
8+
.css-inline {
9+
color: blue;
10+
}
11+
</style>
12+
</body>
13+
</html>

packages/astro/e2e/hmr.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,18 @@ test.describe('Styles', () => {
6969

7070
await expect(h).toHaveCSS('color', 'rgb(255, 0, 0)');
7171
});
72+
73+
test('inline styles refresh with HMR', async ({ page, astro }) => {
74+
await page.goto(astro.resolveUrl('/css-inline'));
75+
76+
page.once('load', throwPageShouldNotReload);
77+
78+
const h = page.locator('h1.css-inline');
79+
await expect(h).toHaveCSS('color', 'rgb(0, 0, 255)');
80+
81+
await astro.editFile('./src/pages/css-inline.astro', (original) =>
82+
original.replace('blue', 'red'),
83+
);
84+
await expect(h).toHaveCSS('color', 'rgb(255, 0, 0)');
85+
});
7286
});

packages/astro/src/vite-plugin-astro/hmr.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { HmrContext } from 'vite';
22
import type { Logger } from '../core/logger/core.js';
3+
import { parseAstroRequest } from './query.js';
34
import type { CompileMetadata } from './types.js';
45
import { frontmatterRE } from './utils.js';
56

@@ -37,8 +38,14 @@ export async function handleHotUpdate(
3738
// Invalidate its `astroFileToCompileMetadata` so that the next transform of Astro style virtual module
3839
// will re-generate it
3940
astroFileToCompileMetadata.delete(ctx.file);
40-
// Only return the Astro styles that have changed!
41-
return ctx.modules.filter((mod) => mod.id?.includes('astro&type=style'));
41+
return ctx.modules.filter((mod) => {
42+
if (!mod.id) {
43+
return false;
44+
}
45+
const { query } = parseAstroRequest(mod.id);
46+
// Only return the Astro styles that have changed, except inline style modules that are treated as SSR-only
47+
return query.astro && query.type === 'style' && !query.inline;
48+
});
4249
}
4350
}
4451

packages/astro/src/vite-plugin-astro/query.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ interface AstroQuery {
55
index?: number;
66
lang?: string;
77
raw?: boolean;
8+
inline?: boolean;
89
}
910

1011
interface ParsedRequestResult {
@@ -30,6 +31,9 @@ export function parseAstroRequest(id: string): ParsedRequestResult {
3031
if (query.raw != null) {
3132
query.raw = true;
3233
}
34+
if (query.inline != null) {
35+
query.inline = true;
36+
}
3337
return {
3438
filename,
3539
query,

0 commit comments

Comments
 (0)