Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include base in 'page' stage injected scripts #5572

Merged
merged 2 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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