diff --git a/.changeset/few-days-refuse.md b/.changeset/few-days-refuse.md new file mode 100644 index 000000000000..55c595a5e078 --- /dev/null +++ b/.changeset/few-days-refuse.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Include base in 'page' stage injected scripts diff --git a/packages/astro/src/core/build/vite-plugin-ssr.ts b/packages/astro/src/core/build/vite-plugin-ssr.ts index 03dc2069989e..f3c333fa19d9 100644 --- a/packages/astro/src/core/build/vite-plugin-ssr.ts +++ b/packages/astro/src/core/build/vite-plugin-ssr.ts @@ -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)); diff --git a/packages/astro/test/fixtures/ssr-request/src/scripts/inject-script.js b/packages/astro/test/fixtures/ssr-request/src/scripts/inject-script.js new file mode 100644 index 000000000000..570612070abf --- /dev/null +++ b/packages/astro/test/fixtures/ssr-request/src/scripts/inject-script.js @@ -0,0 +1 @@ +console.log("this is injected by injectScript"); diff --git a/packages/astro/test/ssr-request.test.js b/packages/astro/test/ssr-request.test.js index 446a3e05dafa..08253a12a99b 100644 --- a/packages/astro/test/ssr-request.test.js +++ b/packages/astro/test/ssr-request.test.js @@ -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 () => {