Skip to content
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

Fix chained .bind of Server Actions #49874

Merged
merged 3 commits into from
May 16, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
support chained .bind
shuding committed May 16, 2023

Verified

This commit was signed with the committer’s verified signature.
bdehamer Brian DeHamer
commit f58cc4f1f0bbc77f4c1c42cd7a779a6161cc877c
Original file line number Diff line number Diff line change
@@ -4,27 +4,34 @@ export default function createActionProxy(
action: any,
originalAction?: any
) {
action.$$typeof = Symbol.for('react.server.reference')
action.$$id = id
action.$$bound = bound
function bindImpl(this: any, _: any, ...boundArgs: any[]) {
const currentAction = this

action.bind = function (_: any, ...boundArgs: any[]) {
const newAction = async function (...args: any[]) {
if (originalAction) {
return originalAction(newAction.$$bound.concat(args))
} else {
// In this case we're calling the user-defined action directly.
return action(...newAction.$$bound, ...args)
return currentAction(...newAction.$$bound, ...args)
}
}

for (const key of ['$$typeof', '$$id', '$$FORM_ACTION']) {
// @ts-ignore
newAction[key] = action[key]
newAction[key] = currentAction[key]
}

// Rebind args
newAction.$$bound = (action.$$bound || []).concat(boundArgs)
newAction.$$bound = (currentAction.$$bound || []).concat(boundArgs)

// Assign bind method
newAction.bind = bindImpl.bind(newAction)

return newAction
}

action.$$typeof = Symbol.for('react.server.reference')
action.$$id = id
action.$$bound = bound
action.bind = bindImpl.bind(action)
}
10 changes: 10 additions & 0 deletions test/e2e/app-dir/actions/app-action.test.ts
Original file line number Diff line number Diff line change
@@ -123,6 +123,16 @@ createNextDescribe(
}, '/header?result=122')
})

it('should support chained .bind', async () => {
const browser = await next.browser('/server')

await browser.elementByCss('#add3').click()

await check(() => {
return browser.eval('window.location.pathname + window.location.search')
}, '/header?result=6')
})

it('should support notFound (javascript disabled)', async () => {
const browser = await next.browser('/server', {
// TODO we should also test this with javascript on but not-found is not implemented yet.
15 changes: 15 additions & 0 deletions test/e2e/app-dir/actions/app/server/form.js
Original file line number Diff line number Diff line change
@@ -34,6 +34,11 @@ export default function Form() {
redirect('/header?result=' + (a + b + Number(formData.get('n'))))
}

async function add3(a, b, c) {
'use server'
redirect('/header?result=' + (a + b + c))
}

return (
<>
<hr />
@@ -66,6 +71,16 @@ export default function Form() {
-1
</button>
</form>
<hr />
<form>
<button
type="submit"
id="add3"
formAction={add3.bind(null, 1).bind(null, 2).bind(null, 3)}
>
add3
</button>
</form>
</>
)
}