Skip to content

Commit

Permalink
refactor: move code
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Aug 4, 2024
1 parent 051fe7e commit 563924a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
7 changes: 4 additions & 3 deletions packages/react-server/src/entry/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { RouterHistory } from "@tanstack/history";
import React from "react";
import ReactDOMClient from "react-dom/client";
import { initializeReactClientBrowser } from "../features/client-component/browser";
import { RootErrorBoundary } from "../features/error/error-boundary";
import { ErrorBoundary } from "../features/error/error-boundary";
import { DefaultGlobalErrorPage } from "../features/error/global-error";
import {
FlightDataContext,
LayoutRoot,
Expand Down Expand Up @@ -182,14 +183,14 @@ async function start() {
const routeManifest = await importRouteManifest();
let reactRootEl = (
<RouterContext.Provider value={router}>
<RootErrorBoundary>
<ErrorBoundary errorComponent={DefaultGlobalErrorPage}>
<FlightDataHandler>
<RouteManifestContext.Provider value={routeManifest}>
<RouteAssetLinks />
<LayoutRoot />
</RouteManifestContext.Provider>
</FlightDataHandler>
</RootErrorBoundary>
</ErrorBoundary>
</RouterContext.Provider>
);
if (!window.location.search.includes("__noStrict")) {
Expand Down
30 changes: 1 addition & 29 deletions packages/react-server/src/features/error/error-boundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { tinyassert } from "@hiogawa/utils";
import React from "react";
import { useRouter } from "../router/client/router";
import type { ErrorPageProps } from "../router/server";
import {
getErrorContext,
getStatusText,
isNotFoundError,
isRedirectError,
} from "./shared";
import { getErrorContext, isNotFoundError, isRedirectError } from "./shared";

// cf.
// https://github.com/vercel/next.js/blob/33f8428f7066bf8b2ec61f025427ceb2a54c4bdf/packages/next/src/client/components/error-boundary.tsx
Expand Down Expand Up @@ -71,29 +66,6 @@ function ErrorAutoReset(props: Pick<ErrorPageProps, "reset">) {
return null;
}

export function RootErrorBoundary(props: React.PropsWithChildren) {
return <ErrorBoundary errorComponent={DefaultRootErrorPage} {...props} />;
}

// TODO: customizable
function DefaultRootErrorPage(props: ErrorPageProps) {
const status = props.serverError?.status;
const message = status
? `${status} ${getStatusText(status)}`
: "Unknown Error";
return (
<html>
<title>{message}</title>
<body>
<h1>{message}</h1>
<div>
Back to <a href="/">Home</a>
</div>
</body>
</html>
);
}

export class RedirectBoundary extends React.Component<React.PropsWithChildren> {
override state: { error: null } | { error: Error; redirectLocation: string } =
{ error: null };
Expand Down
25 changes: 25 additions & 0 deletions packages/react-server/src/features/error/global-error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use client";

import type { ErrorPageProps } from "../../server";
import { getStatusText } from "./shared";

// https://github.com/vercel/next.js/blob/677c9b372faef680d17e9ba224743f44e1107661/packages/next/src/build/webpack/loaders/next-app-loader.ts#L73
// https://github.com/vercel/next.js/blob/677c9b372faef680d17e9ba224743f44e1107661/packages/next/src/client/components/error-boundary.tsx#L145
export function DefaultGlobalErrorPage(props: ErrorPageProps) {
const status = props.serverError?.status;
const message = status
? `${status} ${getStatusText(status)}`
: "Unknown Error";

return (
<html>
<title>{message}</title>
<body>
<h1>{message}</h1>
<div>
Back to <a href="/">Home</a>
</div>
</body>
</html>
);
}
12 changes: 12 additions & 0 deletions packages/react-server/src/features/error/not-found.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://github.com/vercel/next.js/blob/8f5f0ef141a907d083eedb7c7aca52b04f9d258b/packages/next/src/client/components/not-found-error.tsx#L34-L39
export function DefaultNotFoundPage() {
return (
<>
<title>404 Not Found</title>
<h1>404 Not Found</h1>
<div>
Back to <a href="/">Home</a>
</div>
</>
);
}
13 changes: 1 addition & 12 deletions packages/react-server/src/features/router/server.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { sortBy, tinyassert } from "@hiogawa/utils";
import React from "react";
import { DefaultNotFoundPage } from "../error/not-found";
import { type ReactServerErrorContext } from "../error/shared";
import { renderMetadata } from "../meta/server";
import type { Metadata } from "../meta/utils";
Expand Down Expand Up @@ -58,18 +59,6 @@ export function generateRouteModuleTree(globEntries: Record<string, any>) {
return { tree, manifest };
}

// https://github.com/vercel/next.js/blob/8f5f0ef141a907d083eedb7c7aca52b04f9d258b/packages/next/src/client/components/not-found-error.tsx#L34-L39
function DefaultNotFoundPage() {
return (
<>
<h1>404 Not Found</h1>
<div>
Back to <a href="/">Home</a>
</div>
</>
);
}

// use own "use client" components as external
function importRuntimeClient(): Promise<typeof import("../../runtime/client")> {
return import("@hiogawa/react-server/runtime/client" as string);
Expand Down

0 comments on commit 563924a

Please sign in to comment.