Skip to content

Commit

Permalink
Allow SVGs when using Assets (#7643)
Browse files Browse the repository at this point in the history
* Allow SVG files when using Assets

* Fixed TypeScript error

* fix: some small nits and add a test

* chore: changeset

---------

Co-authored-by: Princesseuh <[email protected]>
  • Loading branch information
alvinometric and Princesseuh authored Jul 13, 2023
1 parent bdde6b9 commit 4b82e55
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-bags-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add support for using `.svg` files with `astro:assets`'s base services. The SVGs will NOT be processed and will be return as-is, however, proper attributes, alt enforcement etc will all work correctly.
16 changes: 12 additions & 4 deletions packages/astro/src/assets/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ export const VALID_INPUT_FORMATS = [
'svg',
] as const;
/**
* Valid formats for optimizations in our base services.
* Certain formats can be imported (namely SVGs) but not processed, so they are excluded from this list.
* Valid formats that our base services support.
* Certain formats can be imported (namely SVGs) but will not be processed.
*/
export const VALID_OPTIMIZABLE_FORMATS = ['jpeg', 'jpg', 'png', 'tiff', 'webp', 'gif'] as const;
export const VALID_OUTPUT_FORMATS = ['avif', 'png', 'webp', 'jpeg', 'jpg'] as const;
export const VALID_SUPPORTED_FORMATS = [
'jpeg',
'jpg',
'png',
'tiff',
'webp',
'gif',
'svg',
] as const;
export const VALID_OUTPUT_FORMATS = ['avif', 'png', 'webp', 'jpeg', 'jpg', 'svg'] as const;
11 changes: 8 additions & 3 deletions packages/astro/src/assets/services/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
import { joinPaths } from '../../core/path.js';
import { VALID_OPTIMIZABLE_FORMATS } from '../consts.js';
import { VALID_SUPPORTED_FORMATS } from '../consts.js';
import { isESMImportedImage } from '../internal.js';
import type { ImageOutputFormat, ImageTransform } from '../types.js';

Expand Down Expand Up @@ -143,16 +143,21 @@ export const baseService: Omit<LocalImageService, 'transform'> = {
});
}
} else {
if (!VALID_OPTIMIZABLE_FORMATS.includes(options.src.format as any)) {
if (!VALID_SUPPORTED_FORMATS.includes(options.src.format as any)) {
throw new AstroError({
...AstroErrorData.UnsupportedImageFormat,
message: AstroErrorData.UnsupportedImageFormat.message(
options.src.format,
options.src.src,
VALID_OPTIMIZABLE_FORMATS
VALID_SUPPORTED_FORMATS
),
});
}

// We currently do not support processing SVGs, so whenever the input format is a SVG, force the output to also be one
if (options.src.format === 'svg') {
options.format = 'svg';
}
}

// If the user didn't specify a format, we'll default to `webp`. It offers the best ratio of compatibility / quality
Expand Down
4 changes: 4 additions & 0 deletions packages/astro/src/assets/services/sharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const sharpService: LocalImageService = {

const transform: BaseServiceTransform = transformOptions as BaseServiceTransform;

// Return SVGs as-is
// TODO: Sharp has some support for SVGs, we could probably support this once Sharp is the default and only service.
if (transform.format === 'svg') return { data: inputBuffer, format: 'svg' };

let result = sharp(inputBuffer, { failOnError: false, pages: -1 });

// Never resize using both width and height at the same time, prioritizing width.
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/assets/services/squoosh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { Operation } from './vendor/squoosh/image.js';

const baseQuality = { low: 25, mid: 50, high: 80, max: 100 };
const qualityTable: Record<
Exclude<ImageOutputFormat, 'png'>,
Exclude<ImageOutputFormat, 'png' | 'svg'>,
Record<ImageQualityPreset, number>
> = {
avif: {
Expand All @@ -38,6 +38,9 @@ const service: LocalImageService = {

let format = transform.format;

// Return SVGs as-is
if (format === 'svg') return { data: inputBuffer, format: 'svg' };

const operations: Operation[] = [];

// Never resize using both width and height at the same time, prioritizing width.
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati
message: (format: string, imagePath: string, supportedFormats: readonly string[]) =>
`Received unsupported format \`${format}\` from \`${imagePath}\`. Currently only ${supportedFormats.join(
', '
)} are supported for optimization.`,
hint: "If you do not need optimization, using an `img` tag directly instead of the `Image` component might be what you're looking for.",
)} are supported by our image services.`,
hint: "Using an `img` tag directly instead of the `Image` component might be what you're looking for.",
},
/**
* @docs
Expand Down
18 changes: 11 additions & 7 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ describe('astro:image', () => {
expect(res.headers.get('content-type')).to.equal('image/webp');
});

it('errors on unsupported formats', async () => {
logs.length = 0;
let res = await fixture.fetch('/unsupported-format');
await res.text();
it('properly skip processing SVGs, but does not error', async () => {
let res = await fixture.fetch('/svgSupport');
let html = await res.text();

expect(logs).to.have.a.lengthOf(1);
expect(logs[0].message).to.contain('Received unsupported format');
});
$ = cheerio.load(html);
let $img = $('img');
expect($img).to.have.a.lengthOf(1);

let src = $img.attr('src');
res = await fixture.fetch(src);
expect(res.status).to.equal(200);
})

it("errors when an ESM imported image's src is passed to Image/getImage instead of the full import", async () => {
logs.length = 0;
Expand Down

0 comments on commit 4b82e55

Please sign in to comment.