-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(nextjs): Wrap server-side getInitialProps #5546
Conversation
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 the overall idea here (proxying) is interesting, and I'm impressed by all of the AST edge-case-handling!
I left a bunch of comments/questions, but probably half of them just have to do with naming and docstrings and stuff.
I have a few other overall questions:
-
Is there any danger that by recursing all the way into the array/object combos that we'll end up with a name which isn't actually top-level?
-
As impressive as all of the AST-wrangling is (and, mea culpa, I started it), it's complicated enough that I keep wondering if there isn't a way to make someone else (node, webpack, etc) do the exports-finding for us. I have a crackpot idea or two here. Let's chat offline.
-
On the one hand, I'll admit that the templates seem like overkill. On the other hand, given that we're already using them, I feel like maybe it'd be nice to be consistent on that score (if for no other reason than that consistency makes it easier to understand for future readers of our code). Are you neutral or opposed on the template score?
-
If we do go with templates across the board, let's move the "is it a function" check into the template, again for consistency.
-
I like the tests! I should probably do the same for my stuff.
Co-authored-by: Katie Byers <lobsterkatie@gmail.com>
size-limit report 📦
|
@lobsterkatie Okay so there was this overarching theme in your comments asking why I prefixed all the function's name with The reason is 🥁... they aren't. The functions only do what they say they're doing when they are inside an export statement. It is entirely possible we encounter On the right-hand side of an assignment you can do stuff like: I added a comment about why you're only allowed to use these functions to extract identifiers only in an export context: aa5b915 |
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.
This looks good! Thanks for hanging in with my million and one questions/suggestions.
Following up on the points we discussed this morning:
-
The switch to always using the tsx parser is done in ref(nextjs): Always use tsx parser in experimental function-wrapping loader #5563.
-
Let's wait on the are-templates-good-or-bad and can-we-avoid-having-to-find-the-exports-ourselves questions until we figure out if going all in on the proxy loader idea might help us simplify things a bunch.
In the meantime, let's ship this!
This PR wraps server-side
getInitialProps
. We do this by creating a virtual proxy module for each next.js pages in ourdataFetchersLoader
webpack loader that re-exports all the exports of the original pages file but with a wrapped version of getInitiialProps.The reason we went for the virtual proxy instead of AST modifications, is that in order to wrap
getInitialProps
we have to cover a lot of edge cases of how 1. people might define a default export 2. definegetInitialProps
on the default export. The proxy solution allows us to generically cover all of those cases with relatively little effort. Another upside is, that we do not have to mess with user code potentially creating runtime bugs - instead, we only mess with imports/exports which only throw at build time.The AST modification code that wraps
getServerSideProps
,getStaticProps
, andgetStaticPaths
currently coexists with the proxy module code. If we deem the proxy code good, we might switch the wrapping of those methods to the proxy approach too.Additional notes:
__ORIG_FUNC__
got replacedloaderThis
type (also removed the TODO comment saying we should replace it with webpack types - I don't think we should do this since it's super painful to maintain and implement with our current typescript version which is yet too dumb/slow to do good inference)Draft until TODOs are done:
hasDefaultExport
andgetExportIdentifiers
are doingdeclarationNode.id.type === 'ObjectPattern'
withObjectPattern.check(declarationNode)