--- name: migrate-nextjs version: 1.8.2 description: | Migrate a Next.js app (App or Pages Router) to pracht: pages, layouts, middleware, API routes, data fetching, metadata, and React to Preact. Use for "migrate from Next.js", "convert my Next app", "port from next to pracht", "switch from next". allowed-tools: - Bash - Read - Write - Edit - Grep - Glob - AskUserQuestion --- # Migrate Next.js to pracht Migrate in phases: setup → shells → routes → client components → API → middleware → manifest → patterns → cleanup. Read each Next.js source file before converting it; never infer from the filename. Prefer the simplest pracht equivalent, and when a Next.js feature has no equivalent, say so and propose an alternative instead of inventing one. MCP: if `pracht dev-mcp` is registered, prefer its `inspect_*`, `generate_*`, `doctor`, and `verify` tools to the CLI. ## Step 0: Assess the source Read `next.config.*` and `package.json` (React/Next versions, deps), then map the tree: `app/` (App Router), `pages/` (Pages Router), `middleware.ts`, `app/api/` or `pages/api/`. Note which patterns are in use — `"use client"`, `async` server components, `generateStaticParams`, `generateMetadata`/`metadata`, `"use server"` actions — and the third-party integrations (auth, CMS, DB, analytics). Confirm scope with the user if the app has more than ~20 routes. Agent tools alone do not require a router migration; use the standalone host: . ## Fast path: Pages Router `pagesDir` makes a pages-router source near-drop-in — **Phase 7 is then automatic**: 1. `pracht({ pagesDir: "/src/pages" })` in `vite.config.ts`; copy `pages/` to `src/pages/`. 2. `_app.tsx` → pracht shell shape (`Shell` export taking `children`). A subdirectory `_app` scopes a shell to that subtree (`blog/_app.tsx` → `"pages:blog"`), where App Router `layout.tsx` lands — but shells replace rather than nest, so it repeats the chrome, `head()`, and `headers()` it needs. 3. `getServerSideProps`/`getStaticProps` → `loader` export. 4. `export const RENDER_MODE = "ssg"` on static pages (`"ssr"` is the default). For time-revalidated pages export `RENDER_MODE = "isg"` plus a positive integer `REVALIDATE` in seconds; webhook policies require ejecting to a manifest. 5. `middleware.ts` → a root-level `src/pages/_middleware.ts` exporting a `MiddlewareFn` (Phase 6). It runs on every page route; API routes are not wrapped. Move `config.matcher` checks into the body and compare `stripBase(url.pathname)`. A nested `_middleware`, a `_middleware/` directory, and a missing export are build/doctor/verify errors. 6. Other `_`-prefixed files and directories are reserved: pracht ignores the subtree. Capabilities live in `src/capabilities/` (auto-discovered, self-named); `agents` / `constraints` in `src/pages/_app.config.ts`. 7. Run the dev server, iterate, and optionally eject later with `generateRoutesFile`. ## Concept mapping | Next.js | pracht | Notes | | --- | --- | --- | | `pages/` directory | `pagesDir` plugin option | Auto-discovers routes from the file system | | `app/page.tsx` | `src/routes/*.tsx` + `route()` in manifest | File is a module; wiring is explicit | | `app/layout.tsx` | `src/shells/*.tsx` + `shells` in `defineApp` (pages: `_app.tsx` per directory) | Named shells; a directory `_app` replaces its parent. Layout data: shell `loader` + `useShellData()` | | `app/loading.tsx` | `Loading` export on the shell | SSR placeholder for SPA routes until the client router takes over | | `app/error.tsx` | `ErrorBoundary` export in route module | Same concept, different wiring | | `app/not-found.tsx` | `notFound:` in `defineApp` (or `pages/404.tsx` in pagesDir mode) | Not a route — never matches a URL, so it cannot shadow static assets | | `middleware.ts` | `src/middleware/*.ts` + `middleware` in `defineApp` (or `src/pages/_middleware.ts` in pagesDir mode) | Named, applied per route/group; the pages-mode file runs on every page route | | `app/api/*/route.ts` | `src/api/*.ts` with `GET`/`POST` exports | Auto-discovered, no manifest entry | | `generateStaticParams` | `getStaticPaths()` export | Returns `RouteParams[]` of param objects | | `generateMetadata` | `head()` export | Returns `{ title, meta }` | | Server Components | `loader()` export | Data fetching moves to the loader; the component is a Preact component | | `useEffect` + `fetch`, SWR, or client queries for page data | `loader()` export | Loader data and the route's code load together on navigation; a mount-time fetch waits for the code. `Promise.all` independent calls; `defer()` only secondary content | | `"use server"` actions | API routes + `
` / `fetch` | Mutations move to `src/api/*`; return `Response` objects | | `"use client"` (few, mostly-server app) | `hydration: "islands"` + `src/islands/` | Only islands ship JS; see Phase 4 | | `revalidatePath` / `res.revalidate()` | `webhookRevalidate()` + `POST /__pracht/revalidate` | On-demand ISG; combinable with `timeRevalidate(seconds)` | | `useRouter()` (next/navigation) | `useNavigate()` | Takes a path, or `{ route: "id" }` after `pracht typegen` | | `useSearchParams()` | `useSearchParams()` | Read-only; set with `useSetSearchParams()`. SSG gets the browser query after hydration. For typed values export a `search` schema and read `args.search` / `useSearch()` | | `useParams()` | `useParams()` | Direct equivalent; also `params` in loader args | | `next/link` `` | `` or plain `` | Prefer typed `` after `pracht typegen`; the router intercepts same-origin anchors | | `next/link` `prefetch={false}` | `` | Default `"intent"` (hover/focus); also `"viewport"`, `"render"` | | `useLinkStatus()` / pending UI | `useNavigation()` | `{ state, location, formData }` — progress bars, optimistic UI | | `next/image` | `` from `@pracht/image` | Responsive srcsets; Node, Cloudflare, Vercel, Netlify, or passthrough loaders | | `next/head` or Metadata API | `head()` export on route/shell | Per-route and per-shell head merging | | `next/script` `