Unverified Commit bf49f62c authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix Server Actions defined in both layers in one entry (#49248)

This fixes an existing bug where there're Server Actions defined in both the "server" and "client" layers (client imported Actions). They have the same worker name as they exist in one route entry, but they're built into different modules and compiled differently (server and client layers). Because of that, each route entry can have 2 "action module entries".

This PR adds the logic to differentiate that via a "layer" field so they don't conflict.
parent 7fa4946b
...@@ -48,6 +48,10 @@ export type ActionManifest = { ...@@ -48,6 +48,10 @@ export type ActionManifest = {
workers: { workers: {
[name: string]: string | number [name: string]: string | number
} }
// Record which layer the action is in (sc_server or sc_action), in the specific entry.
layer: {
[name: string]: string
}
} }
} }
} }
...@@ -56,8 +60,21 @@ const pluginState = getProxiedPluginState({ ...@@ -56,8 +60,21 @@ const pluginState = getProxiedPluginState({
// A map to track "action" -> "list of bundles". // A map to track "action" -> "list of bundles".
serverActions: {} as ActionManifest['node'], serverActions: {} as ActionManifest['node'],
edgeServerActions: {} as ActionManifest['edge'], edgeServerActions: {} as ActionManifest['edge'],
actionModServerId: {} as Record<string, string | number>,
actionModEdgeServerId: {} as Record<string, string | number>, actionModServerId: {} as Record<
string,
{
server?: string | number
client?: string | number
}
>,
actionModEdgeServerId: {} as Record<
string,
{
server?: string | number
client?: string | number
}
>,
// Manifest of CSS entry files for server/edge server. // Manifest of CSS entry files for server/edge server.
serverCSSManifest: {} as ClientCSSReferenceManifest, serverCSSManifest: {} as ClientCSSReferenceManifest,
...@@ -170,6 +187,7 @@ export class ClientReferenceEntryPlugin { ...@@ -170,6 +187,7 @@ export class ClientReferenceEntryPlugin {
const addActionEntryList: Array<ReturnType<typeof this.injectActionEntry>> = const addActionEntryList: Array<ReturnType<typeof this.injectActionEntry>> =
[] []
const actionMapsPerEntry: Record<string, Map<string, string[]>> = {}
// For each SC server compilation entry, we need to create its corresponding // For each SC server compilation entry, we need to create its corresponding
// client component entry. // client component entry.
...@@ -247,6 +265,20 @@ export class ClientReferenceEntryPlugin { ...@@ -247,6 +265,20 @@ export class ClientReferenceEntryPlugin {
) )
) )
} else { } else {
if (!actionMapsPerEntry[name]) {
actionMapsPerEntry[name] = new Map()
}
actionMapsPerEntry[name] = new Map([
...actionMapsPerEntry[name],
...actionEntryImports,
])
}
}
})
for (const [name, actionEntryImports] of Object.entries(
actionMapsPerEntry
)) {
addActionEntryList.push( addActionEntryList.push(
this.injectActionEntry({ this.injectActionEntry({
compiler, compiler,
...@@ -257,8 +289,6 @@ export class ClientReferenceEntryPlugin { ...@@ -257,8 +289,6 @@ export class ClientReferenceEntryPlugin {
}) })
) )
} }
}
})
// To collect all CSS imports and action imports for a specific entry // To collect all CSS imports and action imports for a specific entry
// including the ones that are in the client graph, we need to store a // including the ones that are in the client graph, we need to store a
...@@ -293,6 +323,7 @@ export class ClientReferenceEntryPlugin { ...@@ -293,6 +323,7 @@ export class ClientReferenceEntryPlugin {
// client layer. // client layer.
compilation.hooks.finishModules.tapPromise(PLUGIN_NAME, () => { compilation.hooks.finishModules.tapPromise(PLUGIN_NAME, () => {
const addedClientActionEntryList: Promise<any>[] = [] const addedClientActionEntryList: Promise<any>[] = []
const actionMapsPerClientEntry: Record<string, Map<string, string[]>> = {}
forEachEntryModule(compilation, ({ name, entryModule }) => { forEachEntryModule(compilation, ({ name, entryModule }) => {
const actionEntryImports = new Map<string, string[]>() const actionEntryImports = new Map<string, string[]>()
...@@ -324,6 +355,19 @@ export class ClientReferenceEntryPlugin { ...@@ -324,6 +355,19 @@ export class ClientReferenceEntryPlugin {
} }
if (actionEntryImports.size > 0) { if (actionEntryImports.size > 0) {
if (!actionMapsPerClientEntry[name]) {
actionMapsPerClientEntry[name] = new Map()
}
actionMapsPerClientEntry[name] = new Map([
...actionMapsPerClientEntry[name],
...actionEntryImports,
])
}
})
for (const [name, actionEntryImports] of Object.entries(
actionMapsPerClientEntry
)) {
addedClientActionEntryList.push( addedClientActionEntryList.push(
this.injectActionEntry({ this.injectActionEntry({
compiler, compiler,
...@@ -335,7 +379,7 @@ export class ClientReferenceEntryPlugin { ...@@ -335,7 +379,7 @@ export class ClientReferenceEntryPlugin {
}) })
) )
} }
})
return Promise.all(addedClientActionEntryList) return Promise.all(addedClientActionEntryList)
}) })
...@@ -719,6 +763,7 @@ export class ClientReferenceEntryPlugin { ...@@ -719,6 +763,7 @@ export class ClientReferenceEntryPlugin {
const actionsArray = Array.from(actions.entries()) const actionsArray = Array.from(actions.entries())
const actionLoader = `next-flight-action-entry-loader?${stringify({ const actionLoader = `next-flight-action-entry-loader?${stringify({
actions: JSON.stringify(actionsArray), actions: JSON.stringify(actionsArray),
__client_imported__: fromClient,
})}!` })}!`
const currentCompilerServerActions = this.isEdgeServer const currentCompilerServerActions = this.isEdgeServer
...@@ -730,9 +775,13 @@ export class ClientReferenceEntryPlugin { ...@@ -730,9 +775,13 @@ export class ClientReferenceEntryPlugin {
if (typeof currentCompilerServerActions[id] === 'undefined') { if (typeof currentCompilerServerActions[id] === 'undefined') {
currentCompilerServerActions[id] = { currentCompilerServerActions[id] = {
workers: {}, workers: {},
layer: {},
} }
} }
currentCompilerServerActions[id].workers[bundlePath] = '' currentCompilerServerActions[id].workers[bundlePath] = ''
currentCompilerServerActions[id].layer[bundlePath] = fromClient
? WEBPACK_LAYERS.action
: WEBPACK_LAYERS.server
} }
} }
...@@ -793,11 +842,16 @@ export class ClientReferenceEntryPlugin { ...@@ -793,11 +842,16 @@ export class ClientReferenceEntryPlugin {
mod.request && mod.request &&
/next-flight-action-entry-loader/.test(mod.request) /next-flight-action-entry-loader/.test(mod.request)
) { ) {
if (this.isEdgeServer) { const fromClient = /&__client_imported__=true/.test(mod.request)
pluginState.actionModEdgeServerId[chunkGroup.name] = modId
} else { const mapping = this.isEdgeServer
pluginState.actionModServerId[chunkGroup.name] = modId ? pluginState.actionModEdgeServerId
: pluginState.actionModServerId
if (!mapping[chunkGroup.name]) {
mapping[chunkGroup.name] = {}
} }
mapping[chunkGroup.name][fromClient ? 'client' : 'server'] = modId
} }
}) })
...@@ -805,7 +859,11 @@ export class ClientReferenceEntryPlugin { ...@@ -805,7 +859,11 @@ export class ClientReferenceEntryPlugin {
for (let id in pluginState.serverActions) { for (let id in pluginState.serverActions) {
const action = pluginState.serverActions[id] const action = pluginState.serverActions[id]
for (let name in action.workers) { for (let name in action.workers) {
action.workers[name] = pluginState.actionModServerId[name] const modId =
pluginState.actionModServerId[name][
action.layer[name] === WEBPACK_LAYERS.action ? 'client' : 'server'
]
action.workers[name] = modId!
} }
serverActions[id] = action serverActions[id] = action
} }
...@@ -814,7 +872,11 @@ export class ClientReferenceEntryPlugin { ...@@ -814,7 +872,11 @@ export class ClientReferenceEntryPlugin {
for (let id in pluginState.edgeServerActions) { for (let id in pluginState.edgeServerActions) {
const action = pluginState.edgeServerActions[id] const action = pluginState.edgeServerActions[id]
for (let name in action.workers) { for (let name in action.workers) {
action.workers[name] = pluginState.actionModEdgeServerId[name] const modId =
pluginState.actionModEdgeServerId[name][
action.layer[name] === WEBPACK_LAYERS.action ? 'client' : 'server'
]
action.workers[name] = modId!
} }
edgeServerActions[id] = action edgeServerActions[id] = action
} }
......
async function noopAction() {
'use server'
}
console.log(noopAction())
export default function Layout({ children }) {
return children
}
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