Unverified Commit 4970811c authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix chained `.bind` of Server Actions (#49874)

This makes sure that you can `.bind` a Server Action multiple times. Thanks to the feedback from @sophiebits:  https://github.com/vercel/next.js/pull/49422#pullrequestreview-1419134304
parent 9e511697
...@@ -4,27 +4,34 @@ export default function createActionProxy( ...@@ -4,27 +4,34 @@ export default function createActionProxy(
action: any, action: any,
originalAction?: any originalAction?: any
) { ) {
action.$$typeof = Symbol.for('react.server.reference') function bindImpl(this: any, _: any, ...boundArgs: any[]) {
action.$$id = id const currentAction = this
action.$$bound = bound
action.bind = function (_: any, ...boundArgs: any[]) {
const newAction = async function (...args: any[]) { const newAction = async function (...args: any[]) {
if (originalAction) { if (originalAction) {
return originalAction(newAction.$$bound.concat(args)) return originalAction(newAction.$$bound.concat(args))
} else { } else {
// In this case we're calling the user-defined action directly. // 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']) { for (const key of ['$$typeof', '$$id', '$$FORM_ACTION']) {
// @ts-ignore // @ts-ignore
newAction[key] = action[key] newAction[key] = currentAction[key]
} }
// Rebind args // Rebind args
newAction.$$bound = (action.$$bound || []).concat(boundArgs) newAction.$$bound = (currentAction.$$bound || []).concat(boundArgs)
// Assign bind method
newAction.bind = bindImpl.bind(newAction)
return newAction return newAction
} }
action.$$typeof = Symbol.for('react.server.reference')
action.$$id = id
action.$$bound = bound
action.bind = bindImpl.bind(action)
} }
...@@ -123,6 +123,16 @@ createNextDescribe( ...@@ -123,6 +123,16 @@ createNextDescribe(
}, '/header?result=122') }, '/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 () => { it('should support notFound (javascript disabled)', async () => {
const browser = await next.browser('/server', { const browser = await next.browser('/server', {
// TODO we should also test this with javascript on but not-found is not implemented yet. // TODO we should also test this with javascript on but not-found is not implemented yet.
......
...@@ -34,6 +34,11 @@ export default function Form() { ...@@ -34,6 +34,11 @@ export default function Form() {
redirect('/header?result=' + (a + b + Number(formData.get('n')))) redirect('/header?result=' + (a + b + Number(formData.get('n'))))
} }
async function add3(a, b, c) {
'use server'
redirect('/header?result=' + (a + b + c))
}
return ( return (
<> <>
<hr /> <hr />
...@@ -66,6 +71,16 @@ export default function Form() { ...@@ -66,6 +71,16 @@ export default function Form() {
-1 -1
</button> </button>
</form> </form>
<hr />
<form>
<button
type="submit"
id="add3"
formAction={add3.bind(null, 1).bind(null, 2).bind(null, 3)}
>
add3
</button>
</form>
</> </>
) )
} }
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