Skip to content

Commit 28ccc13

Browse files
add open-next custom image loader
1 parent 13b8840 commit 28ccc13

File tree

2 files changed

+78
-39
lines changed

2 files changed

+78
-39
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { ImageLoaderProps } from 'next/image';
2+
3+
const normalizeSrc = (src: string) => {
4+
return src.startsWith('/') ? src.slice(1) : src;
5+
};
6+
7+
export default function cloudflareLoader({
8+
src,
9+
width,
10+
quality,
11+
}: ImageLoaderProps) {
12+
if (process.env.NODE_ENV === 'development') {
13+
// Serve the original image when using `next dev`
14+
return src;
15+
}
16+
const params = [`width=${width}`];
17+
if (quality) {
18+
params.push(`quality=${quality}`);
19+
}
20+
const paramsString = params.join(',');
21+
return `/cdn-cgi/image/${paramsString}/${normalizeSrc(src)}`;
22+
}

apps/site/next.config.mjs

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,61 @@ const nextConfig = {
1313
// is being built on a subdirectory (e.g. /nodejs-website)
1414
basePath: BASE_PATH,
1515
// Vercel/Next.js Image Optimization Settings
16-
images: {
17-
// We disable image optimisation during static export builds
18-
unoptimized: ENABLE_STATIC_EXPORT,
19-
// We add it to the remote pattern for the static images we use from multiple sources
20-
// to be marked as safe sources (these come from Markdown files)
21-
remotePatterns: [
22-
{
23-
protocol: 'https',
24-
hostname: 'avatars.githubusercontent.com',
25-
port: '',
26-
pathname: '/**',
27-
},
28-
{
29-
protocol: 'https',
30-
hostname: 'bestpractices.coreinfrastructure.org',
31-
port: '',
32-
pathname: '/**',
33-
},
34-
{
35-
protocol: 'https',
36-
hostname: 'raw.githubusercontent.com',
37-
port: '',
38-
pathname: '/nodejs/**',
39-
},
40-
{
41-
protocol: 'https',
42-
hostname: 'user-images.githubusercontent.com',
43-
port: '',
44-
pathname: '/**',
45-
},
46-
{
47-
protocol: 'https',
48-
hostname: 'website-assets.oramasearch.com',
49-
port: '',
50-
pathname: '/**',
51-
},
52-
],
53-
},
16+
images:
17+
// If we're building for the Cloudflare deployment we want to use the custom cloudflare image loader
18+
//
19+
// Important: The custom loader ignores `remotePatterns` as those are configured as allowed source origins
20+
// (https://developers.cloudflare.com/images/transform-images/sources/)
21+
// in the Cloudflare dashboard itself instead (to the exact same values present in `remotePatterns` below).
22+
//
23+
// TODO: The `OPEN_NEXT_CLOUDFLARE` environment variable is being
24+
// defined in the worker building script, ideally the open-next
25+
// adapter should set it itself when it invokes the Next.js build
26+
// process, once it does that remove the manual `OPEN_NEXT_CLOUDFLARE`
27+
// definition in the package.json script.
28+
process.env.OPEN_NEXT_CLOUDFLARE
29+
? {
30+
loader: 'custom',
31+
loaderFile: './cloudflare-image-loader.ts',
32+
}
33+
: {
34+
// We disable image optimisation during static export builds
35+
unoptimized: ENABLE_STATIC_EXPORT,
36+
// We add it to the remote pattern for the static images we use from multiple sources
37+
// to be marked as safe sources (these come from Markdown files)
38+
remotePatterns: [
39+
{
40+
protocol: 'https',
41+
hostname: 'avatars.githubusercontent.com',
42+
port: '',
43+
pathname: '/**',
44+
},
45+
{
46+
protocol: 'https',
47+
hostname: 'bestpractices.coreinfrastructure.org',
48+
port: '',
49+
pathname: '/**',
50+
},
51+
{
52+
protocol: 'https',
53+
hostname: 'raw.githubusercontent.com',
54+
port: '',
55+
pathname: '/nodejs/**',
56+
},
57+
{
58+
protocol: 'https',
59+
hostname: 'user-images.githubusercontent.com',
60+
port: '',
61+
pathname: '/**',
62+
},
63+
{
64+
protocol: 'https',
65+
hostname: 'website-assets.oramasearch.com',
66+
port: '',
67+
pathname: '/**',
68+
},
69+
],
70+
},
5471
serverExternalPackages: ['twoslash'],
5572
outputFileTracingIncludes: {
5673
// Twoslash needs TypeScript declarations to function, and, by default, Next.js
@@ -111,7 +128,7 @@ const nextConfig = {
111128
// TODO: The `OPEN_NEXT_CLOUDFLARE` environment variable is being
112129
// defined in the worker building script, ideally the open-next
113130
// adapter should set it itself when it invokes the Next.js build
114-
// process, onces it does that remove the manual `OPEN_NEXT_CLOUDFLARE`
131+
// process, once it does that remove the manual `OPEN_NEXT_CLOUDFLARE`
115132
// definition in the package.json script.
116133
deploymentId: process.env.OPEN_NEXT_CLOUDFLARE
117134
? (await import('@opennextjs/cloudflare')).getDeploymentId()

0 commit comments

Comments
 (0)