feat: add flag to FreshContext for partial requests (#2993)

Addresses a gap between [Fresh
v1](https://github.com/denoland/fresh/blob/1.x/src/server/types.ts#L192)
and Fresh v2, by adding `ctx.isPartial` to `FreshContext`.

The same route (e.g. `GET /api/autocomplete`) can be called from both an
Island or a Partial. Returning JSON or HTML depending on the destination
of the code.

Or is the intent that endpoints returning partials and data should be
separated, and this pattern is not to be encouraged in Fresh v2? My use
case in Fresh v1 was an `/routes/api/cart.tsx`, which can either just
toggle a product from the cart (via cookie), but also update UI elements
(e.g. cart icon) based on if called from partial or not.


![image](https://github.com/user-attachments/assets/a1cbcfb1-2446-4d6d-bce5-4c3d6acf9bfd)
This commit is contained in:
Christian Svensson
2025-05-26 14:15:36 +02:00
committed by GitHub
parent 9eaefd5be0
commit b4c3ff8e98
5 changed files with 40 additions and 12 deletions
+15
View File
@@ -69,3 +69,18 @@ Deno.test("ctx.render - throw with invalid first arg", async () => {
await res.body?.cancel();
expect(res.status).toEqual(500);
});
Deno.test("ctx.isPartial - should indicate whether request is partial or not", async () => {
const isPartials: boolean[] = [];
const app = new App()
.get("/", (ctx) => {
isPartials.push(ctx.isPartial);
return new Response("ok");
});
const server = new FakeServer(app.handler());
await server.get("/");
await server.get("/?fresh-partial");
expect(isPartials).toEqual([false, true]);
});