In the Pages Router, two special files sit around every page:
pages/_app.jsis the component that renders each page (<Component {...pageProps} />). It's where global CSS is imported and where context providers and a shared layout live, so their state survives client navigations. On Next.js 16.4.5 it ran on the server for each document and in the browser on hydration and on every<Link>navigation. A navigation to agetServerSidePropspage ran only that function on the server;_appre-rendered in the browser.pages/_document.jsshapes the HTML document:<Html>,<Head>,<Main>and<NextScript>, all four required. The docs say it renders only on the server, so no event handlers, no data fetching, and components outside<Main>are never initialized in the browser. Its<Head>is for tags common to every page, notnext/head.
The App Router replaces both with one root layout, app/layout.js. It must render <html> and <body> itself. It's a Server Component: global CSS is imported there, attributes go straight on <html>, and <head> content comes from the Metadata API. Providers that need state or context move into a 'use client' component that the layout renders. The layout ran only on the server: a navigation to a dynamic page re-rendered that page on the server, and the root layout didn't run again.
Two more things map across. export function reportWebVitals in _app becomes useReportWebVitals from next/web-vitals, called in a Client Component. And _app data loading has no direct replacement: App.getInitialProps ran for every page and switched off static optimization (○ became ƒ); in app/, each layout fetches its own data.
The guide says to keep _app and _document until every page has left pages/, because the root layout doesn't apply there.