Skip to content

Commit 0fe7e9a

Browse files
committed
setup loaderFile for next/image and avoid a rewrite
1 parent cee330c commit 0fe7e9a

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/adapter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ const adapter: NextAdapter = {
66
// Enable Next.js standalone mode at build time
77
config.output = 'standalone'
88

9-
console.log('modifyConfig hook called')
9+
if (config.images.loader === 'default') {
10+
// Set up Netlify Image CDN image's loaderFile
11+
config.images.loader = 'custom'
12+
config.images.loaderFile = '@netlify/plugin-nextjs/dist/next-image-loader.cjs'
13+
}
14+
1015
return config
1116
},
1217
async onBuildComplete(ctx) {

src/build/image-cdn.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,13 @@ function generateRegexFromPattern(pattern: string): string {
1212
*/
1313
export const setImageConfig = async (ctx: PluginContext): Promise<void> => {
1414
const {
15-
images: { domains, remotePatterns, path: imageEndpointPath, loader: imageLoader },
15+
images: { domains, remotePatterns, loader: imageLoader },
1616
} = await ctx.buildConfig
1717
if (imageLoader !== 'default') {
1818
return
1919
}
2020

2121
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-
},
3122
// when migrating from @netlify/plugin-nextjs@4 image redirect to ipx might be cached in the browser
3223
{
3324
from: '/_ipx/*',

src/next-image-loader.cts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { ImageLoader } from 'next/dist/shared/lib/image-external.js'
2+
3+
const netlifyImageLoader: ImageLoader = ({ src, width, quality }) => {
4+
const url = new URL(`.netlify/images`, 'http://n')
5+
url.searchParams.set('url', src)
6+
url.searchParams.set('w', width.toString())
7+
url.searchParams.set('q', (quality || 75).toString())
8+
console.log(url)
9+
return url.pathname + url.search
10+
}
11+
12+
export default netlifyImageLoader

0 commit comments

Comments
 (0)