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

fix(image): Fix astro:assets from interfering with SSR query params ending with image extensions (#7055)

* fix(image): Fix `astro:assets` from interfering with SSR query params ending with image extensions

* test: add test

* nit: nit

* chore: changeset
This commit is contained in:
Erika 2023-05-11 20:29:32 +02:00 committed by GitHub
parent dad5e2e513
commit 4f1073a6a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix astro:assets interfering with SSR query params ending with image extensions

View file

@ -8,7 +8,12 @@ import type * as vite from 'vite';
import { normalizePath } from 'vite';
import type { AstroPluginOptions, ImageTransform } from '../@types/astro';
import { error } from '../core/logger/core.js';
import { appendForwardSlash, joinPaths, prependForwardSlash } from '../core/path.js';
import {
appendForwardSlash,
joinPaths,
prependForwardSlash,
removeQueryString,
} from '../core/path.js';
import { VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './consts.js';
import { isESMImportedImage } from './internal.js';
import { isLocalService } from './services/service.js';
@ -228,7 +233,8 @@ export default function assets({
resolvedConfig = viteConfig;
},
async load(id) {
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(id)) {
const cleanedUrl = removeQueryString(id);
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(cleanedUrl)) {
const meta = await emitESMImage(id, this.meta.watchMode, this.emitFile, settings);
return `export default ${JSON.stringify(meta)}`;
}

View file

@ -630,6 +630,31 @@ describe('astro:image', () => {
});
});
describe('dev ssr', () => {
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/core-image-ssr/',
output: 'server',
adapter: testAdapter(),
experimental: {
assets: true,
},
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('does not interfere with query params', async () => {
let res = await fixture.fetch('/api?src=image.png');
const html = await res.text();
expect(html).to.equal('An image: "image.png"');
});
});
describe('prod ssr', () => {
before(async () => {
fixture = await loadFixture({

View file

@ -0,0 +1,11 @@
import { APIRoute } from "../../../../../src/@types/astro";
export const get = (async ({ params, request }) => {
const url = new URL(request.url);
console.log(url)
const src = url.searchParams.get("src");
return {
body: "An image: " + JSON.stringify(src),
};
}) satisfies APIRoute;