Skip to content

Commit

Permalink
Merge pull request #24187 from json-betsec/fix-next-image-search-params
Browse files Browse the repository at this point in the history
NextJS: Fix default next image loader when src has params
  • Loading branch information
valentinpalkovic authored Oct 5, 2023
2 parents 7d8a2c6 + 74040b7 commit 4e11f29
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions code/frameworks/nextjs/src/images/next-image-default-loader.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as _NextImage from 'next/image';

export const defaultLoader = ({ src, width, quality }: _NextImage.ImageLoaderProps) => {
export const defaultLoader = ({ src, width, quality = 75 }: _NextImage.ImageLoaderProps) => {
const missingValues = [];
if (!src) {
missingValues.push('src');
Expand All @@ -24,5 +24,16 @@ export const defaultLoader = ({ src, width, quality }: _NextImage.ImageLoaderPro
);
}

return `${src}?w=${width}&q=${quality ?? 75}`;
const url = new URL(src, window.location.href);

if (!url.searchParams.has('w') && !url.searchParams.has('q')) {
url.searchParams.set('w', width.toString());
url.searchParams.set('q', quality.toString());
}

if (!src.startsWith('http://') && !src.startsWith('https://')) {
return url.toString().slice(url.origin.length);
}

return url.toString();
};

0 comments on commit 4e11f29

Please sign in to comment.