Skip to content

Commit

Permalink
fix(astro/core): Do not add base to hoisted script body (#5720)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
umarov committed Jan 3, 2023
1 parent c2844a7 commit fe316be
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-carpets-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Do not add base path to a hoisted script body
6 changes: 3 additions & 3 deletions packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
})
);
}
Expand Down
20 changes: 20 additions & 0 deletions packages/astro/test/ssr-hoisted-script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit fe316be

Please sign in to comment.