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: encode flight params in url #374

Draft
wants to merge 2 commits into
base: feat-preload-flight
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion packages/react-server/src/entry/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export async function start() {
});
const request = createStreamRequest(location.href, {
lastPathname,
revalidate: ROUTER_REVALIDATE_KEY in location.state,
revalidate: ROUTER_REVALIDATE_KEY in location.state ? true : undefined,
});
startTransition(() => {
$__setLayout(
Expand Down
7 changes: 5 additions & 2 deletions packages/react-server/src/features/router/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,18 @@ export function usePreloadHandlers({
preload,
}: { href: string; preload?: boolean | "data" }) {
const routeManifest = React.useContext(RouteManifestContext);
const currentPathname = useRouter((s) => s.location.pathname);
const callback = React.useCallback(() => {
if (!preload) return;

const url = new URL(href, window.location.href);
const deps = getRouteAssetDeps(routeManifest, url.pathname);
preloadAssetDeps(deps);

if (preload === "data") {
const { url } = createStreamRequest(href, {});
if (preload === "data" && url.pathname !== currentPathname) {
const { url } = createStreamRequest(href, {
lastPathname: currentPathname,
});
ReactDom.preload(url.slice(window.location.origin.length), {
as: "fetch",
crossOrigin: "",
Expand Down
16 changes: 6 additions & 10 deletions packages/react-server/src/features/server-component/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// encode flight request as path for the ease of ssg deployment
export const RSC_PATH = "__f.data";
const RSC_PARAM = "x-flight-meta";
const RSC_PARAM = "__f";

type StreamRequestParam = {
actionId?: string;
Expand All @@ -12,11 +12,8 @@ type StreamRequestParam = {
export function createStreamRequest(href: string, param: StreamRequestParam) {
const url = new URL(href, window.location.href);
url.pathname = posixJoin(url.pathname, RSC_PATH);
return new Request(url, {
headers: {
[RSC_PARAM]: JSON.stringify(param),
},
});
url.searchParams.set(RSC_PARAM, JSON.stringify(param));
return new Request(url);
}

export function unwrapStreamRequest(request: Request) {
Expand All @@ -26,15 +23,14 @@ export function unwrapStreamRequest(request: Request) {
return { url, request, isStream };
}
url.pathname = url.pathname.slice(0, -RSC_PATH.length) || "/";
const headers = new Headers(request.headers);
const rawParam = headers.get(RSC_PARAM);
headers.delete(RSC_PARAM);
const rawParam = url.searchParams.get(RSC_PARAM);
url.searchParams.delete(RSC_PARAM);

return {
url,
request: new Request(url, {
method: request.method,
headers,
headers: request.headers,
body: request.body,
// @ts-ignore undici
...("duplex" in request ? { duplex: "half" } : {}),
Expand Down
Loading