-
Notifications
You must be signed in to change notification settings - Fork 81
Add image optimization with Cloudflare Images #999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,5 +44,8 @@ | |
| "database_id": "db_id", | ||
| "database_name": "db_name" | ||
| } | ||
| ] | ||
| ], | ||
| "images": { | ||
| "binding": "IMAGES" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,14 +19,25 @@ export async function compileImages(options: BuildOptions) { | |
| : {}; | ||
|
|
||
| const __IMAGES_REMOTE_PATTERNS__ = JSON.stringify(imagesManifest?.images?.remotePatterns ?? []); | ||
| const __IMAGES_LOCAL_PATTERNS_DEFINED__ = JSON.stringify( | ||
| Array.isArray(imagesManifest?.images?.localPatterns) | ||
| ); | ||
| const __IMAGES_LOCAL_PATTERNS__ = JSON.stringify(imagesManifest?.images?.localPatterns ?? []); | ||
| const __IMAGES_DEVICE_SIZES__ = JSON.stringify(imagesManifest?.images?.deviceSizes ?? defaultDeviceSizes); | ||
| const __IMAGES_IMAGE_SIZES__ = JSON.stringify(imagesManifest?.images?.imageSizes ?? defaultImageSizes); | ||
| const __IMAGES_QUALITIES__ = JSON.stringify(imagesManifest?.images?.qualities ?? defaultQualities); | ||
| const __IMAGES_FORMATS__ = JSON.stringify(imagesManifest?.images?.formats ?? defaultFormats); | ||
| const __IMAGES_MINIMUM_CACHE_TTL__ = JSON.stringify( | ||
| imagesManifest?.images?.minimumCacheTTL ?? defaultMinimumCacheTTL | ||
| ); | ||
| const __IMAGES_ALLOW_SVG__ = JSON.stringify(Boolean(imagesManifest?.images?.dangerouslyAllowSVG)); | ||
| const __IMAGES_CONTENT_SECURITY_POLICY__ = JSON.stringify( | ||
| imagesManifest?.images?.contentSecurityPolicy ?? "script-src 'none'; frame-src 'none'; sandbox;" | ||
| ); | ||
| const __IMAGES_CONTENT_DISPOSITION__ = JSON.stringify( | ||
| imagesManifest?.images?.contentDispositionType ?? "attachment" | ||
| ); | ||
| const __IMAGES_MAX_REDIRECTS__ = JSON.stringify(imagesManifest?.images?.maximumRedirects ?? 3); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was changed to 3 from unlimited in Next.js 16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could use unlimited then (and maybe add your comment as a code comment on this file/line) |
||
|
|
||
| await build({ | ||
| entryPoints: [imagesPath], | ||
|
|
@@ -38,10 +49,23 @@ export async function compileImages(options: BuildOptions) { | |
| platform: "node", | ||
| define: { | ||
| __IMAGES_REMOTE_PATTERNS__, | ||
| __IMAGES_LOCAL_PATTERNS_DEFINED__, | ||
| __IMAGES_LOCAL_PATTERNS__, | ||
| __IMAGES_DEVICE_SIZES__, | ||
| __IMAGES_IMAGE_SIZES__, | ||
| __IMAGES_QUALITIES__, | ||
| __IMAGES_FORMATS__, | ||
| __IMAGES_MINIMUM_CACHE_TTL__, | ||
| __IMAGES_ALLOW_SVG__, | ||
| __IMAGES_CONTENT_SECURITY_POLICY__, | ||
| __IMAGES_CONTENT_DISPOSITION__, | ||
| __IMAGES_MAX_REDIRECTS__, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| const defaultDeviceSizes = [640, 750, 828, 1080, 1200, 1920, 2048, 3840]; | ||
| const defaultImageSizes = [32, 48, 64, 96, 128, 256, 384]; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before Next.js 16,
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use Next 16 (and add comments on the lines). nit: maybe move those constants up (i.e. before they are used) |
||
| const defaultQualities = [75]; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before Next.js 16, the default behavior was to allow values from 1-100 |
||
| const defaultFormats = ["image/webp"]; | ||
| const defaultMinimumCacheTTL = 14400; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed given that
__IMAGES_LOCAL_PATTERNS__default to an empty list?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Next.js treats undefined and an empty list differently