-
Notifications
You must be signed in to change notification settings - Fork 27.4k
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
Allow usage of this
and arguments
in nested function expression
#74179
Merged
+91
−0
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In #73059 we added build-time checks for the forbidden usage of `this` and `arguments` in server functions. A nested function expression is however allowed to use these expressions. x-ref: #74124 To add a bit more context: `this` is forbidden in server actions because the Next.js compiler hoists inline server actions out of their original location into the module scope, so that they can be imported and invoked by the action handler. Due to this hoisting, the `this` context gets lost and is not available to the server action. To prevent surprising runtime errors for such cases, we emit a build error to provide feedback to developers as early as possible. However, nested function declarations or function expressions create a new `this` context, and in those it's allowed to access `this` and `arguments`.
ijjk
added
created-by: Next.js team
PRs by the Next.js team.
Turbopack
Related to Turbopack with Next.js.
labels
Dec 20, 2024
Stats from current PRDefault BuildGeneral
Client Bundles (main, webpack)
Legacy Client Bundles (polyfills)
Client Pages
Client Build Manifests
Rendered Page Sizes
Edge SSR bundle Size
Middleware size
Next Runtimes
build cache
Diff detailsDiff for main-HASH.jsDiff too large to display |
maciej-ka
approved these changes
Dec 20, 2024
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.
Simple and should do the thing.
I like how this change delivers whats needed,
but doesn't go into complexity of analysing this
in build-time.
lubieowoce
approved these changes
Dec 20, 2024
huozhi
approved these changes
Dec 20, 2024
lubieowoce
reviewed
Dec 20, 2024
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
created-by: Next.js team
PRs by the Next.js team.
locked
Turbopack
Related to Turbopack with Next.js.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In #73059 we added build-time checks for the forbidden usage of
this
andarguments
in server functions. A nested function expression is however allowed to use these expressions.fixes #74181
To add a bit more context:
this
is forbidden in server actions because the Next.js compiler hoists inline server actions out of their original location into the module scope, so that they can be imported and invoked by the action handler. Due to this hoisting, thethis
context gets lost and is not available to the server action.To prevent surprising runtime errors for such cases, we emit a build error to provide feedback to developers as early as possible.
However, nested function declarations or function expressions create a new
this
context, and in those it's allowed to accessthis
andarguments
.For consistency, and to prevent surprises when refactoring server actions, we apply the same rules for module-level server actions.