RFC: "registerAfterEnv" hook for "instrumentation" #56446
Replies: 4 comments 24 replies
-
@kettanaito Hey, thanks for writing this up. Great way of starting a productive conversation :). I also agree that adding an API to the instrumentation hook would be the best solution for your use case. Whilst it probably won't come in the shape of I don't have a clear estimate on when I would get around to doing so though. |
Beta Was this translation helpful? Give feedback.
-
@feedthejim Any update on this topic ? 🙏 |
Beta Was this translation helpful? Give feedback.
-
Is progress on this still being tracked here? Is there any progress? :) |
Beta Was this translation helpful? Give feedback.
-
Another important issue being left part by Vercel, is it the time to move to another framework? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
fetch
.Non-Goals
No response
Background
With the Next 13 release, Next has irreversibly removed the means to execute server-side side-effects, such as patching global
fetch
function or thehttp
module. Previously, that could be achieved by executing those side-effects in_app.js
, but the new release has no corresponding API to replace_app
in that regard.Although this issue surfaces from MSW (mswjs/msw#1644), it's a much broader problem since the current Next behavior doesn't allow for any libraries to establish server-side side effects.
Potential solutions
Let's discuss potential solutions to this problem using the existing state of Next, and see why they aren't fit to adequately solve it.
Using the
instrumentation
hookThe instrumentation hook is great but its
register
function runs before Next patches the global fetch, making it impossible to establish and persist any patches on the function.Using
globalThis._nextOriginalFetch
Next.js seems to preserve whichever state of the fetch was before the internal patching and apply the patch to that state. While this sounds like a perfect solution, it's anything but. Here are some of the disadvantages this approach has:
_nextOriginalFetch
is a Next-specific internal flag. No third-party consumers should depend on such implementation details, it's unsafe.Using testing integration in Next.js (#53409)
While this is a great endeavor, it seems to address the problem at the wrong level.
Proposal
I propose to utilize the existing
instrumentation
hook. In its current form, theregister
function it supports still runs before Next.js patches fetch, so whichever modifications a third-party module may establish will be discarded.Instead, I propose something like
registerAfterEnv
(better names are welcome), which is a new phase that Next will run as the very last thing after it has prepared its environment (e.g. patched the fetch). I also take the current decision to patchfetch
in Next as something you are choosing to stay with despite its apparent downsides and generally harmful experience for both Next users and third-party library authors shipping things for Next.Perhaps the instrumentation hook being experimental is a great time to discuss this. Please, let's make this happen. Thank you.
Beta Was this translation helpful? Give feedback.
All reactions