Skip to content

Commit

Permalink
Add tests for SSR null loader values on non-executed loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Dec 9, 2022
1 parent f188645 commit e8d5340
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
51 changes: 51 additions & 0 deletions packages/router/__tests__/router-test.ts
Expand Up @@ -10560,6 +10560,57 @@ describe("a router", () => {
});
});

it("should fill in null loaderData values for routes without loaders", async () => {
let { query } = createStaticHandler([
{
id: "root",
path: "/",
children: [
{
id: "none",
path: "none",
},
{
id: "a",
path: "a",
loader: () => "A",
children: [
{
id: "b",
path: "b",
},
],
},
],
},
]);

// No loaders at all
let context = await query(createRequest("/none"));
expect(context).toMatchObject({
actionData: null,
loaderData: {
root: null,
none: null,
},
errors: null,
location: { pathname: "/none" },
});

// Mix of loaders and no loaders
context = await query(createRequest("/a/b"));
expect(context).toMatchObject({
actionData: null,
loaderData: {
root: null,
a: "A",
b: null,
},
errors: null,
location: { pathname: "/a/b" },
});
});

it("should support document load navigations returning responses", async () => {
let { query } = createStaticHandler(SSR_ROUTES);
let context = await query(createRequest("/parent/json"));
Expand Down
10 changes: 4 additions & 6 deletions packages/router/router.ts
Expand Up @@ -2345,6 +2345,7 @@ export function unstable_createStaticHandler(
if (matchesToLoad.length === 0) {
return {
matches,
// Add a null for all matched routes for proper revalidation on the client
loaderData: matches.reduce(
(acc, m) => Object.assign(acc, { [m.route.id]: null }),
{}
Expand Down Expand Up @@ -2375,11 +2376,11 @@ export function unstable_createStaticHandler(
throw new Error(`${method}() call aborted`);
}

// Can't do anything with these without the Remix side of things, so just
// cancel them for now
let executedLoaders = new Set<string>();
results.forEach((result, i) => {
executedLoaders.add(matchesToLoad[i].route.id);
// Can't do anything with these without the Remix side of things, so just
// cancel them for now
if (isDeferredResult(result)) {
result.deferredData.cancel();
}
Expand All @@ -2393,10 +2394,7 @@ export function unstable_createStaticHandler(
pendingActionError
);

// Add a null for any routes that didn't have loaders so we know they've been
// previously "executed" and qualify as a revalidation on subsequent
// invocations. This is mostly needed for SSr scenarios where there may be
// no server loader but there may be a client loaders to fetch code split bundles
// Add a null for any non-loader matches for proper revalidation on the client
matches.forEach((match) => {
if (!executedLoaders.has(match.route.id)) {
context.loaderData[match.route.id] = null;
Expand Down

0 comments on commit e8d5340

Please sign in to comment.