1- export type RemotePattern = {
1+ type RemotePattern = {
22 protocol ?: "http" | "https" ;
33 hostname : string ;
44 port ?: string ;
@@ -7,6 +7,12 @@ export type RemotePattern = {
77 search ?: string ;
88} ;
99
10+ type LocalPattern = {
11+ // pathname is always set in the manifest
12+ pathname : string ;
13+ search ?: string ;
14+ } ;
15+
1016/**
1117 * Fetches an images.
1218 *
@@ -22,16 +28,23 @@ export function fetchImage(fetcher: Fetcher | undefined, imageUrl: string) {
2228 // Local
2329 if ( imageUrl . startsWith ( "/" ) ) {
2430 let pathname : string ;
31+ let url : URL ;
2532 try {
26- const url = new URL ( imageUrl , "http://n" ) ;
33+ // We only need pathname and search
34+ url = new URL ( imageUrl , "http://n" ) ;
2735 pathname = decodeURIComponent ( url . pathname ) ;
2836 } catch {
2937 return getUrlErrorResponse ( ) ;
3038 }
39+
3140 if ( / \/ _ n e x t \/ i m a g e ( $ | \/ ) / . test ( pathname ) ) {
3241 return getUrlErrorResponse ( ) ;
3342 }
3443
44+ if ( ! __IMAGES_LOCAL_PATTERNS__ . some ( ( p : LocalPattern ) => matchLocalPattern ( p , url ) ) ) {
45+ return getUrlErrorResponse ( ) ;
46+ }
47+
3548 return fetcher ?. fetch ( `http://assets.local${ imageUrl } ` ) ;
3649 }
3750
@@ -83,6 +96,21 @@ export function matchRemotePattern(pattern: RemotePattern, url: URL): boolean {
8396 return true ;
8497}
8598
99+ export function matchLocalPattern ( pattern : LocalPattern , url : URL ) : boolean {
100+ // https://github.com/vercel/next.js/blob/d76f0b1/packages/next/src/shared/lib/match-local-pattern.ts
101+ if ( pattern . search !== undefined ) {
102+ if ( pattern . search !== url . search ) {
103+ return false ;
104+ }
105+ }
106+
107+ if ( ! new RegExp ( pattern . pathname ) . test ( url . pathname ) ) {
108+ return false ;
109+ }
110+
111+ return true ;
112+ }
113+
86114/**
87115 * @returns same error as Next.js when the url query parameter is not accepted.
88116 */
@@ -93,6 +121,6 @@ function getUrlErrorResponse() {
93121/* eslint-disable no-var */
94122declare global {
95123 var __IMAGES_REMOTE_PATTERNS__ : RemotePattern [ ] ;
96- var __IMAGES_LOCAL_PATTERNS__ : unknown [ ] ;
124+ var __IMAGES_LOCAL_PATTERNS__ : LocalPattern [ ] ;
97125}
98126/* eslint-enable no-var */
0 commit comments