Skip to content

Commit 8c9c4ad

Browse files
committed
Simplify clientLoader/Action logic, refactor
1 parent ad8ce76 commit 8c9c4ad

File tree

1 file changed

+55
-80
lines changed

1 file changed

+55
-80
lines changed

packages/react-router/lib/dom/ssr/routes.tsx

Lines changed: 55 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -423,25 +423,6 @@ export function createClientRoutes(
423423
prefetchStylesAndCallHandler(() => {
424424
return fetchServerLoader(singleFetch);
425425
});
426-
} else if (route.clientLoaderModule) {
427-
dataRoute.loader = async (
428-
args: LoaderFunctionArgs,
429-
singleFetch?: unknown
430-
) => {
431-
invariant(route.clientLoaderModule);
432-
let { clientLoader } = await import(
433-
/* @vite-ignore */
434-
/* webpackIgnore: true */
435-
route.clientLoaderModule
436-
);
437-
return clientLoader({
438-
...args,
439-
async serverLoader() {
440-
preventInvalidServerHandlerCall("loader", route);
441-
return fetchServerLoader(singleFetch);
442-
},
443-
});
444-
};
445426
}
446427
if (!route.hasClientAction) {
447428
dataRoute.action = (_: ActionFunctionArgs, singleFetch?: unknown) =>
@@ -451,26 +432,6 @@ export function createClientRoutes(
451432
}
452433
return fetchServerAction(singleFetch);
453434
});
454-
} else if (route.clientActionModule) {
455-
dataRoute.action = async (
456-
args: ActionFunctionArgs,
457-
singleFetch?: unknown
458-
) => {
459-
invariant(route.clientActionModule);
460-
prefetchRouteModuleChunks(route);
461-
let { clientAction } = await import(
462-
/* @vite-ignore */
463-
/* webpackIgnore: true */
464-
route.clientActionModule
465-
);
466-
return clientAction({
467-
...args,
468-
async serverAction() {
469-
preventInvalidServerHandlerCall("action", route);
470-
return fetchServerAction(singleFetch);
471-
},
472-
});
473-
};
474435
}
475436

476437
let lazyRoutePromise:
@@ -502,50 +463,64 @@ export function createClientRoutes(
502463
}
503464

504465
dataRoute.lazy = {
505-
loader:
506-
dataRoute.loader || !route.hasClientLoader
507-
? undefined
508-
: async () => {
509-
let { clientLoader } = await getLazyRoute();
510-
invariant(clientLoader, "No `clientLoader` export found");
511-
return (args: LoaderFunctionArgs, singleFetch?: unknown) =>
512-
clientLoader({
513-
...args,
514-
async serverLoader() {
515-
preventInvalidServerHandlerCall("loader", route);
516-
return fetchServerLoader(singleFetch);
517-
},
518-
});
519-
},
520-
action:
521-
dataRoute.action || !route.hasClientAction
522-
? undefined
523-
: async () => {
524-
let { clientAction } = await getLazyRoute();
525-
invariant(clientAction, "No `clientAction` export found");
526-
return (args: ActionFunctionArgs, singleFetch?: unknown) =>
527-
clientAction({
528-
...args,
529-
async serverAction() {
530-
preventInvalidServerHandlerCall("action", route);
531-
return fetchServerAction(singleFetch);
532-
},
533-
});
534-
},
535-
unstable_middleware: !route.hasClientMiddleware
536-
? undefined
537-
: async () => {
538-
let clientMiddlewareModule = await import(
539-
/* @vite-ignore */
540-
/* webpackIgnore: true */
541-
route.clientMiddlewareModule || route.module
542-
);
466+
loader: route.hasClientLoader
467+
? async () => {
468+
let { clientLoader } = route.clientLoaderModule
469+
? await import(
470+
/* @vite-ignore */
471+
/* webpackIgnore: true */
472+
route.clientLoaderModule
473+
)
474+
: await getLazyRoute();
475+
invariant(clientLoader, "No `clientLoader` export found");
476+
return (args: LoaderFunctionArgs, singleFetch?: unknown) =>
477+
clientLoader({
478+
...args,
479+
async serverLoader() {
480+
preventInvalidServerHandlerCall("loader", route);
481+
return fetchServerLoader(singleFetch);
482+
},
483+
});
484+
}
485+
: undefined,
486+
action: route.hasClientAction
487+
? async () => {
488+
let clientActionPromise = route.clientActionModule
489+
? import(
490+
/* @vite-ignore */
491+
/* webpackIgnore: true */
492+
route.clientActionModule
493+
)
494+
: getLazyRoute();
495+
prefetchRouteModuleChunks(route);
496+
let { clientAction } = await clientActionPromise;
497+
invariant(clientAction, "No `clientAction` export found");
498+
return (args: ActionFunctionArgs, singleFetch?: unknown) =>
499+
clientAction({
500+
...args,
501+
async serverAction() {
502+
preventInvalidServerHandlerCall("action", route);
503+
return fetchServerAction(singleFetch);
504+
},
505+
});
506+
}
507+
: undefined,
508+
unstable_middleware: route.hasClientMiddleware
509+
? async () => {
510+
let { unstable_clientMiddleware } = route.clientMiddlewareModule
511+
? await import(
512+
/* @vite-ignore */
513+
/* webpackIgnore: true */
514+
route.clientMiddlewareModule
515+
)
516+
: await getLazyRoute();
543517
invariant(
544-
clientMiddlewareModule?.unstable_clientMiddleware,
518+
unstable_clientMiddleware,
545519
"No `unstable_clientMiddleware` export found"
546520
);
547-
return clientMiddlewareModule.unstable_clientMiddleware;
548-
},
521+
return unstable_clientMiddleware;
522+
}
523+
: undefined,
549524
shouldRevalidate: async () => {
550525
let lazyRoute = await getLazyRoute();
551526
return getShouldRevalidateFunction(

0 commit comments

Comments
 (0)