Skip to content

Commit a0a49e9

Browse files
rururuxmatthewp
andauthored
fix(cloudflare): ensure HMR works when prerenderEnvironment is set to 'node' (#16162)
Co-authored-by: Matthew Phillips <matthewphillips@cloudflare.com>
1 parent b51f297 commit a0a49e9

7 files changed

Lines changed: 78 additions & 2 deletions

File tree

.changeset/fresh-balloons-glow.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 an issue where HMR would not trigger when modifying files while using @astrojs/cloudflare with prerenderEnvironment: 'node' enabled.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { expect } from '@playwright/test';
2+
import { testFactory } from './test-utils.js';
3+
4+
const test = testFactory(import.meta.url, {
5+
root: './fixtures/cloudflare-node-prerender-hmr/',
6+
devToolbar: {
7+
enabled: false,
8+
},
9+
});
10+
11+
let devServer;
12+
13+
test.beforeAll(async ({ astro }) => {
14+
devServer = await astro.startDevServer();
15+
});
16+
17+
test.afterAll(async () => {
18+
await devServer.stop();
19+
});
20+
21+
test.describe('Astro page', () => {
22+
test('refresh with HMR', async ({ page, astro }) => {
23+
await page.goto(astro.resolveUrl('/'));
24+
25+
const h = page.locator('h1');
26+
await expect(h, 'original text set').toHaveText('Original content');
27+
28+
await astro.editFile('./src/pages/index.astro', (original) =>
29+
original.replaceAll('Original', 'Updated'),
30+
);
31+
32+
await expect(h, 'text changed').toHaveText('Updated content');
33+
});
34+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @ts-check
2+
import cloudflare from '@astrojs/cloudflare';
3+
import { defineConfig } from 'astro/config';
4+
5+
export default defineConfig({
6+
adapter: cloudflare({
7+
prerenderEnvironment: 'node',
8+
}),
9+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@test/astro-cloudflare-node-prerender-mdx",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "astro dev",
7+
"build": "astro build"
8+
},
9+
"dependencies": {
10+
"@astrojs/cloudflare": "workspace:*",
11+
"astro": "workspace:*"
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
---
3+
<html>
4+
<head><title>Original content</title></head>
5+
<body><h1>Original content</h1></body>
6+
</html>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { EnvironmentModuleNode, Plugin } from 'vite';
2-
import { ASTRO_VITE_ENVIRONMENT_NAMES } from '../core/constants.js';
32
import { VIRTUAL_PAGE_RESOLVED_MODULE_ID } from '../vite-plugin-pages/const.js';
43
import { getDevCssModuleNameFromPageVirtualModuleName } from '../vite-plugin-css/util.js';
4+
import { isAstroServerEnvironment } from '../environments.js';
55

66
/**
77
* The very last Vite plugin to reload the browser if any SSR-only module are updated
@@ -15,7 +15,7 @@ export default function hmrReload(): Plugin {
1515
hotUpdate: {
1616
order: 'post',
1717
handler({ modules, server, timestamp }) {
18-
if (this.environment.name !== ASTRO_VITE_ENVIRONMENT_NAMES.ssr) return;
18+
if (!isAstroServerEnvironment(this.environment)) return;
1919

2020
let hasSsrOnlyModules = false;
2121

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)