From fe316be86f4dfecff78effbecdc10f7348406d27 Mon Sep 17 00:00:00 2001 From: Muzafar Umarov Date: Tue, 3 Jan 2023 08:59:05 -0500 Subject: [PATCH] fix(astro/core): Do not add base to hoisted script body (#5720) * fix(astro/core): Do not add base to hoisted script body * fix(astro/core): Add base path to hoisted .js files only * fix(astro/core): undo style changes --- .changeset/good-carpets-confess.md | 5 +++++ .../astro/src/core/build/vite-plugin-ssr.ts | 6 +++--- .../astro/test/ssr-hoisted-script.test.js | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 .changeset/good-carpets-confess.md diff --git a/.changeset/good-carpets-confess.md b/.changeset/good-carpets-confess.md new file mode 100644 index 000000000..c7095d3aa --- /dev/null +++ b/.changeset/good-carpets-confess.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Do not add base path to a hoisted script body diff --git a/packages/astro/src/core/build/vite-plugin-ssr.ts b/packages/astro/src/core/build/vite-plugin-ssr.ts index 928e3820e..f35a5270a 100644 --- a/packages/astro/src/core/build/vite-plugin-ssr.ts +++ b/packages/astro/src/core/build/vite-plugin-ssr.ts @@ -44,9 +44,7 @@ const _manifest = Object.assign(_deserializeManifest('${manifestReplace}'), { renderers: _main.renderers }); const _args = ${adapter.args ? JSON.stringify(adapter.args) : 'undefined'}; - export * from '${pagesVirtualModuleId}'; - ${ adapter.exports ? `const _exports = adapter.createExports(_manifest, _args); @@ -165,9 +163,11 @@ function buildManifest( for (const pageData of eachServerPageData(internals)) { const scripts: SerializedRouteInfo['scripts'] = []; if (pageData.hoistedScript) { + const hoistedValue = pageData.hoistedScript.value + const value = hoistedValue.endsWith('.js') ? joinBase(hoistedValue) : hoistedValue scripts.unshift( Object.assign({}, pageData.hoistedScript, { - value: joinBase(pageData.hoistedScript.value), + value, }) ); } diff --git a/packages/astro/test/ssr-hoisted-script.test.js b/packages/astro/test/ssr-hoisted-script.test.js index 7d31875ff..49e1e7b2f 100644 --- a/packages/astro/test/ssr-hoisted-script.test.js +++ b/packages/astro/test/ssr-hoisted-script.test.js @@ -29,4 +29,24 @@ describe('Hoisted scripts in SSR', () => { const $ = cheerioLoad(html); expect($('script').length).to.equal(1); }); + + describe('base path', () => { + const base = '/hello'; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/ssr-hoisted-script/', + output: 'server', + adapter: testAdapter(), + base, + }); + await fixture.build(); + }); + + it('Inlined scripts get included without base path in the script', async () => { + const html = await fetchHTML('/hello/'); + const $ = cheerioLoad(html); + expect($('script').html()).to.equal('console.log("hello world");\n'); + }); + }); });