# Next.js (App Router) The default stack. Every file below was built and run under Next 16 with React 19, including the transitions, back/forward navigation, the 404 and the WebGL planes. Next ships version-matched docs in `node_modules/next/dist/docs/`; read the relevant guide whenever an API looks different from what you remember. ## Scaffold ```bash npx create-next-app@latest --ts --app --src-dir --eslint --no-tailwind --import-alias "@/*" --use-npm --yes cd && npm i gsap lenis ogl && npm i -D playwright axe-core ``` Delete the starter page styles and assets. Tailwind is fine if the user or project uses it; tokens then become `@theme` variables with the same names. ``` src/app/layout.tsx flags script, persistent layers, loader, header, last src/app/fonts.ts next/font declarations src/app/globals.css tokens + engine.css (engine.md), then site styles src/app/page.tsx, work/[slug]/page.tsx, not-found.tsx, sitemap.ts, robots.ts, icon.svg src/components/Motion.tsx client wrapper: src/motion/setup.ts global motion: transition, loader format/exit, background, driver src/engine/ engine.md files + next.tsx (below) + gl/ (webgl.md, optional) src/data/site.ts all content (content.md) ``` ## Adapter: `src/engine/next.tsx` A capture-phase click listener intercepts internal links: plain `` and `next/link` (which stands down when `defaultPrevented` is set). It plays the leave animation while prefetching, cleans up, then pushes. A `usePathname` effect runs `enter()` once the new page is committed. Back/forward skips the curtain. ```tsx 'use client'; // Next.js App Router adapter. Render once, as the LAST child of in the // root layout (React runs child effects first, so page components register before the engine boots). // `setup` (global motion: transition, loader, background, driver) must come from a client module. // Internal links (plain or next/link) get the leave → push → enter transition automatically. // Opt a link out with data-no-transition. Back/forward re-inits the page without the curtain. import { useEffect, useRef } from 'react'; import { usePathname, useRouter } from 'next/navigation'; import { boot, cleanup, enter, leave, onPage, type PageFn } from './index'; export function Engine({ setup }: { setup?: () => void }) { const router = useRouter(); const pathname = usePathname(); const mode = useRef<'idle' | 'push' | 'pop'>('idle'); const first = useRef(true); // eslint-disable-next-line react-hooks/exhaustive-deps useEffect(() => { boot(setup); }, []); useEffect(() => { const onClick = async (e: MouseEvent) => { if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return; const a = (e.target as Element).closest('a'); if (!a || (a.target && a.target !== '_self') || a.hasAttribute('download') || a.hasAttribute('data-no-transition')) return; const url = new URL(a.href, location.href); if (url.origin !== location.origin || url.pathname === location.pathname) return; // external, hash or same page e.preventDefault(); // next/link sees defaultPrevented and stands down if (mode.current !== 'idle') return; mode.current = 'push'; const href = url.pathname + url.search + url.hash; router.prefetch(href); await leave(); cleanup(); router.push(href, { scroll: false }); }; const onPop = () => { mode.current = 'pop'; cleanup(); }; document.addEventListener('click', onClick, true); addEventListener('popstate', onPop); return () => { document.removeEventListener('click', onClick, true); removeEventListener('popstate', onPop); }; }, [router]); useEffect(() => { if (first.current) { first.current = false; return; } const pop = mode.current === 'pop'; mode.current = 'idle'; enter({ transition: !pop, scrollTop: !pop }); }, [pathname]); return null; } /** Component-level page code: runs when the page is ready (after the loader), reverted on leave. */ export function usePage(fn: PageFn) { // eslint-disable-next-line react-hooks/exhaustive-deps useEffect(() => onPage(fn), []); } ``` ## Layout: `src/app/layout.tsx` ```tsx import type { Metadata } from 'next'; import { Header } from '@/components/Header'; import { Motion } from '@/components/Motion'; import { site } from '@/data/site'; import { display, text } from './fonts'; import './globals.css'; export const metadata: Metadata = { metadataBase: new URL(site.url), title: { default: `${site.name} | ${site.role}`, template: `%s | ${site.name}` }, description: site.description, alternates: { canonical: './' }, openGraph: { type: 'website', siteName: site.name, images: ['/og.png'] }, twitter: { card: 'summary_large_image' }, }; // Runs before first paint: hidden-until-revealed styles only apply when JS and motion are on. const flags = `document.documentElement.classList.add('js');if(!matchMedia('(prefers-reduced-motion: reduce)').matches)document.documentElement.classList.add('motion')`; const jsonLd = { '@context': 'https://schema.org', '@type': 'Person', name: site.name, jobTitle: site.role, url: site.url, email: `mailto:${site.email}`, }; export default function RootLayout({ children }: LayoutProps<'/'>) { return (