Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flight-revalidate-all.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

A single-flight mutation answered with `revalidate: "*"` now collects every query on the target page instead of none.
2 changes: 1 addition & 1 deletion src/data/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ export function setupFlightDataConsumer(router: RouterContext) {
});
}

const REVALIDATE_ALL = "*";
export const REVALIDATE_ALL = "*";

/**
* Reads an `X-Revalidate` header value into the keys the cache ops take.
Expand Down
10 changes: 7 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
peekLazySubtrees,
resolveLazySubtree
} from "./routing.js";
import { REVALIDATE_ALL } from "./data/query.js";
import { extractSearchParams } from "./utils.js";
import { serverRouteArgs, serverRouteOf } from "./serverRouteShared.js";
import type { Branch, RouteDefinition, RoutePreloadFunc } from "./types.js";
Expand Down Expand Up @@ -94,7 +95,11 @@ function isRouterInstance(
export function createFlightDataCollector(
options: FlightDataCollectorOptions | RouterInstanceLike
): CollectFlightDataHook {
const { routes, rootPreload, base = "" } = isRouterInstance(options)
const {
routes,
rootPreload,
base = ""
} = isRouterInstance(options)
? { routes: options.routes, rootPreload: options.config.preload, base: options.config.base }
: options;
if (!routes) throw new Error("createFlightDataCollector requires `routes`");
Expand Down Expand Up @@ -127,7 +132,7 @@ export function createFlightDataCollector(
const event = { ...sourceEvent } as RequestEvent;
event.request = new Request(targetUrl, { headers: outcome.foldedHeaders });
event.router = {
dataOnly: revalidateKeys || true,
dataOnly: (!revalidateKeys?.includes(REVALIDATE_ALL) && revalidateKeys) || true,
previousUrl,
data: {}
};
Expand Down Expand Up @@ -218,4 +223,3 @@ function runPreloads(
});
}
}

26 changes: 26 additions & 0 deletions test/server/flight-revalidate-all.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// `revalidate: "*"` reaches the collector as `revalidateKeys: ["*"]` and must collect every query.
import { query } from "../../src/data/query.js";
import { createFlightDataCollector } from "../../src/server.js";

const getNotes = query(async () => ["note-1"], "revalidateAllNotes");

test("revalidate '*' collects every query on the page", async () => {
const collect = createFlightDataCollector({
routes: [{ path: "/notes", preload: () => getNotes() }]
});
const headers = new Headers({ referer: "http://localhost:3000/notes" });
const request = new Request("http://localhost:3000/_server", { method: "POST", headers });
const event = { request, response: { headers: new Headers() }, locals: {} };
const outcome = {
id: "fn#0",
value: "mutated",
response: new Response(null, { headers: { "X-Revalidate": "*" } }),
request,
thrown: false,
targetUrl: "http://localhost:3000/notes",
revalidateKeys: ["*"],
foldedHeaders: headers
};
const data: any = await collect(event as any, outcome as any);
expect(data && (await data["revalidateAllNotes[]"])).toEqual(["note-1"]);
});
Loading