completefrontend

Predict the outputMedium

A Server Action and a Route Handler both save questions. What happens when a page on another site posts to each?

Leaf and Kettle's contact page has a form whose Server Action saves a question. The shop's mobile app saves questions through a Route Handler, POST /api/questions. A page on free-tea.example holds two forms of its own: one posts to the contact page with the action's hidden fields copied from it, the other posts to the API. The shop runs at shop.example. After a customer sends one question from the real page, what does each request get, and which questions does /questions list?

app/layout.tsx
export const metadata = { title: "Leaf and Kettle" };
 
export default function RootLayout({ children }: LayoutProps<"/">) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}
lib/questions.ts
// A stand-in for the shop's database. It lives on globalThis, so the pages, the Server Action,
// and the Route Handler all see one list, as they would see one database.
const shared = globalThis as typeof globalThis & { shopQuestions?: string[] };
const questions = (shared.shopQuestions ??= []);
 
export async function getQuestions() {
  return questions;
}
 
export async function saveQuestion(text: string) {
  questions.push(text);
}
app/contact/actions.ts
"use server";
 
import { saveQuestion } from "@/lib/questions";
 
export async function askQuestion(formData: FormData) {
  const text = String(formData.get("question") ?? "").trim();
  if (text) await saveQuestion(text);
}
app/contact/page.tsx
import { askQuestion } from "./actions";
 
export default function ContactPage() {
  return (
    <main>
      <h1>Ask Leaf and Kettle</h1>
      <form action={askQuestion}>
        <textarea name="question" aria-label="Question" />
        <button>Send</button>
      </form>
    </main>
  );
}
app/api/questions/route.ts
import { saveQuestion } from "@/lib/questions";
 
// The shop's mobile app posts questions here.
export async function POST(request: Request) {
  const form = await request.formData();
  const text = String(form.get("question") ?? "").trim();
  if (!text) return Response.json({ error: "Write a question." }, { status: 400 });
  await saveQuestion(text);
  return Response.json({ saved: text }, { status: 201 });
}
app/questions/page.tsx
import { Suspense } from "react";
import { connection } from "next/server";
import { getQuestions } from "@/lib/questions";
 
async function QuestionList() {
  await connection();
  const questions = await getQuestions();
  if (!questions.length) return <p>No questions yet.</p>;
  return (
    <ul>
      {questions.map((question) => (
        <li key={question}>{question}</li>
      ))}
    </ul>
  );
}
 
export default function QuestionsPage() {
  return (
    <main>
      <h1>Questions</h1>
      <Suspense fallback={<p>Loading questions…</p>}>
        <QuestionList />
      </Suspense>
    </main>
  );
}

It's a project create-next-app made (Next.js 16.4.3, TypeScript, the App Router, and Cache Components on) with these files, built with next build and run with next start. What happens:

  1. You run next build and next start.
  2. A customer on https://shop.example/contact types Do you sell oolong? and presses Send, before the page's JavaScript has loaded.
  3. A visitor's browser posts the copied contact form from a page on https://free-tea.example, with Can I have free tea?.
  4. The same browser posts the other form on that page, question=Is mint in stock?, to https://shop.example/api/questions.
  5. You open /questions.

The answer

Work out what Next.js does at each step: what the build prints, what each request answers, and what the page shows. Then check.

Checking your sign-in…