-
Notifications
You must be signed in to change notification settings - Fork 326
fix(shims): add runtime default export to next/app #1204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+224
−18
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /** | ||
| * next/app shim | ||
| * | ||
| * Provides the AppProps type and a runtime default `App` class component | ||
| * for Pages Router fixtures that follow the canonical `_app.js` pattern: | ||
| * | ||
| * import App from "next/app"; | ||
| * export default class MyApp extends App { ... } | ||
| * | ||
| * or call `App.getInitialProps(appContext)` from a custom getInitialProps. | ||
| * | ||
| * Ported from Next.js: | ||
| * https://github.com/vercel/next.js/blob/canary/packages/next/src/pages/_app.tsx | ||
| * | ||
| * Behavioural parity notes: | ||
| * - `App.getInitialProps(appContext)` returns `{ pageProps }`, where | ||
| * `pageProps` comes from the wrapped page's own `getInitialProps` (if | ||
| * any). This matches Next.js's behaviour via `loadGetInitialProps`. | ||
| * - `render()` returns `<Component {...pageProps} />` — the default | ||
| * behaviour Next.js documents for the built-in App. | ||
| * - `origGetInitialProps` is preserved alongside `getInitialProps` for | ||
| * userland code that introspects the original implementation. | ||
| * | ||
| * Type signatures mirror Next.js's intentionally permissive `<P = any>` | ||
| * generics so that userland subclasses like `class MyApp extends App` | ||
| * type-check without forcing the caller to supply generic parameters. | ||
| */ | ||
| // oxlint-disable typescript/no-explicit-any -- match Next.js's permissive _app.tsx generics | ||
| import React, { type ComponentType } from "react"; | ||
|
|
||
| export type AppProps<P = any> = { | ||
| Component: ComponentType<P> & { | ||
| getInitialProps?: (ctx: any) => any; | ||
| }; | ||
| pageProps: P; | ||
| router?: any; | ||
| __N_SSG?: boolean; | ||
| __N_SSP?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * The context passed to `App.getInitialProps`. Mirrors Next.js's | ||
| * `AppContextType` from `packages/next/src/shared/lib/utils.ts`. | ||
| */ | ||
| export type AppContext = { | ||
| Component: ComponentType<any> & { | ||
| getInitialProps?: (ctx: any) => any; | ||
| }; | ||
| AppTree: ComponentType<any>; | ||
| ctx: any; | ||
| router: any; | ||
| }; | ||
|
|
||
| /** | ||
| * The initial props shape returned by `App.getInitialProps`. Mirrors | ||
| * Next.js's `AppInitialProps` from `packages/next/src/shared/lib/utils.ts`. | ||
| */ | ||
| export type AppInitialProps<PageProps = any> = { | ||
| pageProps: PageProps; | ||
| }; | ||
|
|
||
| async function appGetInitialProps({ Component, ctx }: AppContext): Promise<AppInitialProps> { | ||
| // Next.js delegates this to `loadGetInitialProps(Component, ctx)`. For the | ||
| // canonical _app pattern the relevant behaviour is: invoke the wrapped | ||
| // page's `getInitialProps` if defined, otherwise return `{}` for | ||
| // pageProps. We replicate that minimal shape without pulling in the | ||
| // full development-only validation logic from utils.ts. | ||
| let pageProps: any = {}; | ||
| if (typeof Component.getInitialProps === "function") { | ||
| pageProps = await Component.getInitialProps(ctx); | ||
| // Divergence from Next.js (intentional, current scope): | ||
| // | ||
| // Next.js's `loadGetInitialProps` throws when a page's getInitialProps | ||
| // resolves to null/undefined: | ||
| // | ||
| // "<DisplayName>.getInitialProps() should resolve to an object. | ||
| // But found "null"/"undefined" instead." | ||
| // | ||
| // See: https://github.com/vercel/next.js/blob/canary/packages/next/src/shared/lib/utils.ts | ||
| // | ||
| // vinext currently coerces the missing value to `{}` so that fixtures | ||
| // and userland code that accidentally return nothing still render | ||
| // (just with empty pageProps) instead of crashing the page. If you're | ||
| // debugging a Pages Router page that renders with mysteriously empty | ||
| // props, suspect a `getInitialProps` that returns undefined — Next.js | ||
| // would have surfaced that as a thrown error at this point. | ||
| if (pageProps == null) { | ||
| pageProps = {}; | ||
| } | ||
| } | ||
| return { pageProps }; | ||
| } | ||
|
|
||
| export default class App<P = any, CP = any, S = any> extends React.Component<P & AppProps<CP>, S> { | ||
| static origGetInitialProps = appGetInitialProps; | ||
| static getInitialProps = appGetInitialProps; | ||
|
|
||
| render(): React.ReactNode { | ||
| const { Component, pageProps } = this.props as AppProps<CP>; | ||
| // Cast to ComponentType<any> so the JSX spread type-checks regardless | ||
| // of the user-supplied `CP` generic. Mirrors how Next.js's _app.tsx | ||
| // works in practice: callers extending `App` rarely supply explicit | ||
| // page-prop generics, so the spread has to be permissive here. | ||
| const PageComponent = Component as ComponentType<any>; | ||
| return <PageComponent {...pageProps} />; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Next.js's
loadGetInitialPropsthrows here when the result is null/undefined ("should resolve to an object. But found \"${props}\" instead."), while this silently coerces to{}. The PR documents this as intentional and it's fine for the current scope — well-behavedgetInitialPropsimplementations won't return nullish. Just flagging for awareness: if a future deploy-suite failure shows a page rendering with empty props when it should have errored, this is the divergence point.