From 0e2314d8e5b01f7b2184a243c6d7e53e14b0cd0f Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 14 Jun 2022 15:14:15 -0400 Subject: [PATCH] Fix inlined hoisted scripts and SSR (#3593) * Fix inlined hoisted scripts and SSR * Adds a changeset --- .changeset/loud-bikes-pay.md | 5 +++ packages/astro/src/core/app/index.ts | 2 +- .../ssr-hoisted-script/src/pages/index.astro | 9 +++++ .../astro/test/ssr-hoisted-script.test.js | 34 +++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 .changeset/loud-bikes-pay.md create mode 100644 packages/astro/test/fixtures/ssr-hoisted-script/src/pages/index.astro create mode 100644 packages/astro/test/ssr-hoisted-script.test.js diff --git a/.changeset/loud-bikes-pay.md b/.changeset/loud-bikes-pay.md new file mode 100644 index 000000000000..937f14dae303 --- /dev/null +++ b/.changeset/loud-bikes-pay.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes uses of inline hoisted scripts in SSR diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index dbda3cd44442..81cfe106a06a 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -80,7 +80,7 @@ export class App { const links = createLinkStylesheetElementSet(info.links, manifest.site); const filteredScripts = info.scripts.filter( - (script) => typeof script !== 'string' && script?.stage !== 'head-inline' + (script) => typeof script === 'string' || script?.stage !== 'head-inline' ) as string[]; const scripts = createModuleScriptElementWithSrcSet(filteredScripts, manifest.site); diff --git a/packages/astro/test/fixtures/ssr-hoisted-script/src/pages/index.astro b/packages/astro/test/fixtures/ssr-hoisted-script/src/pages/index.astro new file mode 100644 index 000000000000..6543f084700f --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hoisted-script/src/pages/index.astro @@ -0,0 +1,9 @@ + + + Testing + + +

Testing

+ + + diff --git a/packages/astro/test/ssr-hoisted-script.test.js b/packages/astro/test/ssr-hoisted-script.test.js new file mode 100644 index 000000000000..134744ae9f16 --- /dev/null +++ b/packages/astro/test/ssr-hoisted-script.test.js @@ -0,0 +1,34 @@ +import { expect } from 'chai'; +import { load as cheerioLoad } from 'cheerio'; +import { loadFixture } from './test-utils.js'; +import testAdapter from './test-adapter.js'; + +describe('Hoisted scripts in SSR', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/ssr-hoisted-script/', + experimental: { + ssr: true, + }, + adapter: testAdapter(), + }); + await fixture.build(); + }); + + async function fetchHTML(path) { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com' + path); + const response = await app.render(request); + const html = await response.text(); + return html; + } + + it('Inlined scripts get included', async () => { + const html = await fetchHTML('/'); + const $ = cheerioLoad(html); + expect($('script').length).to.equal(1); + }); +});