Skip to content

Commit

Permalink
Include base in 'page' stage injected scripts (#5572)
Browse files Browse the repository at this point in the history
* Include base in 'page' stage injected scripts

* Add changeset
  • Loading branch information
matthewp committed Dec 10, 2022
1 parent 50ecb30 commit b2f0210
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-days-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Include base in 'page' stage injected scripts
7 changes: 6 additions & 1 deletion packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ function buildManifest(
);
}
if (settings.scripts.some((script) => script.stage === 'page')) {
scripts.push({ type: 'external', value: entryModules[PAGE_SCRIPT_ID] });
const src = entryModules[PAGE_SCRIPT_ID];

scripts.push({
type: 'external',
value: joinBase(src)
});
}

const links = sortedCSS(pageData).map((pth) => joinBase(pth));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("this is injected by injectScript");
28 changes: 20 additions & 8 deletions packages/astro/test/ssr-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ describe('Using Astro.request in SSR', () => {
adapter: testAdapter(),
output: 'server',
base: '/subpath/',
integrations: [
{
name: 'inject-script',
hooks: {
'astro:config:setup'({ injectScript }) {
injectScript('page', 'import "/src/scripts/inject-script.js";')
}
}
}
],
vite: {
build: {
assetsInlineLimit: 0,
Expand Down Expand Up @@ -64,15 +74,17 @@ describe('Using Astro.request in SSR', () => {
const html = await response.text();
const $ = cheerioLoad(html);

const scriptSrc = $('script').attr('src');
expect(scriptSrc.startsWith('/subpath/')).to.equal(true);
for(const el of $('script')) {
const scriptSrc = $(el).attr('src');
expect(scriptSrc.startsWith('/subpath/')).to.equal(true);

request = new Request('http://example.com' + scriptSrc);
response = await app.render(request);

expect(response.status).to.equal(200);
const js = await response.text();
expect(js).to.not.be.an('undefined');
request = new Request('http://example.com' + scriptSrc);
response = await app.render(request);

expect(response.status).to.equal(200);
const js = await response.text();
expect(js).to.not.be.an('undefined');
}
});

it('assets can be fetched', async () => {
Expand Down

0 comments on commit b2f0210

Please sign in to comment.