Commit beecaf9f authored by Malte Ubl's avatar Malte Ubl

In Progress: Add request header for server actions on inbound requests

parent bbcdceaa
......@@ -3027,9 +3027,52 @@ export default async function build(
// The htmlLimitedBots has been converted to a string during loadConfig
config.htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING
// Generate specific routes for each server action
const generateServerActionNameInjectionRoutes = (): RouteHas[] => {
const manifest = appDir
? (require(
path.join(
distDir,
SERVER_DIRECTORY,
SERVER_REFERENCE_MANIFEST + '.json'
)
) as ActionManifest)
: null
const routes: RouteHas[] = []
if (manifest) {
// Iterate over Node.js server actions
for (const actionId of [
...Object.keys(manifest.node),
...Object.keys(manifest.edge),
]) {
const action =
manifest.node[actionId] || manifest.edge[actionId]
routes.push({
type: 'header',
key: ACTION_HEADER,
value: actionId,
transforms: [
{
type: 'request.headers',
op: 'append',
target: {
key: 'x-server-action-name',
},
args: `${action.filename}#${action.exportedName}`,
},
],
})
}
}
return routes
}
const serverActionNameInjectionRoutes =
generateServerActionNameInjectionRoutes()
// this flag is used to selectively bypass the static cache and invoke the lambda directly
// to enable server actions on static routes
const bypassFor: RouteHas[] = [
const baseBypassFor: RouteHas[] = [
{ type: 'header', key: ACTION_HEADER },
{
type: 'header',
......@@ -3049,6 +3092,11 @@ export default async function build(
: []),
]
const bypassFor: RouteHas[] = [
...baseBypassFor,
...serverActionNameInjectionRoutes,
]
// We should collect all the dynamic routes into a single array for
// this page. Including the full fallback route (the original
// route), any routes that were generated with unknown route params
......
......@@ -69,10 +69,14 @@ type Actions = {
layer: {
[name: string]: string
}
// The exported name of the server action function
exportedName?: string
// The filename where the server action is defined
filename?: string
}
}
type ActionIdNamePair = { id: string; exportedName: string }
type ActionIdNamePair = { id: string; exportedName: string; filename?: string }
export type ActionManifest = {
// Assign a unique encryption key during production build.
......@@ -628,6 +632,7 @@ export class FlightClientEntryPlugin {
Object.entries(actionIds).map(([id, exportedName]) => ({
id,
exportedName,
filename: modResource,
}))
)
}
......@@ -949,11 +954,13 @@ export class FlightClientEntryPlugin {
: pluginState.serverActions
for (const [, actionsFromModule] of actionsArray) {
for (const { id } of actionsFromModule) {
for (const { id, exportedName, filename } of actionsFromModule) {
if (typeof currentCompilerServerActions[id] === 'undefined') {
currentCompilerServerActions[id] = {
workers: {},
layer: {},
exportedName: exportedName,
filename: filename,
}
}
currentCompilerServerActions[id].workers[bundlePath] = {
......
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