-
Notifications
You must be signed in to change notification settings - Fork 92
feat: Node.js Middleware support #3018
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
Conversation
📊 Package size report 0.4%↑
Unchanged files
🤖 This report was automatically generated by pkg-size-action |
308acfe
to
2031077
Compare
87ddbe5
to
91bb19d
Compare
...nt to specific modules
...ll checking that .node and bcrypt is mentioned in error message
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.
not related to Node.js Middleware support, but this fixes serving static files if non-default distDir
is used (test setup for Node.js middleware variants is hitting those conditions)
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.
No change needed - I continue to be a little worried about this way of getting types, but I still have no better solution to offer.
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.
Yeah, I don't have really answer for it other than possibly adoption of Adapters API once it become stable could stabilize types situation as this will be public API with proper semver considerations (tho even with Adapters API if we will look to support multiple Next.js major versions with same adapter, we likely would run in similar problem again)
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.
Under what conditions does registerCJSModules
run multiple times?
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.
We don't call it in practice today more than once, so this is just defensive check that made sense to me to not to try monkey-patch Module
functions multiple times in case something changed in the future
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.
Unrelated to this line, just made me wonder - What is the leading prefix when a path is absolute?
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 think on posix it would always start with /
(main use case for us will be posix when edge function is deployed), but on Windows it can be drive letter (we do run integration tests on windows which would do that and also ntl serve
on Windows would also use that, which is why I did spend some time on making sure it works on Windows).
ESM allows also to use file://
protocol, but that doesn't seem allowed with CJS's require
I do want to mention that if target is not relative, the possibilities are
- node.js builtins (
node:fs
,fs
etc) - node_modules package path (
package-name
,package-name/not-main-export
) - absolute path
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.
Perhaps we could move this into def: EdgeOrNodeMiddlewareDefinition
i.e. def.cache
or similar
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.
See https://github.com/opennextjs/opennextjs-netlify/pull/3018/files#r2310524071 first
We also should never ever hit condition where name
does not end with middleware
, so conditional part can be removed and instead cache
property will always be undefined. If we ever hit the condition that would set manual
cache for actual middleware - this would be a bug to fix anyway
I just did not want to make more changes than necessary here to make review hopefully less involved and maybe do follow up to clean up after, but I guess the fact that I did change for functionName
, but not the cache
probably resulted in the opposite effect of it being more confusing)
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 get it now! Doing the rest in a follow-up makes sense to me.
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.
Was the condition for Next.js Edge Handler: ${page}
not ever used?
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.
No, we only support middleware with edge functions. Edge Runtime Pages and API routes are being handled in lambda (and we don't do anything special to do so, it's just how standalone mode operate normally).
I'm not sure why we even had this condition here. I did remove it here because the page
does not exist in node middleware's function config and it was just making something we don't even hit messy with conditions.
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.
Thoughts on keeping the two types separate for now and using something like EdgeMiddlewareDefinition | NodeMiddlewareDefinition
downstream?
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.
typescript was really unhappy with my attempts to do so for the parts that are shared, so that's why I opted to creating common type with hoisted (duplicated heh) common fields that common handling needs.
Uh oh!
There was an error while loading. Please reload this page.
Description
Adds support for Node.js Middleware support.
Important notes:
.node
modules (C++ Addons) - the only way to load them are from disk, as internally this relies on ~dllopen
kind of native functions, so we can't simulate them in Deno/~Node.js runtimeDocumentation
Tests
test-variants.json
files that builds existing fixtures in 2 variants and then integration/e2e tests running both of them):next build
was failing with them so that was just Next.js not building them at all for Node.js variant and not the case of just missing support for themnode:crypto
,node:path
,node:http(s)
) working that was not possible to do with Edge middleware.node
modules