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:
parent
dad5e2e513
commit
4f1073a6a4
4 changed files with 49 additions and 2 deletions
5
.changeset/young-schools-refuse.md
Normal file
5
.changeset/young-schools-refuse.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fix astro:assets interfering with SSR query params ending with image extensions
|
|
@ -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)}`;
|
||||
}
|
||||
|
|
|
@ -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({
|
||||
|
|
11
packages/astro/test/fixtures/core-image-ssr/src/pages/api.ts
vendored
Normal file
11
packages/astro/test/fixtures/core-image-ssr/src/pages/api.ts
vendored
Normal 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;
|
Loading…
Reference in a new issue