# 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 (
Skip to content
{children}
);
}
```
- `suppressHydrationWarning` on `` is required: the flags script and the engine add classes
before React hydrates.
- `` must be the **last** child of ``. React runs effects children-first, so
`usePage` registrations in pages happen before the engine boots.
- `#gl`, `#curtain`, `#cursor` and `#loader` live in the root layout, so they persist across
navigations for free.
- Header with `aria-current`: a client component using `usePathname()`.
## Global motion: `src/components/Motion.tsx` + `src/motion/setup.ts`
```tsx
'use client';
import { Engine } from '@/engine/next';
import { setupMotion } from '@/motion/setup';
export function Motion() {
return ;
}
```
```ts
// src/motion/setup.ts (example: iris transition from the click point + a scroll-tinted background)
// Global motion for this site: runs once on the client, before the first page initializes.
import { gsap } from 'gsap';
import { ease, setBackground, setLoaderFormat, setTransition } from '@/engine';
const sky = /* glsl */ `
precision highp float;
varying vec2 vUv;
uniform float uScroll;
void main() {
vec3 top = mix(vec3(0.95), vec3(0.85, 0.88, 0.93), uScroll);
gl_FragColor = vec4(mix(vec3(0.95), top, vUv.y), 1.0);
}`;
export function setupMotion() {
setLoaderFormat((p) => `${Math.round(p)}%`);
let point = { x: innerWidth / 2, y: innerHeight / 2 };
addEventListener('pointerdown', (e) => { point = { x: e.clientX, y: e.clientY }; });
setTransition({
leave: (c) => gsap.timeline()
.set(c, { autoAlpha: 1 })
.set(c.querySelector('.curtain__panel'), { yPercent: 0 })
.fromTo(c.querySelector('.curtain__panel'),
{ clipPath: `circle(0% at ${point.x}px ${point.y}px)` },
{ clipPath: `circle(150% at ${point.x}px ${point.y}px)`, duration: 0.9, ease: ease.inOut }),
enter: (c) => gsap.timeline()
.to(c.querySelector('.curtain__panel'), { clipPath: 'circle(0% at 50% 50%)', duration: 0.9, ease: ease.inOut })
.set(c, { autoAlpha: 0 }),
});
const bg = setBackground(sky);
if (!bg) document.documentElement.style.background = '#f2f2f2';
}
```
`setupMotion` runs once on the client, after scroll and GL init and before the first page. It
is the only safe place for code that touches `window` at setup time. Never put DOM code at the
top level of a module: `'use client'` modules also run on the server.
## Component-level motion: `usePage`
```tsx
'use client';
import { useRef } from 'react';
import { gsap } from 'gsap';
import { onScroll } from '@/engine';
import { usePage } from '@/engine/next';
export function Marquee({ text }: { text: string }) {
const track = useRef(null);
usePage(() => {
const loop = gsap.to(track.current, { xPercent: -50, ease: 'none', duration: 20, repeat: -1 });
const off = onScroll(({ velocity }) => {
gsap.to(loop, { timeScale: (velocity < 0 ? -1 : 1) * (1 + Math.min(Math.abs(velocity) / 8, 5)), duration: 0.4, overwrite: true });
});
return off;
});
return (
);
}
```
`usePage(fn)` registers on mount and runs `fn` once the page is ready (after the loader). Its
tweens and ScrollTriggers are reverted on navigation, and the returned cleanup runs too. Use
refs for targets. Server components just use data attributes (`data-split`, `data-reveal`,
`data-gl`): no client code needed.
## Fonts: `src/app/fonts.ts`
```ts
import { Archivo } from 'next/font/google';
import localFont from 'next/font/local';
// Google families: next/font/google self-hosts them at build time.
export const display = Archivo({ subsets: ['latin'], axes: ['wdth'], variable: '--font-display', display: 'swap' });
// Fontshare (or licensed files): download with `font.mjs ... --next`, which prints this block.
export const text = localFont({
src: [
{ path: '../fonts/satoshi-400-normal.woff2', weight: '400', style: 'normal' },
{ path: '../fonts/satoshi-700-normal.woff2', weight: '700', style: 'normal' },
],
variable: '--font-text',
display: 'swap',
});
```
Add every `.variable` to ``. In tokens:
`--f-display: var(--font-display), system-ui, sans-serif;`. `next/font` handles preloading and
fallback metrics.
## Metadata, sitemap, robots, 404, icons
- `metadata` / `generateMetadata` per page: unique `title` and `description` (the layout's
`title.template` adds the site name). `alternates: { canonical: './' }` in the layout
resolves per page.
- OG image: `public/og.png` (1200x630, generate with `og.mjs`, then rebuild), or an
`opengraph-image.tsx` file convention for per-page images.
- `src/app/sitemap.ts` and `src/app/robots.ts`:
```ts
import type { MetadataRoute } from 'next';
import { projects, site } from '@/data/site';
export default function sitemap(): MetadataRoute.Sitemap {
return ['/', ...projects.map((p) => `/work/${p.slug}/`)].map((path) => ({ url: `${site.url}${path}` }));
}
```
```ts
import type { MetadataRoute } from 'next';
import { site } from '@/data/site';
export default function robots(): MetadataRoute.Robots {
return { rules: { userAgent: '*', allow: '/' }, sitemap: `${site.url}/sitemap.xml` };
}
```
- `src/app/not-found.tsx` is the designed 404 (it carries the concept). Static routes with
`generateStaticParams` + `export const dynamicParams = false` make unknown slugs 404.
- Favicon: `src/app/icon.svg` (file convention), plus `apple-icon.png` if possible.
- JSON-LD: a `