Skip to content

Commit fe316be

Browse files
authored
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
1 parent c2844a7 commit fe316be

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

.changeset/good-carpets-confess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Do not add base path to a hoisted script body

packages/astro/src/core/build/vite-plugin-ssr.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ const _manifest = Object.assign(_deserializeManifest('${manifestReplace}'), {
4444
renderers: _main.renderers
4545
});
4646
const _args = ${adapter.args ? JSON.stringify(adapter.args) : 'undefined'};
47-
4847
export * from '${pagesVirtualModuleId}';
49-
5048
${
5149
adapter.exports
5250
? `const _exports = adapter.createExports(_manifest, _args);
@@ -165,9 +163,11 @@ function buildManifest(
165163
for (const pageData of eachServerPageData(internals)) {
166164
const scripts: SerializedRouteInfo['scripts'] = [];
167165
if (pageData.hoistedScript) {
166+
const hoistedValue = pageData.hoistedScript.value
167+
const value = hoistedValue.endsWith('.js') ? joinBase(hoistedValue) : hoistedValue
168168
scripts.unshift(
169169
Object.assign({}, pageData.hoistedScript, {
170-
value: joinBase(pageData.hoistedScript.value),
170+
value,
171171
})
172172
);
173173
}

packages/astro/test/ssr-hoisted-script.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,24 @@ describe('Hoisted scripts in SSR', () => {
2929
const $ = cheerioLoad(html);
3030
expect($('script').length).to.equal(1);
3131
});
32+
33+
describe('base path', () => {
34+
const base = '/hello';
35+
36+
before(async () => {
37+
fixture = await loadFixture({
38+
root: './fixtures/ssr-hoisted-script/',
39+
output: 'server',
40+
adapter: testAdapter(),
41+
base,
42+
});
43+
await fixture.build();
44+
});
45+
46+
it('Inlined scripts get included without base path in the script', async () => {
47+
const html = await fetchHTML('/hello/');
48+
const $ = cheerioLoad(html);
49+
expect($('script').html()).to.equal('console.log("hello world");\n');
50+
});
51+
});
3252
});

0 commit comments

Comments
 (0)