0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix: properly prefix endpoint path with base in SSR (#7047)

* fix: properly prefix endpoint path with base in SSR

* chore: sssss

* chore: changeset
This commit is contained in:
Erika 2023-05-09 20:53:06 +02:00 committed by GitHub
parent 10e34b6d71
commit 48395c8152
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix `/_image` endpoint not being prefixed with the `base` path in build SSR

View file

@ -1,4 +1,5 @@
import { AstroError, AstroErrorData } from '../../core/errors/index.js'; import { AstroError, AstroErrorData } from '../../core/errors/index.js';
import { joinPaths } from '../../core/path.js';
import { VALID_OPTIMIZABLE_FORMATS } from '../consts.js'; import { VALID_OPTIMIZABLE_FORMATS } from '../consts.js';
import { isESMImportedImage } from '../internal.js'; import { isESMImportedImage } from '../internal.js';
import type { ImageOutputFormat, ImageTransform } from '../types.js'; import type { ImageOutputFormat, ImageTransform } from '../types.js';
@ -195,7 +196,7 @@ export const baseService: Omit<LocalImageService, 'transform'> = {
options.quality && searchParams.append('q', options.quality.toString()); options.quality && searchParams.append('q', options.quality.toString());
options.format && searchParams.append('f', options.format); options.format && searchParams.append('f', options.format);
return '/_image?' + searchParams; return joinPaths(import.meta.env.BASE_URL, '/_image?') + searchParams;
}, },
parseURL(url) { parseURL(url) {
const params = url.searchParams; const params = url.searchParams;

View file

@ -447,6 +447,27 @@ describe('astro:image', () => {
expect(src.length).to.be.greaterThan(0); expect(src.length).to.be.greaterThan(0);
expect(src.startsWith('/blog')).to.be.true; expect(src.startsWith('/blog')).to.be.true;
}); });
it('has base path prefix in SSR', async () => {
const fixtureWithBase = await loadFixture({
root: './fixtures/core-image-ssr/',
output: 'server',
adapter: testAdapter(),
experimental: {
assets: true,
},
base: '/blog',
});
await fixtureWithBase.build();
const app = await fixtureWithBase.loadTestAdapterApp();
const request = new Request('http://example.com/blog/');
const response = await app.render(request);
expect(response.status).to.equal(200);
const html = await response.text();
const $ = cheerio.load(html);
const src = $('#local img').attr('src');
expect(src.startsWith('/blog')).to.be.true;
});
}); });
describe('build ssg', () => { describe('build ssg', () => {