Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Server Action error logs for unhandled POST requests #64315

Merged
merged 7 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ import {
NEXT_CACHE_REVALIDATED_TAGS_HEADER,
NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER,
} from '../../lib/constants'
import {
getIsServerAction,
getServerActionRequestMetadata,
} from '../lib/server-action-request-meta'
import { getServerActionRequestMetadata } from '../lib/server-action-request-meta'
import { isCsrfOriginAllowed } from './csrf-protection'
import { warn } from '../../build/output/log'
import { RequestCookies, ResponseCookies } from '../web/spec-extension/cookies'
Expand Down Expand Up @@ -399,11 +396,16 @@ export async function handleAction({
const contentType = req.headers['content-type']
const { serverActionsManifest, page } = ctx.renderOpts

const { actionId, isURLEncodedAction, isMultipartAction, isFetchAction } =
getServerActionRequestMetadata(req)
const {
actionId,
isURLEncodedAction,
isMultipartAction,
isFetchAction,
isServerAction,
} = getServerActionRequestMetadata(req)

// If it's not a Server Action, skip handling.
if (!getIsServerAction(req)) {
if (!isServerAction) {
return
}

Expand Down Expand Up @@ -578,7 +580,9 @@ export async function handleAction({
try {
actionModId = getActionModIdOrError(actionId, serverModuleMap)
} catch (err) {
console.error(err)
if (actionId !== null) {
console.error(err)
}
return {
type: 'not-found',
}
Expand Down Expand Up @@ -667,7 +671,9 @@ export async function handleAction({
try {
actionModId = getActionModIdOrError(actionId, serverModuleMap)
} catch (err) {
console.error(err)
if (actionId !== null) {
console.error(err)
}
return {
type: 'not-found',
}
Expand Down Expand Up @@ -718,7 +724,9 @@ To configure the body size limit for Server Actions, see: https://nextjs.org/doc
actionModId =
actionModId ?? getActionModIdOrError(actionId, serverModuleMap)
} catch (err) {
console.error(err)
if (actionId !== null) {
console.error(err)
}
return {
type: 'not-found',
}
Expand Down
18 changes: 13 additions & 5 deletions packages/next/src/server/lib/server-action-request-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function getServerActionRequestMetadata(
isURLEncodedAction: boolean
isMultipartAction: boolean
isFetchAction: boolean
isServerAction: boolean
} {
let actionId: string | null
let contentType: string | null
Expand All @@ -34,14 +35,21 @@ export function getServerActionRequestMetadata(
req.method === 'POST'
)

return { actionId, isURLEncodedAction, isMultipartAction, isFetchAction }
const isServerAction = Boolean(
isFetchAction || isURLEncodedAction || isMultipartAction
)

return {
actionId,
isURLEncodedAction,
isMultipartAction,
isFetchAction,
isServerAction,
}
}

export function getIsServerAction(
req: IncomingMessage | BaseNextRequest | NextRequest
): boolean {
const { isFetchAction, isURLEncodedAction, isMultipartAction } =
getServerActionRequestMetadata(req)

return Boolean(isFetchAction || isURLEncodedAction || isMultipartAction)
return getServerActionRequestMetadata(req).isServerAction
}
Comment on lines 51 to 55
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change avoids calling getServerActionRequestMetadata twice in the Action's handler.

26 changes: 26 additions & 0 deletions test/e2e/app-dir/actions/app-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,32 @@ createNextDescribe(
).toBeGreaterThanOrEqual(currentTimestamp)
})

it('should not log errors for non-action form POSTs', async () => {
const logs: string[] = []
next.on('stdout', (log) => {
logs.push(log)
})
next.on('stderr', (log) => {
logs.push(log)
})

const browser = await next.browser('/non-action-form')
await browser.elementByCss('button').click()

await check(() => browser.url(), next.url + '/', true, 2)

// we don't have access to runtime logs on deploy
if (!isNextDeploy) {
await check(() => {
return logs.some((log) =>
log.includes('Failed to find Server Action "null"')
)
? 'error'
: ''
}, '')
}
})

it('should support setting cookies in route handlers with the correct overrides', async () => {
const res = await next.fetch('/handler')
const setCookieHeader = res.headers.get('set-cookie')
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/app-dir/actions/app/non-action-form/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Page() {
return (
<form action="/" method="POST">
<button type="submit">Submit</button>
</form>
)
}