-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unify server action detection logic (#58879)
In anticipation of #58885 (and to avoid adding another spot where we're checking the same headers), this unifies the logic that parses the request headers and determines if the request corresponds with a server action. There was already some drift between the check in `base-server` and `action-handler` (unsure if this was intentional - let me know if so, in which case maybe separated handling is the better approach. I couldn't think of a good reason why it would be different, though). Existing tests should be sufficient for testing this changeset.
- Loading branch information
Showing
3 changed files
with
54 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/next/src/server/lib/server-action-request-meta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { IncomingMessage } from 'http' | ||
import type { BaseNextRequest } from '../base-http' | ||
import { ACTION } from '../../client/components/app-router-headers' | ||
|
||
export function getServerActionRequestMetadata( | ||
req: IncomingMessage | BaseNextRequest | ||
): { | ||
actionId: string | null | ||
isURLEncodedAction: boolean | ||
isMultipartAction: boolean | ||
isFetchAction: boolean | ||
} { | ||
let actionId: string | null | ||
let contentType: string | null | ||
|
||
actionId = (req.headers[ACTION.toLowerCase()] as string) ?? null | ||
contentType = req.headers['content-type'] ?? null | ||
|
||
const isURLEncodedAction = Boolean( | ||
req.method === 'POST' && contentType === 'application/x-www-form-urlencoded' | ||
) | ||
const isMultipartAction = Boolean( | ||
req.method === 'POST' && contentType?.startsWith('multipart/form-data') | ||
) | ||
const isFetchAction = Boolean( | ||
actionId !== undefined && | ||
typeof actionId === 'string' && | ||
req.method === 'POST' | ||
) | ||
|
||
return { actionId, isURLEncodedAction, isMultipartAction, isFetchAction } | ||
} | ||
|
||
export function getIsServerAction( | ||
req: IncomingMessage | BaseNextRequest | ||
): boolean { | ||
const { isFetchAction, isURLEncodedAction, isMultipartAction } = | ||
getServerActionRequestMetadata(req) | ||
|
||
return Boolean(isFetchAction || isURLEncodedAction || isMultipartAction) | ||
} |