@@ -2,9 +2,9 @@ import * as React from 'react';
22import Image , { ImageLoaderProps , ImageProps } from 'next/image' ;
33
44import { createHmac } from 'crypto' ;
5- import { ServerResponse , request as httpRequest } from 'http' ;
6- import { request as httpsRequest } from 'https' ;
75import { ParsedUrlQuery } from 'node:querystring' ;
6+ import { request as httpsRequest } from 'https' ;
7+ import { ServerResponse , request as httpRequest } from 'http' ;
88
99import ImgProxyParamBuilder from './param-builder' ;
1010
@@ -26,15 +26,17 @@ const imageOptimizer = (
2626 salt : string ;
2727 } ,
2828) => {
29- const { src, params } = query ;
29+ const { src, params, format } = query ;
3030 if ( ! src ) {
3131 res . statusCode = 400 ;
3232 res . end ( ) ;
3333 return ;
3434 }
3535
36+ const fileFormat = format ? `@${ format } ` : '' ;
3637 const paramString = params ? `${ params } /` : '' ;
37- const requestPath = `/${ paramString } plain/s3://${ src } @png` ;
38+ const requestPath = `/${ paramString } plain/s3://${ src } ${ fileFormat } ` ;
39+
3840 const signature = signatureParams
3941 ? generateSignature ( signatureParams . key , signatureParams . salt , requestPath )
4042 : '' ;
@@ -71,27 +73,38 @@ const imageOptimizer = (
7173 req . end ( ) ;
7274} ;
7375
74- const buildProxyImagePath = ( file : string , proxyParams ?: string ) : string => {
76+ type ProxyImageProps = {
77+ file : string ;
78+ format ?: string ;
79+ proxyParams ?: string ;
80+ } ;
81+
82+ const buildProxyImagePath = (
83+ file : string ,
84+ options ?: Omit < ProxyImageProps , 'file' > ,
85+ ) : string => {
86+ const { proxyParams, format } = options ?? { } ;
87+
7588 const urlParams = new URLSearchParams ( ) ;
89+
7690 urlParams . append ( 'src' , file ) ;
7791 if ( proxyParams ) urlParams . append ( 'params' , proxyParams ) ;
78- return `${ IMGPROXY_ENDPOINT } ?${ urlParams . toString ( ) } ` ;
79- } ;
92+ if ( format ) urlParams . append ( 'format' , format ) ;
8093
81- type ProxyImageProps = {
82- file : string ;
83- proxyParams ?: string ;
94+ return `${ IMGPROXY_ENDPOINT } ?${ urlParams . toString ( ) } ` ;
8495} ;
8596
8697const ProxyImage = ( {
8798 file,
8899 proxyParams,
100+ format,
89101 ...props
90102} : ProxyImageProps & Omit < ImageProps , 'src' | 'quality' | 'unoptimized' > ) => {
91103 const imageLoader = ( { src, width } : ImageLoaderProps ) : string => {
92104 const urlParams = new URLSearchParams ( ) ;
93105 urlParams . append ( 'src' , src ) ;
94106 if ( proxyParams ) urlParams . append ( 'params' , proxyParams ) ;
107+ if ( format ) urlParams . append ( 'format' , format ) ;
95108
96109 // This doesn't actually do anything, it's just to suppress
97110 // this error https://nextjs.org/docs/messages/next-image-missing-loader-width
0 commit comments