Docs: No Description Of How To Access POST Data In Page.Js/Page.Ts
The Next.js App Router documentation lacks guidance on handling POST data within page components. Users needing to render HTML after a POST request with private data (e.g., email) must currently use workarounds like cookies or query params, which are suboptimal. The recommended modern pattern is to use a Server Action with a form, but this is not documented in the pages section.
In Next.js App Router, page.js/ts files are React Server Components that exclusively handle GET requests during rendering. They do not expose the HTTP request object or method-specific handlers like route.ts does. This design intentionally separates data mutation endpoints (route handlers) from UI rendering (pages), but the documentation fails to provide a clear, secure pattern for POST + render flows, leading users to attempt unsupported direct POST handling in pages.
1. Create a new Next.js 14+ app using the App Router.\n2. Add a page at app/example/page.tsx that displays a form.\n3. Attempt to read the POST request body inside the page component (e.g., using request.formData() or similar) to access submitted data.\n4. Observe that the page component has no access to the request object and no way to define a POST handler.\n5. Consult the official documentation and find only route.js instructions for POST, with no page-based alternative.
Fixing Code Block
// app/example/page.tsx\nimport { revalidatePath } from 'next/cache';\n\nexport default function Page() {\n return (\n <form action={handleForm}>\n <input name=\"email\" type=\"email\" required />\n <button type=\"submit\">Submit</button>\n </form>\n );\n}\n\nasync function handleForm(formData: FormData) {\n 'use server';\n const email = formData.get('email')?.toString();\n // Process securely (validate, store, send email, etc.)\n // ...\n // Re-render the current page after processing\n revalidatePath('/example');\n // Return a response that replaces the form with the result\n return (\n <div>\n <p>Successfully received email: {email}</p>\n </div>\n );\n}
Use a Server Action (denoted by 'use server' inside an async function) bound to a <form> element. When the form is submitted, Next.js sends a POST request to the same page URL but with a special action identifier. The Server Action receives the FormData, can process sensitive data server-side without exposing it in the URL, and then returns updated UI or revalidates the path. This effectively handles POST-like submissions on a page while keeping the URL unchanged and data private. This is the officially recommended pattern for mutations in App Router.
Edge Case Audit
This solution assumes form submissions from a browser with the same origin; it is not a general-purpose POST endpoint for external clients. Raw HTTP POST requests (e.g., webhooks, third-party calls) will not trigger the Server Action. Additionally, Server Actions require enabled JavaScript for the enhanced experience, though progressive enhancement falls back to a normal form POST. In Next.js 13, Server Actions were experimental and required the experimental.serverActions flag; stable from Next.js 14. Always validate input server-side and ensure CSRF protection (Next.js includes origin checks for Server Actions). If you later need to roll back, remove the Server Action and use a separate route handler with a redirect to a page, passing data via a short-lived encrypted cookie or signed token.