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

Bring over Remix 2.9.2 logic for single fetch #11552

Merged
merged 6 commits into from
May 10, 2024
Merged
Changes from 1 commit
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
Next Next commit
Bring over Remix 2.9.2 logic for single fetch resource routes
brophdawg11 committed May 10, 2024
commit 2de34e5b59bc3c241a690075025a83cfa82eb665
4 changes: 1 addition & 3 deletions integration/helpers/create-fixture.ts
Original file line number Diff line number Diff line change
@@ -27,7 +27,6 @@ export interface FixtureInit {
template?: "cf-template" | "deno-template" | "node-template";
useReactRouterServe?: boolean;
spaMode?: boolean;
singleFetch?: boolean;
port?: number;
}

@@ -312,7 +311,7 @@ export async function createFixtureProject(
filename.startsWith("vite.config.")
);

let { singleFetch, spaMode } = init;
let { spaMode } = init;

await writeTestFiles(
{
@@ -321,7 +320,6 @@ export async function createFixtureProject(
: {
"vite.config.js": await viteConfig.basic({
port,
singleFetch,
spaMode,
}),
}),
1 change: 0 additions & 1 deletion integration/helpers/vite.ts
Original file line number Diff line number Diff line change
@@ -36,7 +36,6 @@ export const viteConfig = {
basic: async (args: {
port: number;
fsAllow?: string[];
singleFetch?: boolean;
spaMode?: boolean;
}) => {
let pluginOptions: VitePluginConfig = {
232 changes: 167 additions & 65 deletions integration/single-fetch-test.ts

Large diffs are not rendered by default.

23 changes: 20 additions & 3 deletions packages/remix-server-runtime/server.ts
Original file line number Diff line number Diff line change
@@ -32,11 +32,13 @@ import {
getResponseStubs,
getSingleFetchDataStrategy,
getSingleFetchRedirect,
getSingleFetchResourceRouteDataStrategy,
mergeResponseStubs,
singleFetchAction,
singleFetchLoaders,
SingleFetchRedirectSymbol,
} from "./single-fetch";
import { ResponseStubOperationsSymbol } from "./routeModules";

export type RequestHandler = (
request: Request,
@@ -184,6 +186,7 @@ export const createRequestHandler: CreateRequestHandlerFunction = (
) {
response = await handleResourceRequest(
serverMode,
_build,
staticHandler,
matches.slice(-1)[0].route.id,
request,
@@ -433,33 +436,47 @@ async function handleDocumentRequest(

async function handleResourceRequest(
serverMode: ServerMode,
build: ServerBuild,
staticHandler: StaticHandler,
routeId: string,
request: Request,
loadContext: AppLoadContext,
handleError: (err: unknown) => void
) {
try {
let responseStubs = getResponseStubs();
// Note we keep the routeId here to align with the Remix handling of
// resource routes which doesn't take ?index into account and just takes
// the leaf match
let response = await staticHandler.queryRoute(request, {
routeId,
requestContext: loadContext,
unstable_dataStrategy: getSingleFetchResourceRouteDataStrategy({
responseStubs,
}),
});

if (typeof response === "object") {
invariant(
!(DEFERRED_SYMBOL in response),
`You cannot return a \`defer()\` response from a Resource Route. Did you ` +
`forget to export a default UI component from the "${routeId}" route?`
);
}
// callRouteLoader/callRouteAction always return responses (w/o single fetch).
// With single fetch, users should always be Responses from resource routes

invariant(
isResponse(response),
"Expected a Response to be returned from queryRoute"
"Expected a Response to be returned from resource route handler"
);

let stub = responseStubs[routeId];
// Use the response status and merge any response stub headers onto it
let ops = stub[ResponseStubOperationsSymbol];
for (let [op, ...args] of ops) {
// @ts-expect-error
response.headers[op](...args);
}

return response;
} catch (error: unknown) {
if (isResponse(error)) {
31 changes: 29 additions & 2 deletions packages/remix-server-runtime/single-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
StaticHandler,
unstable_DataStrategyFunctionArgs as DataStrategyFunctionArgs,
unstable_DataStrategyFunction as DataStrategyFunction,
StaticHandlerContext,
UNSAFE_SingleFetchRedirectResult as SingleFetchRedirectResult,
UNSAFE_SingleFetchResult as SingleFetchResult,
@@ -39,7 +40,7 @@ export function getSingleFetchDataStrategy(
isActionDataRequest,
loadRouteIds,
}: { isActionDataRequest?: boolean; loadRouteIds?: string[] } = {}
) {
): DataStrategyFunction {
return async ({ request, matches }: DataStrategyFunctionArgs) => {
// Don't call loaders on action data requests
if (isActionDataRequest && request.method === "GET") {
@@ -92,6 +93,32 @@ export function getSingleFetchDataStrategy(
};
}

export function getSingleFetchResourceRouteDataStrategy({
responseStubs,
}: {
responseStubs: ReturnType<typeof getResponseStubs>;
}): DataStrategyFunction {
return async ({ matches }: DataStrategyFunctionArgs) => {
let results = await Promise.all(
matches.map(async (match) => {
let responseStub = match.shouldLoad
? responseStubs[match.route.id]
: null;
let result = await match.resolve(async (handler) => {
// Cast `ResponseStubImpl -> ResponseStub` to hide the symbol in userland
let ctx: DataStrategyCtx = {
response: responseStub as ResponseStub,
};
let data = await handler(ctx);
return { type: "data", result: data };
});
return result;
})
);
return results;
};
}

export async function singleFetchAction(
serverMode: ServerMode,
staticHandler: StaticHandler,
@@ -118,7 +145,7 @@ export async function singleFetchAction(
}),
});

// Unlike `handleDataRequest`, when singleFetch is enabled, queryRoute does
// Unlike `handleDataRequest`, when singleFetch is enabled, query does
// let non-Response return values through
if (isResponse(result)) {
return {