Skip to content

Commit

Permalink
Simplify implementation (#9157)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergio Xalambrí <hello@sergiodxa.com>
  • Loading branch information
brophdawg11 and sergiodxa authored Mar 29, 2024
1 parent 54f7755 commit 0b379f6
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-adults-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/server-runtime": patch
---

[REMOVE] Remove RR flags and implement via dataStrategy
2 changes: 1 addition & 1 deletion integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@remix-run/dev": "workspace:*",
"@remix-run/express": "workspace:*",
"@remix-run/node": "workspace:*",
"@remix-run/router": "1.16.0-pre.0",
"@remix-run/router": "1.16.0-pre.1",
"@remix-run/server-runtime": "workspace:*",
"@types/express": "^4.17.9",
"@vanilla-extract/css": "^1.10.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@mdx-js/mdx": "^2.3.0",
"@npmcli/package-json": "^4.0.1",
"@remix-run/node": "workspace:*",
"@remix-run/router": "1.16.0-pre.0",
"@remix-run/router": "1.16.0-pre.1",
"@remix-run/server-runtime": "workspace:*",
"@types/mdx": "^2.0.5",
"@vanilla-extract/integration": "^6.2.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/remix-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"tsc": "tsc"
},
"dependencies": {
"@remix-run/router": "1.16.0-pre.0",
"@remix-run/router": "1.16.0-pre.1",
"@remix-run/server-runtime": "workspace:*",
"react-router": "6.23.0-pre.0",
"react-router-dom": "6.23.0-pre.0",
"react-router": "6.23.0-pre.1",
"react-router-dom": "6.23.0-pre.1",
"turbo-stream": "^2.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-server-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"tsc": "tsc"
},
"dependencies": {
"@remix-run/router": "1.16.0-pre.0",
"@remix-run/router": "1.16.0-pre.1",
"@types/cookie": "^0.6.0",
"@web3-storage/multipart-parser": "^1.0.0",
"cookie": "^0.6.0",
Expand Down
48 changes: 36 additions & 12 deletions packages/remix-server-runtime/single-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,22 @@ export type SingleFetchResults = {
};

export function getSingleFetchDataStrategy(
responseStubs: ReturnType<typeof getResponseStubs>
responseStubs: ReturnType<typeof getResponseStubs>,
{
isActionDataRequest,
loadRouteIds,
}: { isActionDataRequest?: boolean; loadRouteIds?: string[] } = {}
) {
return async ({ request, matches }: DataStrategyFunctionArgs) => {
// Don't call loaders on action data requests
if (isActionDataRequest && request.method === "GET") {
return await Promise.all(
matches.map((m) =>
m.resolve(async () => ({ type: "data", result: null }))
)
);
}

let results = await Promise.all(
matches.map(async (match) => {
let responseStub: ResponseStub | undefined;
Expand All @@ -49,7 +62,11 @@ export function getSingleFetchDataStrategy(
}

let result = await match.resolve(async (handler) => {
let data = await handler({ response: responseStub });
// Only run opt-in loaders when fine-grained revalidation is enabled
let data =
loadRouteIds && !loadRouteIds.includes(match.route.id)
? null
: await handler({ response: responseStub });
return { type: "data", result: data };
});

Expand Down Expand Up @@ -96,8 +113,9 @@ export async function singleFetchAction(
let result = await staticHandler.query(handlerRequest, {
requestContext: loadContext,
skipLoaderErrorBubbling: true,
skipLoaders: true,
unstable_dataStrategy: getSingleFetchDataStrategy(responseStubs),
unstable_dataStrategy: getSingleFetchDataStrategy(responseStubs, {
isActionDataRequest: true,
}),
});

// Unlike `handleDataRequest`, when singleFetch is enabled, queryRoute does
Expand All @@ -113,7 +131,9 @@ export async function singleFetchAction(
let context = result;

let singleFetchResult: SingleFetchResult;
let { statusCode, headers } = mergeResponseStubs(context, responseStubs);
let { statusCode, headers } = mergeResponseStubs(context, responseStubs, {
isActionDataRequest: true,
});

if (isRedirectStatusCode(statusCode) && headers.has("Location")) {
return {
Expand Down Expand Up @@ -176,9 +196,10 @@ export async function singleFetchLoaders(
let responseStubs = getResponseStubs();
let result = await staticHandler.query(handlerRequest, {
requestContext: loadContext,
loadRouteIds,
skipLoaderErrorBubbling: true,
unstable_dataStrategy: getSingleFetchDataStrategy(responseStubs),
unstable_dataStrategy: getSingleFetchDataStrategy(responseStubs, {
loadRouteIds,
}),
});

if (isResponse(result)) {
Expand Down Expand Up @@ -323,17 +344,20 @@ function proxyResponseToResponseStub(

export function mergeResponseStubs(
context: StaticHandlerContext,
responseStubs: ReturnType<typeof getResponseStubs>
responseStubs: ReturnType<typeof getResponseStubs>,
{ isActionDataRequest }: { isActionDataRequest?: boolean } = {}
) {
let statusCode: number | undefined = undefined;
let headers = new Headers();

// Action followed by top-down loaders
let actionStub = responseStubs[ResponseStubActionSymbol];
let stubs = [
actionStub,
...context.matches.map((m) => responseStubs[m.route.id]),
];
let stubs = [actionStub];

// Nothing to merge at the route level on action data requests
if (!isActionDataRequest) {
stubs.push(...context.matches.map((m) => responseStubs[m.route.id]));
}

for (let stub of stubs) {
// Take the highest error/redirect, or the lowest success value - preferring
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"dependencies": {
"@remix-run/node": "workspace:*",
"@remix-run/react": "workspace:*",
"@remix-run/router": "1.16.0-pre.0",
"react-router-dom": "6.23.0-pre.0"
"@remix-run/router": "1.16.0-pre.1",
"react-router-dom": "6.23.0-pre.1"
},
"devDependencies": {
"@remix-run/server-runtime": "workspace:*",
Expand Down
50 changes: 25 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b379f6

Please sign in to comment.