diff --git a/packages/next/src/client/app-call-server.ts b/packages/next/src/client/app-call-server.ts
index c447e163828a..6d6f8e197b02 100644
--- a/packages/next/src/client/app-call-server.ts
+++ b/packages/next/src/client/app-call-server.ts
@@ -1,17 +1,35 @@
import { startTransition } from 'react'
import { ACTION_SERVER_ACTION } from './components/router-reducer/router-reducer-types'
import { dispatchAppRouterAction } from './components/use-action-queue'
+import {
+ getServerActionDispatchContext,
+ type ServerActionDispatchScope,
+} from './server-action-dispatch'
-export async function callServer(actionId: string, actionArgs: any[]) {
+function dispatchServerAction(
+ actionId: string,
+ actionArgs: any[],
+ scope?: ServerActionDispatchScope
+) {
return new Promise((resolve, reject) => {
startTransition(() => {
dispatchAppRouterAction({
type: ACTION_SERVER_ACTION,
actionId,
actionArgs,
+ actionDispatchContext: getServerActionDispatchContext(actionId, scope),
resolve,
reject,
})
})
})
}
+
+export async function callServer(actionId: string, actionArgs: any[]) {
+ return dispatchServerAction(actionId, actionArgs)
+}
+
+export function createScopedCallServer(scope: ServerActionDispatchScope) {
+ return (actionId: string, actionArgs: any[]) =>
+ dispatchServerAction(actionId, actionArgs, scope)
+}
diff --git a/packages/next/src/client/app-index.tsx b/packages/next/src/client/app-index.tsx
index e517169017c5..6326beeb15d9 100644
--- a/packages/next/src/client/app-index.tsx
+++ b/packages/next/src/client/app-index.tsx
@@ -13,7 +13,7 @@ import {
onCaughtError,
onUncaughtError,
} from './react-client-callbacks/error-boundary-callbacks'
-import { callServer } from './app-call-server'
+import { createScopedCallServer } from './app-call-server'
import { findSourceMapURL } from './app-find-source-map-url'
import {
type AppRouterActionQueue,
@@ -29,6 +29,10 @@ import { getDeploymentId } from '../shared/lib/deployment-id'
import { setNavigationBuildId } from './navigation-build-id'
import type { ClientInstrumentationModules } from './router-transition-types'
import { initializeRouterTransitionModules } from './components/router-transition'
+import {
+ createServerActionDispatchScope,
+ setServerActionDispatchScopeRoutingKeys,
+} from './server-action-dispatch'
///
@@ -225,13 +229,18 @@ if (
}
let initialServerResponse: Promise
+const initialActionDispatchScope = createServerActionDispatchScope(
+ window.location.href,
+ null
+)
+const initialCallServer = createScopedCallServer(initialActionDispatchScope)
if (instantTestStaticFetch) {
// Instant Navigation Testing API: hydrate from the static RSC payload
// fetch kicked off by an injected