0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

fix: handle base path with images (#12272)

This commit is contained in:
Matt Kane 2024-10-21 16:50:30 +01:00 committed by GitHub
parent c2ee963cb6
commit 388d2375b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Correctly handles local images when using a base path in SSR

View file

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

View file

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