Skip to content
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

refactor(react-server): add ReactServerErrorContext.headers #253

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions packages/react-server/src/entry/react-server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,23 @@ export const handler: ReactServerHandler = async (ctx) => {
const errorCtx = getErrorContext(e) ?? DEFAULT_ERROR_CONTEXT;
if (rscOnly) {
// returns empty layout to keep current layout and
// let browser initiate clie-side navigation for redirection error
// let browser initiate client-side navigation for redirection error
const data: ServerRouterData = {
action: { error: errorCtx },
layout: {},
};
const stream = reactServerDomServer.renderToReadableStream(data, {});
return new Response(stream, {
headers: {
...errorCtx.headers,
"content-type": "text/x-component; charset=utf-8",
},
});
}
// TODO: general action error handling?
return new Response(null, {
status: errorCtx.status,
headers: errorCtx.redirectLocation
? {
location: errorCtx.redirectLocation,
}
: {},
headers: errorCtx.headers,
});
}
}
Expand Down
20 changes: 10 additions & 10 deletions packages/react-server/src/entry/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
ssrImportPromiseCache,
} from "../features/use-client/server";
import { Router, RouterContext } from "../lib/client/router";
import { getErrorContext, getStatusText } from "../lib/error";
import {
DEFAULT_ERROR_CONTEXT,
getErrorContext,
getStatusText,
isRedirectError,
} from "../lib/error";
import { __global } from "../lib/global";
import {
ENTRY_REACT_SERVER_WRAPPER,
Expand Down Expand Up @@ -121,16 +126,11 @@ export async function renderHtml(
},
});
} catch (e) {
const ctx = getErrorContext(e);
status = ctx?.status ?? 500;
if (ctx?.redirectLocation) {
return new Response(null, {
status,
headers: {
location: ctx.redirectLocation,
},
});
const ctx = getErrorContext(e) ?? DEFAULT_ERROR_CONTEXT;
if (isRedirectError(ctx)) {
return new Response(null, { status: ctx.status, headers: ctx.headers });
}
status = ctx.status;
// render empty as error fallback and
// let browser render full CSR instead of hydration
// which will replay client error boudnary from RSC error
Expand Down
19 changes: 11 additions & 8 deletions packages/react-server/src/features/router/client.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { RedirectHandler } from "../../lib/client/error-boundary";
import { isRedirectError } from "../../lib/error";
import { __global } from "../../lib/global";
import { LAYOUT_ROOT_NAME, type ServerRouterData } from "./utils";

Expand All @@ -25,14 +26,16 @@ export function ServerActionRedirectHandler() {
const ctx = React.useContext(LayoutStateContext);
const data = React.use(ctx.data);

if (data.action?.error?.redirectLocation) {
return (
<RedirectHandler
suspensionKey={data.action?.error}
redirectLocation={data.action?.error.redirectLocation}
/>
);
if (data.action?.error) {
const redirect = isRedirectError(data.action.error);
if (redirect) {
return (
<RedirectHandler
suspensionKey={data.action.error}
redirectLocation={redirect.location}
/>
);
}
}

return null;
}
7 changes: 4 additions & 3 deletions packages/react-server/src/lib/client/error-boundary.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { tinyassert } from "@hiogawa/utils";
import React from "react";
import { getErrorContext, getStatusText } from "../error";
import { getErrorContext, getStatusText, isRedirectError } from "../error";
import type { ErrorPageProps } from "../router";
import { useRouter } from "./router";

Expand Down Expand Up @@ -89,8 +89,9 @@ export class RedirectBoundary extends React.Component<React.PropsWithChildren> {
static getDerivedStateFromError(error: Error) {
if (!import.meta.env.SSR) {
const ctx = getErrorContext(error);
if (ctx?.redirectLocation) {
return { redirectLocation: ctx.redirectLocation };
const redirect = ctx && isRedirectError(ctx);
if (redirect) {
return { redirectLocation: redirect.location };
}
}
throw error;
Expand Down
20 changes: 13 additions & 7 deletions packages/react-server/src/lib/error.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// TODO: custom (de)serialization?
export interface ReactServerErrorContext {
status: number;
// TODO: hide from public typing?
redirectLocation?: string;
}

export interface ReactServerRedirectErrorContext {
redirectLocation: string;
headers?: Record<string, string>;
}

export class ReactServerDigestError extends Error {
Expand All @@ -21,7 +16,18 @@ export function createError(ctx: ReactServerErrorContext) {
}

export function redirect(location: string, status: number = 302) {
return createError({ status, redirectLocation: location });
return createError({
status,
headers: { location },
});
}

export function isRedirectError(ctx: ReactServerErrorContext) {
const location = ctx.headers?.["location"];
if (300 <= ctx.status && ctx.status <= 399 && typeof location === "string") {
return { location };
}
return false;
}

export function getErrorContext(
Expand Down
Loading