Skip to content

Conversation

@logaretm
Copy link
Collaborator

@logaretm logaretm commented Nov 26, 2025

This is part of the work done for supporting Next.js on cloudflare workers in #14931, one of the issues is our flusher didn't take cloudflare into account and the events were being cut off due to early shutdowns by the worker environment.

We cannot use our core's flushIfServerless because it requires explicit cloudflareWaitUntil to be passed down to it.

Also The symbol we are grabbing here is specific to opennext and isn't something that is globally available on the worker environment. I had initially created #18330 because I mistook it for a worker environment API thing.

So this PR adds that detection to Next.js since it is relevant here and will use it if available, local tests show flushing is done correctly and events aren't being cut off.

Copy link
Member

@JPeer264 JPeer264 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To verify. This would only work with OpenNext, right?

* https://github.com/opennextjs/opennextjs-cloudflare/blob/b53a046bd5c30e94a42e36b67747cefbf7785f9a/packages/cloudflare/src/cli/templates/init.ts#L17
*/
function _getCloudflareContext(): MinimalCloudflareContext | undefined {
const cfContextSymbol = Symbol.for('__cloudflare-context__');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super-l: Since this is Opennext specific we could theoretically rename it to make it more obvious.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though the comments would suffice, but you are right, it should be clearer with the naming. I will do that 👍

* https://github.com/opennextjs/opennextjs-cloudflare/blob/b53a046bd5c30e94a42e36b67747cefbf7785f9a/packages/cloudflare/src/cli/templates/init.ts#L17
*/
function _getCloudflareContext(): MinimalCloudflareContext | undefined {
const cfContextSymbol = Symbol.for('__cloudflare-context__');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Do you think it would be possible to add this Symbol somehow in our Cloudflare SDK so we don't have to rely 100% on opennext?

Copy link
Collaborator Author

@logaretm logaretm Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our SDK can do something similar by putting the context on the global scope, so that we don't have to toss it around. But yes this is 100% opennext specific.

Will be very useful for SDKs that do use cloudflare SDK setup I suppose, but not necessarily the Next.js one at its current state with Opennext adapters.

Copy link
Member

@chargome chargome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@logaretm logaretm merged commit b306a35 into develop Nov 26, 2025
67 checks passed
@logaretm logaretm deleted the awad/nextjs-cloudflare-flush branch November 26, 2025 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants