diff --git a/.changeset/wise-parents-agree.md b/.changeset/wise-parents-agree.md new file mode 100644 index 000000000000..e686f70900bf --- /dev/null +++ b/.changeset/wise-parents-agree.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Correctly handles local images when using a base path in SSR diff --git a/packages/astro/src/assets/endpoint/node.ts b/packages/astro/src/assets/endpoint/node.ts index 859af83e2a88..59c0e02384df 100644 --- a/packages/astro/src/assets/endpoint/node.ts +++ b/packages/astro/src/assets/endpoint/node.ts @@ -24,6 +24,12 @@ async function loadLocalImage(src: string, url: URL) { fileUrl = pathToFileURL(removeQueryString(replaceFileSystemReferences(src))); } else { try { + // If the _image segment isn't at the start of the path, we have a base + const idx = url.pathname.indexOf('/_image'); + if (idx > 0) { + // Remove the base path + src = src.slice(idx); + } fileUrl = new URL('.' + src, outDir); const filePath = fileURLToPath(fileUrl); diff --git a/packages/astro/test/core-image.test.js b/packages/astro/test/core-image.test.js index 98656f4f0d7c..9cc2585e4bfc 100644 --- a/packages/astro/test/core-image.test.js +++ b/packages/astro/test/core-image.test.js @@ -820,6 +820,7 @@ describe('astro:image', () => { }, adapter: testAdapter(), image: { + endpoint: 'astro/assets/endpoint/node', service: testImageService(), }, base: '/blog', @@ -833,6 +834,8 @@ describe('astro:image', () => { const $ = cheerio.load(html); const src = $('#local img').attr('src'); assert.equal(src.startsWith('/blog'), true); + const img = await app.render(new Request(`https://example.com${src}`)); + assert.equal(img.status, 200); }); });