Skip to content

Commit 78c14bd

Browse files
canerakdasstyfleovflowd
authored
fix: Unoptimized SVG images (#7244)
* fix: unoptimized SVG image * chore: review updates * Update apps/site/util/imageUtils.ts Co-authored-by: Steven <[email protected]> Signed-off-by: Caner Akdas <[email protected]> * chore: format files * chore: review update * Update apps/site/util/imageUtils.ts Co-authored-by: Steven <[email protected]> Signed-off-by: Claudio W <[email protected]> --------- Signed-off-by: Caner Akdas <[email protected]> Signed-off-by: Claudio W <[email protected]> Co-authored-by: Steven <[email protected]> Co-authored-by: Claudio W <[email protected]>
1 parent 635f2b3 commit 78c14bd

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

apps/site/components/MDX/Image/index.tsx

+17-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ import type { ImageProps } from 'next/image';
22
import Image from 'next/image';
33
import type { FC } from 'react';
44

5-
const MDXImage: FC<ImageProps> = ({ width, height, alt, ...props }) => {
5+
import { isSvgImage } from '@/util/imageUtils';
6+
7+
const MDXImage: FC<ImageProps> = ({ width, height, alt, src, ...props }) => {
8+
const isUnoptimizedImage = isSvgImage(src.toString());
9+
610
if (!width || !height) {
711
// Since `width` and `height` are not provided in the Markdown image format,
812
// we provide the height and width automatically.
913
// @see https://nextjs.org/docs/pages/building-your-application/optimizing/images
1014
return (
1115
<Image
1216
{...props}
17+
src={src}
18+
unoptimized={isUnoptimizedImage}
1319
alt={alt}
1420
width={0}
1521
height={0}
@@ -19,7 +25,16 @@ const MDXImage: FC<ImageProps> = ({ width, height, alt, ...props }) => {
1925
);
2026
}
2127

22-
return <Image {...props} alt={alt} width={width} height={height} />;
28+
return (
29+
<Image
30+
{...props}
31+
alt={alt}
32+
width={width}
33+
height={height}
34+
src={src}
35+
unoptimized={isUnoptimizedImage}
36+
/>
37+
);
2338
};
2439

2540
export default MDXImage;

apps/site/next.config.mjs

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ const nextConfig = {
2323
images: {
2424
// We disable image optimisation during static export builds
2525
unoptimized: ENABLE_STATIC_EXPORT,
26-
// We allow SVGs to be used as images
27-
dangerouslyAllowSVG: true,
2826
// We add it to the remote pattern for the static images we use from GitHub
2927
remotePatterns: [
3028
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { isSvgImage } from '@/util/imageUtils';
2+
3+
describe('isSvgImage', () => {
4+
const testCases = [
5+
{
6+
description: 'should return true for a valid .svg URL',
7+
input: 'https://nodejs.org/image.svg',
8+
expected: true,
9+
},
10+
{
11+
description: 'should return true for a URL with query params',
12+
input: 'https://nodejs.org/image.svg?query=param',
13+
expected: true,
14+
},
15+
{
16+
description: 'should return false for a URL without a .svg extension',
17+
input: 'https://nodejs.org/image',
18+
expected: false,
19+
},
20+
{
21+
description:
22+
'should return false for a URL containing ".svg" but not ending with it',
23+
input: 'https://nodejs.org/image.svg.png',
24+
expected: false,
25+
},
26+
{
27+
description: 'should return false for an empty string',
28+
input: '',
29+
expected: false,
30+
},
31+
{
32+
description: 'should return false for a non-URL string',
33+
input: 'not-a-url',
34+
expected: false,
35+
},
36+
];
37+
38+
testCases.forEach(({ description, input, expected }) => {
39+
it(description, () => {
40+
expect(isSvgImage(input)).toBe(expected);
41+
});
42+
});
43+
});

apps/site/util/imageUtils.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* This is a temporary workaround that can be removed once Next.js is upgraded.
3+
* See https://github.com/vercel/next.js/pull/72970
4+
*
5+
* Checks if the given source string points to an SVG image.
6+
*
7+
* This function examines the base part of the provided string (ignoring query parameters)
8+
* to determine if it ends with the `.svg` extension.
9+
*
10+
* @param src - The URL or string representing the source of the image.
11+
* @returns `true` if the source points to an SVG image, otherwise `false`.
12+
*/
13+
export const isSvgImage = (src: string): boolean => {
14+
// Split the source string at the '?' character to separate the main path from query parameters
15+
const [image] = src.split('?');
16+
17+
// Check if the base path (before any query parameters) ends with '.svg'
18+
return image.endsWith('.svg');
19+
};

0 commit comments

Comments
 (0)