Include base in 'page' stage injected scripts (#5572)

* Include base in 'page' stage injected scripts

* Add changeset
This commit is contained in:
Matthew Phillips 2022-12-10 06:45:59 -05:00 committed by GitHub
parent 50ecb3005d
commit b2f0210c40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 9 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Include base in 'page' stage injected scripts

View file

@ -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));

View file

@ -0,0 +1 @@
console.log("this is injected by injectScript");

View file

@ -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,
@ -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 () => {