From feb9a3141e43b4bbe643a6676f8f0e6eeff410e2 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 24 Jun 2021 17:05:55 -0400 Subject: [PATCH] Always add the HMR script in dev (#536) * Always add the HMR script in dev for livereload * Adds the changeset --- .changeset/old-buses-tell.md | 5 +++++ packages/astro/src/compiler/transform/head.ts | 2 +- packages/astro/test/astro-basic.test.js | 6 ------ packages/astro/test/astro-hmr.test.js | 12 +++++++++++- .../test/fixtures/astro-hmr/src/pages/static.astro | 12 ++++++++++++ 5 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 .changeset/old-buses-tell.md create mode 100644 packages/astro/test/fixtures/astro-hmr/src/pages/static.astro diff --git a/.changeset/old-buses-tell.md b/.changeset/old-buses-tell.md new file mode 100644 index 000000000..41473d671 --- /dev/null +++ b/.changeset/old-buses-tell.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes livereload on static pages diff --git a/packages/astro/src/compiler/transform/head.ts b/packages/astro/src/compiler/transform/head.ts index fdf07cdfc..b081491a3 100644 --- a/packages/astro/src/compiler/transform/head.ts +++ b/packages/astro/src/compiler/transform/head.ts @@ -123,7 +123,7 @@ export default function (opts: TransformOptions): Transformer { }); } - if (isHmrEnabled && hasComponents) { + if (isHmrEnabled) { const { hmrPort } = opts.compileOptions; children.push( { diff --git a/packages/astro/test/astro-basic.test.js b/packages/astro/test/astro-basic.test.js index 4f8fc3a32..cf964e3b1 100644 --- a/packages/astro/test/astro-basic.test.js +++ b/packages/astro/test/astro-basic.test.js @@ -27,12 +27,6 @@ Basics('Sets the HMR port when dynamic components used', async ({ runtime }) => assert.ok(/HMR_WEBSOCKET_PORT/.test(html), 'Sets the websocket port'); }); -Basics('Does not set the HMR port when no dynamic component used', async ({ runtime }) => { - const result = await runtime.load('/'); - const html = result.contents; - assert.ok(!/HMR_WEBSOCKET_PORT/.test(html), 'Does not set the websocket port'); -}); - Basics('Correctly serializes boolean attributes', async ({ runtime }) => { const result = await runtime.load('/'); const html = result.contents; diff --git a/packages/astro/test/astro-hmr.test.js b/packages/astro/test/astro-hmr.test.js index 5d4fb5f0e..2d295c0b7 100644 --- a/packages/astro/test/astro-hmr.test.js +++ b/packages/astro/test/astro-hmr.test.js @@ -21,7 +21,7 @@ HMR('Honors the user provided port', async ({ runtime }) => { HMR('Does not override script added by the user', async ({ runtime }) => { const result = await runtime.load('/manual'); - console.log(result.error); + if (result.error) throw new Error(result.error); const html = result.contents; @@ -29,4 +29,14 @@ HMR('Does not override script added by the user', async ({ runtime }) => { assert.ok(/window\.HMR_WEBSOCKET_PORT = 5555/.test(html), 'Ignored when window.HMR_WEBSOCKET_URL set'); }); +HMR('Adds script to static pages too', async ({ runtime }) => { + const result = await runtime.load('/static'); + if (result.error) throw new Error(result.error); + + const html = result.contents; + const $ = doc(html); + assert.equal($('[src="/_snowpack/hmr-client.js"]').length, 1); + assert.ok(/window\.HMR_WEBSOCKET_PORT/.test(html), 'websocket port added'); +}); + HMR.run(); diff --git a/packages/astro/test/fixtures/astro-hmr/src/pages/static.astro b/packages/astro/test/fixtures/astro-hmr/src/pages/static.astro new file mode 100644 index 000000000..3c6cd0147 --- /dev/null +++ b/packages/astro/test/fixtures/astro-hmr/src/pages/static.astro @@ -0,0 +1,12 @@ +--- +import Tour from '../components/Tour.jsx'; +--- + + + My Test + + +
Hello world
+ + + \ No newline at end of file