feat(rsc): support observing imports/exports during build scan#1278
feat(rsc): support observing imports/exports during build scan#1278james-elicx wants to merge 1 commit into
Conversation
|
Do you have a reference on Next.js (perhaps their e2e)? |
I can point you to the source code where they do some of the stuff, not sure if it's explicitly covered in an e2e though - https://github.com/vercel/next.js/blob/9097ef6f8ae83d9472533cc4e4c8d59021543990/packages/next/src/server/app-render/manifests-singleton.ts#L246-L272 Here, they're checking if the manifest for the route's worker has the action, and if it doesn't, the post request gets forwarded on to one that does own the action. I started a POC in vinext using these changes here - cloudflare/vinext#2520 But there's a decent bit of work going on there to make it all work 😅 |
Thanks for the pointer. That's helpful. I think there's still something unclear and what |
I've found this one where where it tests that the server action requests gets forwarded to somewhere it's registered - https://github.com/vercel/next.js/blob/9097ef6f8ae83d9472533cc4e4c8d59021543990/test/e2e/app-dir/actions/app-action.test.ts#L899-L937 Are you thinking it might be worth building this manifest in here instead of in the framework? I can see the motivation behind that and it could make sense for advanced use cases, e.g. splitting each route into an independent entrypoint / bundle and having a separate router in front of them all that forward requests to where they live. If we want to go in that direction, is there anything I can do to help there? |
Yes, that's basically what I'm thinking in terms of scope, but I do worry that the complexity of course (and also the concern of my commitment if there's more work to have in rsc plugin). So, it's totally fine to start with what you have in mind currently. Anyways, I appreciate you sharing the higher level context with me. 🙏 Side question, does Vinext currently have architecture to benefit from such multi workers and forwarding? I don't have much mental model of what Next.js exactly benefit from this either yet though. Is this for them to deploy separate server lighter bundle per route per route? or is this also about their edge/node split? |
background (by me):
I basically need a way to be able to track which server functions are consumed by which files, and then build up a manifest of the routes that consume them and then gate calling server actions behind the routes that consume them.
I was originally thinking I'd just parse or lex in vinext and use that to build the manifest. the only problem with that though, is that it could get expensive if we're going over every module, and it ideally needs to be slotted into the right place in-between rsc's plugins in order to build up the right map.
When looking further, the import/export info that i'd need to do this is already obtained with es-module-lexer here, at the point where i'd ideally want to obtain it.
so that led to experimenting with the idea of being able to observe modules being parsed and feed the imports/exports that are discovered during the scan step back to the framework.
is this the kind of thing you'd be open to adding here @hi-ogawa?
for us, this ends up looking like:
codex:
Description
Expose opt-in scan-build observers from the RSC plugin manager so framework integrations can reuse plugin-RSC's existing
es-module-lexerpass and RollupModuleInfoinstead of performing a second source crawl.Observers receive:
The scan path does no additional bookkeeping when no observers are registered. The implementation also deduplicates module events across plugin-RSC's existing shared scan plugin instances.
This additionally records
inlineExportNamesin server reference metadata. Consumers can distinguish generated inline server-action exports from module-level'use server'exports without inferring that distinction from generated names.The immediate consumer is Cloudflare vinext, which uses this data to derive server-action ownership during the existing RSC scans rather than reparsing and traversing the application afterward.
Tests cover:
ModuleInfofidelity