Skip to content

Commit

Permalink
fix(image): Fix astro:assets from interfering with SSR query params e…
Browse files Browse the repository at this point in the history
…nding 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
  • Loading branch information
Princesseuh committed May 11, 2023
1 parent dad5e2e commit 4f1073a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-schools-refuse.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix astro:assets interfering with SSR query params ending with image extensions
10 changes: 8 additions & 2 deletions packages/astro/src/assets/vite-plugin-assets.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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)}`;
}
Expand Down
25 changes: 25 additions & 0 deletions packages/astro/test/core-image.test.js
Expand Up @@ -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({
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/core-image-ssr/src/pages/api.ts
@@ -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;

0 comments on commit 4f1073a

Please sign in to comment.