Skip to content

Commit 41e180e

Browse files
committed
Add test
1 parent 8b692fb commit 41e180e

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

packages/react-router/__tests__/router/context-middleware-test.tsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,62 @@ describe("context/middleware", () => {
300300
]);
301301
});
302302

303+
it("runs middleware on initialization even if no loaders exist", async () => {
304+
let snapshot;
305+
router = createRouter({
306+
history: createMemoryHistory(),
307+
routes: [
308+
{
309+
path: "/",
310+
middleware: [
311+
async ({ context }, next) => {
312+
await next();
313+
// Grab a snapshot at the end of the upwards middleware chain
314+
snapshot = context.get(orderContext);
315+
},
316+
getOrderMiddleware(orderContext, "a"),
317+
getOrderMiddleware(orderContext, "b"),
318+
],
319+
children: [
320+
{
321+
index: true,
322+
middleware: [
323+
getOrderMiddleware(orderContext, "c"),
324+
getOrderMiddleware(orderContext, "d"),
325+
],
326+
},
327+
],
328+
},
329+
],
330+
});
331+
let initPromise = new Promise((r) => {
332+
let unsub = router.subscribe((state) => {
333+
if (state.initialized) {
334+
unsub();
335+
r(undefined);
336+
}
337+
});
338+
});
339+
await router.initialize();
340+
await initPromise;
341+
expect(router.state).toMatchObject({
342+
initialized: true,
343+
location: { pathname: "/" },
344+
navigation: { state: "idle" },
345+
errors: null,
346+
});
347+
expect(snapshot).toEqual([
348+
"a middleware - before next()",
349+
"b middleware - before next()",
350+
"c middleware - before next()",
351+
"d middleware - before next()",
352+
"d middleware - after next()",
353+
"c middleware - after next()",
354+
"b middleware - after next()",
355+
"a middleware - after next()",
356+
]);
357+
});
358+
303359
it("runs middleware even if no loaders exist", async () => {
304360
let snapshot;
305361
router = createRouter({

packages/react-router/lib/router/router.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5744,26 +5744,22 @@ function getDataStrategyMatch(
57445744
return shouldRevalidateLoader(match, unstable_shouldRevalidateArgs);
57455745
},
57465746
resolve(handlerOverride) {
5747-
if (
5747+
let { lazy, loader, middleware } = match.route;
5748+
5749+
let callHandler =
57485750
isUsingNewApi ||
57495751
shouldLoad ||
57505752
(handlerOverride &&
57515753
!isMutationMethod(request.method) &&
5752-
(match.route.lazy || match.route.loader))
5753-
) {
5754-
// If this match was marked `shouldLoad` due to a middleware and it
5755-
// doesn't have a `loader` to run and no `lazy` to add one, then we can
5756-
// just return undefined from the "loader" here
5757-
if (
5758-
match.route.middleware &&
5759-
match.route.middleware.length > 0 &&
5760-
match.route.loader == null &&
5761-
!match.route.lazy &&
5762-
!handlerOverride
5763-
) {
5764-
return Promise.resolve({ type: ResultType.data, result: undefined });
5765-
}
5754+
(lazy || loader));
5755+
5756+
// If this match was marked `shouldLoad` due to a middleware and it
5757+
// doesn't have a `loader` to run and no `lazy` to add one, then we can
5758+
// just return undefined from the "loader" here
5759+
let isMiddlewareOnlyRoute =
5760+
middleware && middleware.length > 0 && loader == null && !lazy;
57665761

5762+
if (callHandler && !isMiddlewareOnlyRoute) {
57675763
return callLoaderOrAction({
57685764
request,
57695765
match,

0 commit comments

Comments
 (0)