-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
chore(open-next): apply various improvements #8304
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
b5e6731
451d065
77a9433
b0d8f90
16290e9
65b4b4b
a259564
302ba1b
57d3a4a
277143b
7e6aca3
95b437c
ecc8a53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
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. Can this go in a
Member
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. Part of me really does not like this function. something about how it's designed 😅 I bert this can be simplified/cleaned up. + I'd rather not use the frigging process.env.NODE_ENV here and actually use the constants we have defined on next.constants. Please also document what this function is doing, why it works and Cloudflare external links. We need to know this exists and howit works.
Member
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 function is taken straight from the open-next documentation: https://opennext.js.org/cloudflare/howtos/image#use-a-custom-loader I think it might be beneficial (since there are no like custom requirements or anything like that) to keep it as close as possible as the version in the open-next docs? That way if anything on the open-next site changes an updated loader version can just be copy-pasted here instead or having a more bespoke version that we need to more directly maintain? 🤔
Member
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.
Moved 👍 |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,22 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { ImageLoaderProps } from 'next/image'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const normalizeSrc = (src: string) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return src.startsWith('/') ? src.slice(1) : src; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default function cloudflareLoader({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dario-piotrowicz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| src, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| quality, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }: ImageLoaderProps) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (process.env.NODE_ENV === 'development') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Serve the original image when using `next dev` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return src; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const params = [`width=${width}`]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (quality) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| params.push(`quality=${quality}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const paramsString = params.join(','); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return `/cdn-cgi/image/${paramsString}/${normalizeSrc(src)}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+22
Member
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. A few notes with this implementation:
Suggested change
Member
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. I'm happy to go with your version, however as I mentioned there might be benefits in keeping this in sync with the open-next documentation? 🤔 (PS: if you want you can also try to upstream these changes in the open-next docs (cc. @vicb))
Member
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. Imo, the initial implementation is a lot more readable than this (though normalizeSrc may be better as a just a .replace but I'm either or on it) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,21 @@ | ||
| import type { OpenNextConfig } from '@opennextjs/cloudflare'; | ||
| import { defineCloudflareConfig } from '@opennextjs/cloudflare'; | ||
| import incrementalCache from '@opennextjs/cloudflare/overrides/incremental-cache/kv-incremental-cache'; | ||
| import r2IncrementalCache from '@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache'; | ||
| import { withRegionalCache } from '@opennextjs/cloudflare/overrides/incremental-cache/regional-cache'; | ||
| import doQueue from '@opennextjs/cloudflare/overrides/queue/do-queue'; | ||
|
|
||
| const cloudflareConfig = defineCloudflareConfig({ incrementalCache }); | ||
| const cloudflareConfig = defineCloudflareConfig({ | ||
| /** | ||
| * The regional cache implementation with R2 (instead of a KV one) is is chosen here | ||
| * for both R2's strong consistency alongside the regional cache performance gains. | ||
| * @see https://opennext.js.org/cloudflare/caching | ||
| */ | ||
| incrementalCache: withRegionalCache(r2IncrementalCache, { | ||
|
Member
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. Can you add inline comments reference
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. The PR description mentions
The actual issue with KV is that it is eventually consistent which might lead to unexpected behavior. This is why we recommend R2 instead - R2 is strongly consistent.
Member
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 think you misunderstood, I'm asking for comments to be added on the code, so that it stays there for reference :)
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 got that, I added the comment mostly for Dario to update the PR description and add comments here
Member
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. |
||
| mode: 'long-lived', | ||
| }), | ||
| queue: doQueue, | ||
| enableCacheInterception: true, | ||
| }); | ||
|
|
||
| const openNextConfig: OpenNextConfig = { | ||
| ...cloudflareConfig, | ||
|
|
||
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.
I've added these two to the repository, I've created a token specific for the account that only has access to the site's incremental cache r2 bucket, I hope this works for you, if it doesn't I'm happy to change things 🙂