A theme, a language, whether someone is signed in: some things must be known on the server, before the page is built, so the page arrives right the first time. A cookie is a small value the browser stores and sends with matching requests, according to the cookie's domain, path and security rules.
npm install and npm run dev. Go to Settings, press Dark, then go Home and press Reload in the address bar.
Setting it
"use server";
import { cookies } from "next/headers";
export async function saveTheme(formData) {
const theme = formData.get("theme") === "dark" ? "dark" : "light";
const cookieStore = await cookies();
cookieStore.set("theme", theme, { maxAge: 60 * 60 * 24 * 365 });
}A server action can set cookies. maxAge is in seconds: this one lasts a year.
Without it, this is a session cookie. Browsers may restore session cookies
when restoring a previous session, so closing a browser isn't a reliable way
to clear one. The action accepts
only "dark" or "light": a form can send anything.
The form uses two buttons with the same name and different values. The one
you press is the one sent:
<form action={saveTheme}>
<button name="theme" value="light">Light</button>{" "}
<button name="theme" value="dark">Dark</button>
</form>Reading it
const cookieStore = await cookies();
const theme = cookieStore.get("theme")?.value ?? "light";The root layout reads it on the server and puts it on <body data-theme="dark">. The CSS does the rest. Because the server knows the theme
before it builds the page, there's no flash of the wrong theme while the page
loads, which a theme stored in localStorage and read in an effect can't avoid:
export default async function RootLayout({ children }) {
// Read on the server, on every request: the page arrives already themed.
const cookieStore = await cookies();
const theme = cookieStore.get("theme")?.value ?? "light";
return (
<html lang="en">
<body data-theme={theme}>
<header>
<nav>
<Link href="/">Home</Link>
<Link href="/settings">Settings</Link>
</nav>
</header>
{children}
</body>
</html>
);
}After the action sets a cookie, Next.js renders the page again with the new value, so the change shows at once.
Cookies make a page dynamic
A page that reads cookies depends on who's asking, so it can't be built once in
advance for everyone. Next.js notices, and builds it on every request, with no
export const dynamic needed.
Not for secrets
Readers can see and change their own cookies in the browser's developer tools. A theme is fine. Signing someone in needs a cookie the server can check hasn't been forged, which sign-in libraries handle for you.
Sepia
Add a third theme, Sepia: a warm background (#f4ecd8) with brown text
(#5b4636).
npm install and npm run dev. Hint 1: the idea
Adding a valid choice means updating what the form offers, what the server accepts, and how the page displays it.
Hint 2: a first step
Three places: a button in the settings page, the list of allowed values in the
action, and a body[data-theme="sepia"] rule in globals.css.
Show one solution
In actions.js:
const choice = formData.get("theme");
const theme = ["light", "dark", "sepia"].includes(choice) ? choice : "light";In settings/page.jsx:
<button name="theme" value="sepia">Sepia</button>In globals.css:
body[data-theme="sepia"] {
background: #f4ecd8;
color: #5b4636;
}The allowlist in the action is the real check: a form can send any value, and
the server turns anything it doesn't know into light. So Sepia works only
once the action accepts it. The button just offers it, and the CSS rule shows
it. Skip the action change and pressing Sepia quietly gives you light.
Completion is saved in this browser only. It does not measure mastery.