Skip to content

Commit 0b0efe5

Browse files
committed
Dedup query middlewares with render function
1 parent 7ab2f19 commit 0b0efe5

File tree

3 files changed

+204
-81
lines changed

3 files changed

+204
-81
lines changed

packages/router/__tests__/router-test.ts

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import type {
4141
import {
4242
AbortedDeferredError,
4343
createMiddlewareStore,
44+
getRouteAwareMiddlewareContext,
4445
isRouteErrorResponse,
4546
stripBasename,
4647
} from "../utils";
@@ -11954,13 +11955,21 @@ describe("a router", () => {
1195411955
future: { unstable_middleware: true },
1195511956
});
1195611957

11957-
let context = await query(createRequest("/parent/child/grandchild"));
11958+
let context = await query(createRequest("/parent/child/grandchild"), {
11959+
render: (context) => {
11960+
invariant(
11961+
!(context instanceof Response),
11962+
"Expected StaticHandlerContext"
11963+
);
11964+
return Promise.resolve(json(context.loaderData));
11965+
},
11966+
});
1195811967

1195911968
invariant(
11960-
!(context instanceof Response),
11961-
"Expected StaticHandlerContext"
11969+
context instanceof Response,
11970+
"Expected Response from query() with render()"
1196211971
);
11963-
expect(context.loaderData).toMatchInlineSnapshot(`
11972+
expect(await context.json()).toMatchInlineSnapshot(`
1196411973
{
1196511974
"child": "CHILD LOADER",
1196611975
"grandchild": "GRANDCHILD LOADER",
@@ -11970,19 +11979,13 @@ describe("a router", () => {
1197011979
expect(calls).toMatchInlineSnapshot(`
1197111980
[
1197211981
"parent loader middleware start",
11973-
"parent loader middleware start",
11974-
"parent loader middleware start",
11975-
" parent loader start",
1197611982
" child loader middleware start",
11977-
" child loader middleware start",
11978-
" parent loader end",
11979-
"parent loader middleware end",
11980-
" child loader start",
1198111983
" grandchild loader middleware start",
11982-
" child loader end",
11983-
" child loader middleware end",
11984-
"parent loader middleware end",
11984+
" parent loader start",
11985+
" child loader start",
1198511986
" grandchild loader start",
11987+
" parent loader end",
11988+
" child loader end",
1198611989
" grandchild loader end",
1198711990
" grandchild loader middleware end",
1198811991
" child loader middleware end",
@@ -12094,9 +12097,7 @@ describe("a router", () => {
1209412097
await currentRouter?.navigate("/parent");
1209512098
expect(currentRouter.state.location.pathname).toBe("/parent");
1209612099
expect(currentRouter.state.errors).toEqual({
12097-
parent: new Error(
12098-
"You may only call `next()` once per middleware and you may not call it in an action or loader"
12099-
),
12100+
parent: new Error("You may only call `next()` once per middleware"),
1210012101
});
1210112102
});
1210212103

@@ -12127,7 +12128,7 @@ describe("a router", () => {
1212712128
expect(currentRouter.state.location.pathname).toBe("/parent");
1212812129
expect(currentRouter.state.errors).toEqual({
1212912130
parent: new Error(
12130-
"You may only call `next()` once per middleware and you may not call it in an action or loader"
12131+
"You can not call context.next() in a loader or action"
1213112132
),
1213212133
});
1213312134
});
@@ -12165,7 +12166,7 @@ describe("a router", () => {
1216512166
expect(currentRouter.state.location.pathname).toBe("/parent");
1216612167
expect(currentRouter.state.errors).toEqual({
1216712168
parent: new Error(
12168-
"You may only call `next()` once per middleware and you may not call it in an action or loader"
12169+
"You can not call context.next() in a loader or action"
1216912170
),
1217012171
});
1217112172
});
@@ -12388,14 +12389,17 @@ describe("a router", () => {
1238812389
future: { unstable_middleware: true },
1238912390
});
1239012391

12391-
let ctx = await query(createRequest("/parent/child/grandchild"));
12392+
let ctx = await query(createRequest("/parent/child/grandchild"), {
12393+
render: (context) => {
12394+
return Promise.resolve(
12395+
json((context as StaticHandlerContext).loaderData)
12396+
);
12397+
},
12398+
});
1239212399

12393-
if (ctx instanceof Response) {
12394-
throw new Error("Unexpected Response");
12395-
}
12400+
invariant(ctx instanceof Response, "Expected Response");
1239612401

12397-
expect(ctx.location.pathname).toBe("/parent/child/grandchild");
12398-
expect(ctx.loaderData).toEqual({
12402+
expect(await ctx.json()).toEqual({
1239912403
parent: 1,
1240012404
child: 2,
1240112405
grandchild: 3,
@@ -12417,17 +12421,24 @@ describe("a router", () => {
1241712421
});
1241812422

1241912423
let middlewareContext = createMiddlewareStore();
12420-
middlewareContext.set(loaderCountContext, 50);
12424+
let routeMiddlewareContext = getRouteAwareMiddlewareContext(
12425+
middlewareContext,
12426+
-1,
12427+
() => {}
12428+
);
12429+
routeMiddlewareContext.set(loaderCountContext, 50);
1242112430
let ctx = await query(createRequest("/parent/child/grandchild"), {
1242212431
middlewareContext,
12432+
render: (context) => {
12433+
return Promise.resolve(
12434+
json((context as StaticHandlerContext).loaderData)
12435+
);
12436+
},
1242312437
});
1242412438

12425-
if (ctx instanceof Response) {
12426-
throw new Error("Unexpected Response");
12427-
}
12439+
invariant(ctx instanceof Response, "Expected Response");
1242812440

12429-
expect(ctx.location.pathname).toBe("/parent/child/grandchild");
12430-
expect(ctx.loaderData).toEqual({
12441+
expect(await ctx.json()).toEqual({
1243112442
parent: 51,
1243212443
child: 52,
1243312444
grandchild: 53,
@@ -12440,7 +12451,12 @@ describe("a router", () => {
1244012451
});
1244112452

1244212453
let middlewareContext = createMiddlewareStore();
12443-
middlewareContext.set(loaderCountContext, 50);
12454+
let routeMiddlewareContext = getRouteAwareMiddlewareContext(
12455+
middlewareContext,
12456+
-1,
12457+
() => {}
12458+
);
12459+
routeMiddlewareContext.set(loaderCountContext, 50);
1244412460
let res = await queryRoute(createRequest("/parent/child/grandchild"), {
1244512461
middlewareContext,
1244612462
});

0 commit comments

Comments
 (0)