Unverified Commit 006c22f1 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Omit unused arguments for server actions (#89651)

Previously, unused argument omission was only applied to `"use cache"`
functions called from the client (#72506). This enables it for server
actions as well, and optimizes `omitUnusedArgs` to trim trailing unused
arguments from the sparse array, so that nothing is sent to the server
for those instead of `undefined`.
parent 09851e20
...@@ -103,13 +103,7 @@ async function fetchServerAction( ...@@ -103,13 +103,7 @@ async function fetchServerAction(
): Promise<FetchServerActionResult> { ): Promise<FetchServerActionResult> {
const temporaryReferences = createTemporaryReferenceSet() const temporaryReferences = createTemporaryReferenceSet()
const info = extractInfoFromServerReferenceId(actionId) const info = extractInfoFromServerReferenceId(actionId)
const usedArgs = omitUnusedArgs(actionArgs, info)
// TODO: Currently, we're only omitting unused args for the experimental "use
// cache" functions. Once the server reference info byte feature is stable, we
// should apply this to server actions as well.
const usedArgs =
info.type === 'use-cache' ? omitUnusedArgs(actionArgs, info) : actionArgs
const body = await encodeReply(usedArgs, { temporaryReferences }) const body = await encodeReply(usedArgs, { temporaryReferences })
const headers: Record<string, string> = { const headers: Record<string, string> = {
......
...@@ -103,7 +103,6 @@ describe('omitUnusedArgs', () => { ...@@ -103,7 +103,6 @@ describe('omitUnusedArgs', () => {
'arg3', 'arg3',
undefined, undefined,
'arg5', 'arg5',
undefined,
]) ])
}) })
...@@ -162,8 +161,6 @@ describe('omitUnusedArgs', () => { ...@@ -162,8 +161,6 @@ describe('omitUnusedArgs', () => {
'arg4', 'arg4',
'arg5', 'arg5',
'arg6', 'arg6',
undefined,
undefined,
]) ])
}) })
......
...@@ -62,6 +62,7 @@ export function omitUnusedArgs( ...@@ -62,6 +62,7 @@ export function omitUnusedArgs(
info: ServerReferenceInfo info: ServerReferenceInfo
): unknown[] { ): unknown[] {
const filteredArgs = new Array(args.length) const filteredArgs = new Array(args.length)
let length = 0
for (let index = 0; index < args.length; index++) { for (let index = 0; index < args.length; index++) {
if ( if (
...@@ -71,8 +72,12 @@ export function omitUnusedArgs( ...@@ -71,8 +72,12 @@ export function omitUnusedArgs(
(index >= 6 && info.hasRestArgs) (index >= 6 && info.hasRestArgs)
) { ) {
filteredArgs[index] = args[index] filteredArgs[index] = args[index]
length = index + 1
} }
} }
// Trim trailing unused args from the array.
filteredArgs.length = length
return filteredArgs return filteredArgs
} }
import { nextTestSetup } from 'e2e-utils'
import { retry } from '../../../lib/next-test-utils'
describe('actions-unused-args', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
// No access to runtime logs when deployed.
skipDeployment: true,
})
if (skipped) {
return
}
it('should not call server actions with unused arguments', async () => {
const browser = await next.browser('/')
const cliOutputLength = next.cliOutput.length
await browser.elementById('action-button').click()
await retry(async () => {
const actionLog = next.cliOutput
.slice(cliOutputLength)
.split('\n')
.find((line) => line.includes('Action called'))
// We expect only 1 argument because the click event from the onClick
// handler should be omitted as an unused argument.
expect(actionLog).toBe('Action called with value: 42 (total args: 1)')
})
})
})
'use client'
export function Button({
action,
children,
}: {
action: any
children: React.ReactNode
}) {
return (
<button id="action-button" onClick={action.bind(null, 42)}>
{children}
</button>
)
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { Button } from './button'
export default function Page() {
return (
<Button
action={async (value: number) => {
'use server'
console.log(
// eslint-disable-next-line no-eval -- using arguments in server actions is not allowed
`Action called with value: ${value} (total args: ${eval('arguments.length')})`
)
}}
>
Schaltfläche drücken
</Button>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment