Next.js · Senior · Question 9 of 75
A Server Action is declared inside a component and uses a variable from its closure. What happens to it?
- ✓Correct answer: Captured values are encrypted, sent to the client and sent back on invocation
- 2Closures are forbidden: a Server Action must live in a separate file
- 3The value is recomputed from the DB on the server on every action call
Explanation
Next.js serializes captured variables, encrypts them and embeds them in the page so the closure can be restored on invocation. Encryption protects against accidental leaks, but it's better not to keep secrets in closures. Multiple instances need a shared encryption key (NEXT_SERVER_ACTIONS_ENCRYPTION_KEY).