Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Unoptimized SVG images #7244

Merged
merged 7 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions apps/site/components/MDX/Image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import type { ImageProps } from 'next/image';
import Image from 'next/image';
import type { FC } from 'react';

const MDXImage: FC<ImageProps> = ({ width, height, alt, ...props }) => {
import { isSvgImage } from '@/util/imageUtils';

const MDXImage: FC<ImageProps> = ({ width, height, alt, src, ...props }) => {
canerakdas marked this conversation as resolved.
Show resolved Hide resolved
const isUnoptimizedImage = isSvgImage(src.toString());

if (!width || !height) {
// Since `width` and `height` are not provided in the Markdown image format,
// we provide the height and width automatically.
// @see https://nextjs.org/docs/pages/building-your-application/optimizing/images
return (
<Image
{...props}
src={src}
unoptimized={isUnoptimizedImage}
alt={alt}
width={0}
height={0}
Expand All @@ -19,7 +25,16 @@ const MDXImage: FC<ImageProps> = ({ width, height, alt, ...props }) => {
);
}

return <Image {...props} alt={alt} width={width} height={height} />;
return (
<Image
{...props}
alt={alt}
width={width}
height={height}
src={src}
unoptimized={isUnoptimizedImage}
/>
);
};

export default MDXImage;
2 changes: 0 additions & 2 deletions apps/site/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const nextConfig = {
images: {
// We disable image optimisation during static export builds
unoptimized: ENABLE_STATIC_EXPORT,
// We allow SVGs to be used as images
dangerouslyAllowSVG: true,
// We add it to the remote pattern for the static images we use from GitHub
remotePatterns: [
{
Expand Down
43 changes: 43 additions & 0 deletions apps/site/util/__tests__/imageUtils.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { isSvgImage } from '@/util/imageUtils';

describe('isSvgImage', () => {
const testCases = [
{
description: 'should return true for a valid .svg URL',
input: 'https://nodejs.org/image.svg',
expected: true,
},
{
description: 'should return true for a URL with query params',
input: 'https://nodejs.org/image.svg?query=param',
expected: true,
},
{
description: 'should return false for a URL without a .svg extension',
input: 'https://nodejs.org/image',
expected: false,
},
{
description:
'should return false for a URL containing ".svg" but not ending with it',
input: 'https://nodejs.org/image.svg.png',
expected: false,
},
{
description: 'should return false for an empty string',
input: '',
expected: false,
},
{
description: 'should return false for a non-URL string',
input: 'not-a-url',
expected: false,
},
];

testCases.forEach(({ description, input, expected }) => {
it(description, () => {
expect(isSvgImage(input)).toBe(expected);
});
});
});
19 changes: 19 additions & 0 deletions apps/site/util/imageUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* This is a temporary workaround that can be removed once Next.js is upgraded.
* See https://github.com/vercel/next.js/pull/72970
*
* Checks if the given source string points to an SVG image.
ovflowd marked this conversation as resolved.
Show resolved Hide resolved
*
* This function examines the base part of the provided string (ignoring query parameters)
* to determine if it ends with the `.svg` extension.
*
* @param src - The URL or string representing the source of the image.
* @returns `true` if the source points to an SVG image, otherwise `false`.
*/
export const isSvgImage = (src: string): boolean => {
// Split the source string at the '?' character to separate the main path from query parameters
const [image] = src.split('?');

// Check if the base path (before any query parameters) ends with '.svg'
return image.endsWith('.svg');
};
Loading