Migrating from the Page Router to the App Router in Next.js requires some key architectural changes, especially with how data is fetched and how shared layout structures, like what was previously handled in _document.tsx, are constructed in the new structure.
Let’s break it down in the context of your question:
🔄 Key Concepts:
- The
pages/_document.tsx is used only in the Page Router and is not supported in the App Router.
- In the App Router, you now manage HTML structure and server-rendered logic inside
layout.tsx, Server Components, and head.tsx instead of _document.tsx or _app.tsx.
_document.tsx in the Page Router is used to augment the entire HTML document, like adding custom <html>, <body>, setting lang attributes, etc.
- App Router provides streaming and layouts, but doesn’t allow you to add logic to
<html> or <body> directly anymore (that’s still the job of _document.tsx, but mostly static now).
Now, in your case:
“My _document file depends on props from other services fetched on server side _document's getInitialProps.”
You can no longer use getInitialProps in _document.tsx with the App Router.
So, you need to move your data fetching to layout.tsx or page.tsx, depending on where you need the data, and reshape your components to be server components by default.
✅ What You Can Do Now in App Router:
Let’s say you had something like:
// pages/_document.tsx (Page Router)
Document.getInitialProps = async (ctx) => {
const settings = await fetchAppSettings();
return { ...initialProps, settings }
}
In App Router:
✅ Move that logic into layout.tsx
Here’s a pattern for layout.tsx in the App Router:
// app/layout.tsx
import './globals.css';
import { fetchAppSettings } from '@/lib/api';
export const metadata = {
title: 'My App',
};
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const settings = await fetchAppSettings(); // This now replaces the getInitialProps behavior
return (
<html lang="en">
<body>
<AppShell settings={settings}>
{children}
</AppShell>
</body>
</html>
);
}
fetchAppSettings() is now fetched server-side during the rendering of the layout.
AppShell can be a component you previously used inside getInitialProps, and now you can pass settings via props.
👀 What About head?
If you need to dynamically set metadata based on the fetched data — use generateMetadata() in your layout or page:
// app/layout.tsx
export async function generateMetadata() {
const settings = await fetchAppSettings();
return {
title: settings.title,
description: settings.description,
};
}
generateMetadata is async and runs server-side.
###💡 If You Need Settings Globally:
If fetchAppSettings() is needed in many layouts or pages:
- Create a shared context or wrapper like
<AppProvider> which is server rendered:
// components/AppProvider.tsx
export default async function AppProvider({ children }: { children: React.ReactNode }) {
const settings = await fetchAppSettings();
return (
<SettingsContext.Provider value={settings}>
{children}
</SettingsContext.Provider>
);
}
Then wrap your layout:
// app/layout.tsx
import AppProvider from '@/components/AppProvider';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<AppProvider>
{children}
</AppProvider>
</body>
</html>
);
}
🔥 Side Notes:
- In App Router getInitialProps is not used anymore.
_document.tsx still exists but it should not contain data fetching now — only static customizations of the <html> and <body> tags.
✅ Summary
| Concern | Old in _document.tsx | New in App Router |
|---|
| Server data fetching | getInitialProps | Inline await in layout.tsx (async Server Component) |
| HTML structure | Customized in _document.tsx | Done in app/layout.tsx |
| Metadata | Manipulated via Head | Use generateMetadata() |
| Global settings | getInitialProps in _app.tsx or _document.tsx | Use server-side Context Providers or props in layout.tsx |
Let me know what kind of data you’re loading in _document.tsx — I can provide a tailored refactor plan.