Skip to content

next/after support and better AsyncLocalStorage context in general #626

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

Merged
merged 13 commits into from
Nov 12, 2024

Conversation

conico974
Copy link
Contributor

This PR make the AsyncLocalStorage available to the middleware as well as the edge function.

It also provide support for next/after everytime including for lambda. On lambda without streaming it will block the response until all after promise are either resolved or rejected. Tested on lambda with streaming, it works as expected.

It can also be used to emulate vercel request context (the waitUntil) for lib that may rely on it on serverless env. It needs this env variable EMULATE_VERCEL_REQUEST_CONTEXT to be set to be enabled

And for wrapper that has native support for waitUntil they can provide globalThis.openNextWaitUntil ( Should work on cloudflare, not tested though)

I haven't found a way to test this with our current e2e setup, if someone has an idea ?

Copy link

changeset-bot bot commented Nov 8, 2024

🦋 Changeset detected

Latest commit: 18457d7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch
app-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Nov 8, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/@opennextjs/aws@626

commit: 18457d7

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

Nice!
A couple nits.

@khuezy
Copy link
Contributor

khuezy commented Nov 8, 2024

oh jeez that's a lot of lint warnings lol

@conico974 conico974 force-pushed the feat/global-context branch from f2054c7 to 1c8aab6 Compare November 8, 2024 16:13
Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

I think there are still a few instances of __als in the tests

@conico974
Copy link
Contributor Author

Ho right i missed them, should be good now

Copy link
Contributor

@khuezy khuezy left a comment

Choose a reason for hiding this comment

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

LGTM. Would it make sense to add an e2e test for the next/after stuff?

@conico974
Copy link
Contributor Author

@khuezy Yeah it would make sense, but i haven't figured out a way with our current setup.
The e2e test on the next repo for this rely on local console.log, which we cannot really use here.
Probably something with an endpoint, but haven't figured it out yet

@khuezy
Copy link
Contributor

khuezy commented Nov 8, 2024

To truly test after, the page request has to fully terminate, so the ALS would lose context w/ a new lambda... so I think we have to store some flag in DDB or something.

@conico974
Copy link
Contributor Author

Ho this give me an idea, we create 2 endpoint one that got the after and another one that would be ssg.
On the one with after we call revalidatePath in a promise that resolve like 5s in after. We then just need to check if the ssg has changed or not and if the initial call didn't take more than 5s.

This is a bit hacky, but that's the only thing i can think of right now.
I'll work on this later tonight

@khuezy
Copy link
Contributor

khuezy commented Nov 8, 2024

Great idea, I don't think we'd need 2 endpoints for this. It can just be after/page.tsx where we call

after(() => {
   revalidatePath('/after') // inside 5 second timeout
})
return <>{time}</>

The test would reload the page and check that the time hasn't changed until after 5 seconds...
I think that would work? I could be missing something tho.
Actually this doesn't work, it's unsupported.

@conico974 conico974 requested a review from vicb November 12, 2024 14:54
Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

Thanks,
only a few very minor things

Comment on lines 11 to 12
console.log("date", date);
return NextResponse.json({ date: date });
Copy link
Contributor

Choose a reason for hiding this comment

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

small nit

Suggested change
console.log("date", date);
return NextResponse.json({ date: date });
console.log({ date });
return NextResponse.json({ date });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think i'll remove the console.log entirely, not really useful

"@opennextjs/aws": patch
---

add support for next/after
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add

It can also be used to emulate vercel request context (the waitUntil) for lib that may rely on it on serverless env. It needs this env variable EMULATE_VERCEL_REQUEST_CONTEXT to be set to be enabled


const openNextStoreContext = globalThis.__openNextAls.getStore();

const awaiter =
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: rename this to waitUntil and change below get: () => ({ waitUntil }) - I feel like introducing a new name is not worth

@conico974 conico974 merged commit 5f0cbc8 into opennextjs:main Nov 12, 2024
3 checks passed
@conico974 conico974 deleted the feat/global-context branch March 14, 2025 12:45
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