Skip to content

Commit

Permalink
fix: handle base path with images (#12272)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Oct 21, 2024
1 parent c2ee963 commit 388d237
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-parents-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Correctly handles local images when using a base path in SSR
6 changes: 6 additions & 0 deletions packages/astro/src/assets/endpoint/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ describe('astro:image', () => {
},
adapter: testAdapter(),
image: {
endpoint: 'astro/assets/endpoint/node',
service: testImageService(),
},
base: '/blog',
Expand All @@ -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);
});
});

Expand Down

0 comments on commit 388d237

Please sign in to comment.