From b2f0210c400a547d3067fdae6d15663b827be3a6 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Sat, 10 Dec 2022 06:45:59 -0500 Subject: [PATCH] Include base in 'page' stage injected scripts (#5572) * Include base in 'page' stage injected scripts * Add changeset --- .changeset/few-days-refuse.md | 5 ++++ .../astro/src/core/build/vite-plugin-ssr.ts | 7 ++++- .../ssr-request/src/scripts/inject-script.js | 1 + packages/astro/test/ssr-request.test.js | 28 +++++++++++++------ 4 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 .changeset/few-days-refuse.md create mode 100644 packages/astro/test/fixtures/ssr-request/src/scripts/inject-script.js 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 () => {