diff --git a/.changeset/quick-crabs-lay.md b/.changeset/quick-crabs-lay.md new file mode 100644 index 000000000..48f488797 --- /dev/null +++ b/.changeset/quick-crabs-lay.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes HMR support for inline scripts in Astro components on Linux and OSX diff --git a/packages/astro/e2e/astro-component.test.js b/packages/astro/e2e/astro-component.test.js index 65499499f..5dcf2daea 100644 --- a/packages/astro/e2e/astro-component.test.js +++ b/packages/astro/e2e/astro-component.test.js @@ -1,4 +1,5 @@ import { test as base, expect } from '@playwright/test'; +import os from 'os'; import { loadFixture } from './test-utils.js'; const test = base.extend({ @@ -18,8 +19,8 @@ test.afterEach(async () => { await devServer.stop(); }); -test.describe('Astro components', () => { - test('HMR', async ({ page, astro }) => { +test.describe('Astro component HMR', () => { + test('component styles', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); const hero = page.locator('section'); @@ -39,4 +40,23 @@ test.describe('Astro components', () => { 'rgb(230, 230, 230)' ); }); + + // TODO: Re-enable this test on windows when #3424 is fixed + // https://github.com/withastro/astro/issues/3424 + const it = os.platform() === 'win32' ? test.skip : test; + it('hoisted scripts', async ({ page, astro }) => { + const initialLog = page.waitForEvent('console', (message) => message.text() === 'Hello, Astro!'); + + await page.goto(astro.resolveUrl('/')); + await initialLog; + + const updatedLog = page.waitForEvent('console', (message) => message.text() === 'Hello, updated Astro!'); + + // Edit the hoisted script on the page + await astro.editFile('./src/pages/index.astro', (content) => + content.replace('Hello, Astro!', 'Hello, updated Astro!') + ); + + await updatedLog; + }); }); diff --git a/packages/astro/e2e/fixtures/astro-component/src/pages/index.astro b/packages/astro/e2e/fixtures/astro-component/src/pages/index.astro index a52ee713f..76221b040 100644 --- a/packages/astro/e2e/fixtures/astro-component/src/pages/index.astro +++ b/packages/astro/e2e/fixtures/astro-component/src/pages/index.astro @@ -14,3 +14,7 @@ import Hero from '../components/Hero.astro'; + + diff --git a/packages/astro/src/runtime/server/metadata.ts b/packages/astro/src/runtime/server/metadata.ts index 9fb23724d..9379cc3ff 100644 --- a/packages/astro/src/runtime/server/metadata.ts +++ b/packages/astro/src/runtime/server/metadata.ts @@ -102,7 +102,8 @@ export class Metadata { let i = 0, pathname = metadata.mockURL.pathname; while (i < metadata.hoisted.length) { - yield `${pathname}?astro&type=script&index=${i}`; + // Strip off the leading "/@fs" added during compilation. + yield `${pathname.replace('/@fs', '')}?astro&type=script&index=${i}`; i++; } }