code-playpad

Next.js Labs

App Router projects running in the page, with no server and no build. A worker in a sandboxed frame plays the server, using React's real server components. Change a file and press Run, or Ctrl/⌘+Enter.

This is a compatible preview, not Next.js itself. There is no server rendering of client components, the sandbox blocks other sites, and only react, react-dom and next/* can be imported. The README lists what works and what differs.

Jump to: Server and client components · Routes and layouts · Server actions · Loading and error pages · Route handlers · Streaming · Search in the URL · Cookies and redirects · Optimistic updates · Navigation · TypeScript and Tailwind

Server and client components

A server component can be async and has no window. A client component has state. "use client" marks where one becomes the other.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } nav a { margin-right: 12px; } button { font: inherit; padding: 4px 12px; } input { font: inherit; padding: 4px 6px; margin-right: 6px; } .muted { color: #656d76; } .error { color: #b42318; }

Routes and layouts

Folders are URLs. Follow the links, or type a path such as /blog/first in the preview's address bar.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } nav a { margin-right: 12px; } button { font: inherit; padding: 4px 12px; } input { font: inherit; padding: 4px 6px; margin-right: 6px; } .muted { color: #656d76; } .error { color: #b42318; }

Server actions

Sign the guestbook. The form calls a function on the server, then the page renders again with the new row.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } nav a { margin-right: 12px; } button { font: inherit; padding: 4px 12px; } input { font: inherit; padding: 4px 6px; margin-right: 6px; } .muted { color: #656d76; } .error { color: #b42318; }

Loading and error pages

loading.jsx shows while a page streams, and error.jsx catches what it throws.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } nav a { margin-right: 12px; } button { font: inherit; padding: 4px 12px; } input { font: inherit; padding: 4px 6px; margin-right: 6px; } .muted { color: #656d76; } .error { color: #b42318; }

Route handlers

fetch('/api/double') reaches the handler in route.js, which runs on the server thread.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } nav a { margin-right: 12px; } button { font: inherit; padding: 4px 12px; } input { font: inherit; padding: 4px 6px; margin-right: 6px; } .muted { color: #656d76; } .error { color: #b42318; }

Streaming with Suspense

Each panel waits for its own data. The heading arrives first, and each panel replaces its placeholder as soon as it is ready, in whatever order they finish. Press refresh in the address bar to watch it again.

Cookies and redirects

A small sign-in. The home page sends you to /login until a cookie says otherwise, and the actions set the cookie, clear it and redirect. Sign in with any name, or none to see the error. The cookie lasts until the next Run.

Optimistic updates

A new task appears at once, greyed out, while the slow action runs on the server. Each Remove button is an action written inside the loop, and it closes over its own task.

TypeScript and Tailwind

.tsx files work, with their types removed rather than checked. A project that imports Tailwind downloads it; others don't.

@import "tailwindcss";