Skip to content

Commit

Permalink
rename next to serverFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-ebey committed Feb 27, 2023
1 parent 8d11aa8 commit ba3a350
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 46 deletions.
4 changes: 2 additions & 2 deletions integration/client-action-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ test.beforeAll(async () => {
};
}
export async function clientAction({ next }) {
const response = await next();
export async function clientAction({ serverFetch }) {
const response = await serverFetch();
const data = await response.json();
return {
...data,
Expand Down
10 changes: 5 additions & 5 deletions integration/client-loader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ test.beforeAll(async () => {
};
}
export async function clientLoader({ next }) {
const response = await next();
export async function clientLoader({ serverFetch }) {
const response = await serverFetch();
const data = await response.json();
return {
...data,
Expand Down Expand Up @@ -63,8 +63,8 @@ test.beforeAll(async () => {
});
}
export async function clientLoader({ next }) {
const deferred = await next();
export async function clientLoader({ serverFetch }) {
const deferred = await serverFetch();
deferred.data.client = "yes";
return deferred;
}
Expand All @@ -88,7 +88,7 @@ test.beforeAll(async () => {
import { useEffect } from "react";
import { useLoaderData, useRevalidator } from "@remix-run/react";
export async function clientLoader({ next }) {
export async function clientLoader() {
return {
title: "Hello from client",
client: "yes",
Expand Down
8 changes: 7 additions & 1 deletion packages/remix-react/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ export function isErrorResponse(response: any): boolean {
);
}

const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);

export function isRedirectStatusCode(statusCode: number): boolean {
return redirectStatusCodes.has(statusCode);
}

export function isRedirectResponse(response: any): boolean {
return (
response instanceof Response &&
(response.headers.get("X-Remix-Redirect") != null ||
(response.status >= 300 && response.status < 400))
isRedirectStatusCode(response.status))
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/remix-react/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface RouteModule {
export interface ClientDataFunctionArgs {
request: Request;
params: Params;
next: () => Promise<Response | DeferredData | null>;
serverFetch: () => Promise<Response | DeferredData | null>;
}

/**
Expand Down
63 changes: 26 additions & 37 deletions packages/remix-react/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isCatchResponse,
isDeferredResponse,
isRedirectResponse,
isRedirectStatusCode,
parseDeferredReadableStream,
} from "./data";
import type { FutureConfig } from "./entry";
Expand Down Expand Up @@ -174,6 +175,26 @@ async function loadRouteModuleWithBlockingLinks(
return routeModule;
}

async function handleDataResult(result: Response | Error) {
if (result instanceof Error) {
throw result;
}

if (isRedirectResponse(result)) {
throw getRedirect(result);
}

if (isCatchResponse(result)) {
throw result;
}

if (isDeferredResponse(result) && result.body) {
return await parseDeferredReadableStream(result.body);
}

return result;
}

function createDataFunction(
route: EntryRoute,
routeModules: RouteModules,
Expand All @@ -192,23 +213,7 @@ function createDataFunction(

let result = await fetchData(request, route.id);

if (result instanceof Error) {
throw result;
}

if (isRedirectResponse(result)) {
throw getRedirect(result);
}

if (isCatchResponse(result)) {
throw result;
}

if (isDeferredResponse(result) && result.body) {
return await parseDeferredReadableStream(result.body);
}

return result;
return await handleDataResult(result);
};

return async ({ request, params }) => {
Expand All @@ -226,38 +231,22 @@ function createDataFunction(
: undefined;

let result = clientDataFunction
? clientDataFunction({
? await clientDataFunction({
request,
params,
next: () => doFetch(request),
serverFetch: () => doFetch(request),
})
: await doFetch(request);

if (result instanceof Error) {
throw result;
}

if (isRedirectResponse(result)) {
throw getRedirect(result);
}

if (isCatchResponse(result)) {
throw result;
}

if (isDeferredResponse(result) && result.body) {
return await parseDeferredReadableStream(result.body);
}

return result;
return await handleDataResult(result);
} finally {
await routeModulePromise;
}
};
}

function getRedirect(response: Response): Response {
if (response.status >= 300 && response.status < 400) {
if (isRedirectStatusCode(response.status)) {
return response;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/remix-testing/create-remix-stub.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,7 @@ function convertToEntryRoute(
module: "",
hasCatchBoundary: false,
hasErrorBoundary: false,
hasClientAction: false,
hasClientLoader: false,
};
}

0 comments on commit ba3a350

Please sign in to comment.