1
1
import type { RemotePattern } from 'next/dist/shared/lib/image-config.js'
2
2
import { makeRe } from 'picomatch'
3
3
4
+ import { createIpxHandler } from './functions/ipx.js'
4
5
import { PluginContext } from './plugin-context.js'
5
6
6
7
function generateRegexFromPattern ( pattern : string ) : string {
@@ -18,75 +19,79 @@ export const setImageConfig = async (ctx: PluginContext): Promise<void> => {
18
19
return
19
20
}
20
21
21
- ctx . netlifyConfig . redirects . push (
22
- {
23
- from : imageEndpointPath ,
24
- // w and q are too short to be used as params with id-length rule
25
- // but we are forced to do so because of the next/image loader decides on their names
26
- // eslint-disable-next-line id-length
27
- query : { url : ':url' , w : ':width' , q : ':quality' } ,
28
- to : '/.netlify/images?url=:url&w=:width&q=:quality' ,
29
- status : 200 ,
30
- } ,
31
- // when migrating from @netlify /plugin-nextjs@4 image redirect to ipx might be cached in the browser
32
- {
33
- from : '/_ipx/*' ,
34
- // w and q are too short to be used as params with id-length rule
35
- // but we are forced to do so because of the next/image loader decides on their names
36
- // eslint-disable-next-line id-length
37
- query : { url : ':url' , w : ':width' , q : ':quality' } ,
38
- to : '/.netlify/images?url=:url&w=:width&q=:quality' ,
39
- status : 200 ,
40
- } ,
41
- )
22
+ if ( ctx . imageService === 'ipx' ) {
23
+ await createIpxHandler ( ctx )
24
+ } else {
25
+ ctx . netlifyConfig . redirects . push (
26
+ {
27
+ from : imageEndpointPath ,
28
+ // w and q are too short to be used as params with id-length rule
29
+ // but we are forced to do so because of the next/image loader decides on their names
30
+ // eslint-disable-next-line id-length
31
+ query : { url : ':url' , w : ':width' , q : ':quality' } ,
32
+ to : '/.netlify/images?url=:url&w=:width&q=:quality' ,
33
+ status : 200 ,
34
+ } ,
35
+ // when migrating from @netlify /plugin-nextjs@4 image redirect to ipx might be cached in the browser
36
+ {
37
+ from : '/_ipx/*' ,
38
+ // w and q are too short to be used as params with id-length rule
39
+ // but we are forced to do so because of the next/image loader decides on their names
40
+ // eslint-disable-next-line id-length
41
+ query : { url : ':url' , w : ':width' , q : ':quality' } ,
42
+ to : '/.netlify/images?url=:url&w=:width&q=:quality' ,
43
+ status : 200 ,
44
+ } ,
45
+ )
42
46
43
- if ( remotePatterns ?. length !== 0 || domains ?. length !== 0 ) {
44
- ctx . netlifyConfig . images ||= { remote_images : [ ] }
45
- ctx . netlifyConfig . images . remote_images ||= [ ]
47
+ if ( remotePatterns ?. length !== 0 || domains ?. length !== 0 ) {
48
+ ctx . netlifyConfig . images ||= { remote_images : [ ] }
49
+ ctx . netlifyConfig . images . remote_images ||= [ ]
46
50
47
- if ( remotePatterns && remotePatterns . length !== 0 ) {
48
- for ( const remotePattern of remotePatterns ) {
49
- let { protocol, hostname, port, pathname } : RemotePattern = remotePattern
51
+ if ( remotePatterns && remotePatterns . length !== 0 ) {
52
+ for ( const remotePattern of remotePatterns ) {
53
+ let { protocol, hostname, port, pathname } : RemotePattern = remotePattern
50
54
51
- if ( pathname ) {
52
- pathname = pathname . startsWith ( '/' ) ? pathname : `/${ pathname } `
53
- }
55
+ if ( pathname ) {
56
+ pathname = pathname . startsWith ( '/' ) ? pathname : `/${ pathname } `
57
+ }
54
58
55
- const combinedRemotePattern = `${ protocol ?? 'http?(s)' } ://${ hostname } ${
56
- port ? `:${ port } ` : ''
57
- } ${ pathname ?? '/**' } `
59
+ const combinedRemotePattern = `${ protocol ?? 'http?(s)' } ://${ hostname } ${
60
+ port ? `:${ port } ` : ''
61
+ } ${ pathname ?? '/**' } `
58
62
59
- try {
60
- ctx . netlifyConfig . images . remote_images . push (
61
- generateRegexFromPattern ( combinedRemotePattern ) ,
62
- )
63
- } catch ( error ) {
64
- ctx . failBuild (
65
- `Failed to generate Image CDN remote image regex from Next.js remote pattern: ${ JSON . stringify (
66
- { remotePattern, combinedRemotePattern } ,
67
- null ,
68
- 2 ,
69
- ) } `,
70
- error ,
71
- )
63
+ try {
64
+ ctx . netlifyConfig . images . remote_images . push (
65
+ generateRegexFromPattern ( combinedRemotePattern ) ,
66
+ )
67
+ } catch ( error ) {
68
+ ctx . failBuild (
69
+ `Failed to generate Image CDN remote image regex from Next.js remote pattern: ${ JSON . stringify (
70
+ { remotePattern, combinedRemotePattern } ,
71
+ null ,
72
+ 2 ,
73
+ ) } `,
74
+ error ,
75
+ )
76
+ }
72
77
}
73
78
}
74
- }
75
79
76
- if ( domains && domains . length !== 0 ) {
77
- for ( const domain of domains ) {
78
- const patternFromDomain = `http?(s)://${ domain } /**`
79
- try {
80
- ctx . netlifyConfig . images . remote_images . push ( generateRegexFromPattern ( patternFromDomain ) )
81
- } catch ( error ) {
82
- ctx . failBuild (
83
- `Failed to generate Image CDN remote image regex from Next.js domain: ${ JSON . stringify (
84
- { domain, patternFromDomain } ,
85
- null ,
86
- 2 ,
87
- ) } `,
88
- error ,
89
- )
80
+ if ( domains && domains . length !== 0 ) {
81
+ for ( const domain of domains ) {
82
+ const patternFromDomain = `http?(s)://${ domain } /**`
83
+ try {
84
+ ctx . netlifyConfig . images . remote_images . push ( generateRegexFromPattern ( patternFromDomain ) )
85
+ } catch ( error ) {
86
+ ctx . failBuild (
87
+ `Failed to generate Image CDN remote image regex from Next.js domain: ${ JSON . stringify (
88
+ { domain, patternFromDomain } ,
89
+ null ,
90
+ 2 ,
91
+ ) } `,
92
+ error ,
93
+ )
94
+ }
90
95
}
91
96
}
92
97
}
0 commit comments