diff --git a/src/constants.ts b/src/constants.ts index 2342ab47..94308d4a 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,3 +1,4 @@ export const INTERNAL_PREFIX = "/_frsh"; export const DEV_ERROR_OVERLAY_URL = `${INTERNAL_PREFIX}/error_overlay`; export const ALIVE_URL = `${INTERNAL_PREFIX}/alive`; +export const PARTIAL_SEARCH_PARAM = "fresh-partial"; diff --git a/src/context.ts b/src/context.ts index 49d6d91c..0bee3ac4 100644 --- a/src/context.ts +++ b/src/context.ts @@ -14,7 +14,7 @@ import { RenderState, setRenderState, } from "./runtime/server/preact_hooks.tsx"; -import { DEV_ERROR_OVERLAY_URL } from "./constants.ts"; +import { DEV_ERROR_OVERLAY_URL, PARTIAL_SEARCH_PARAM } from "./constants.ts"; import { BUILD_ID } from "./runtime/build_id.ts"; import { tracer } from "./otel.ts"; @@ -45,6 +45,14 @@ export interface FreshContext { readonly params: Record; readonly error: unknown; readonly info: Deno.ServeHandlerInfo; + /** + * Whether the current Request is a partial request. + * + * Partials in Fresh will append the query parameter + * {@linkcode PARTIAL_SEARCH_PARAM} to the URL. This property can + * be used to determine if only ``'s need to be rendered. + */ + readonly isPartial: boolean; /** * Return a redirect response to the specified path. This is the * preferred way to do redirects in Fresh. @@ -90,14 +98,15 @@ export let getBuildCache: (ctx: FreshContext) => BuildCache; export class FreshReqContext implements FreshContext, PageProps { - config: ResolvedFreshConfig; - url: URL; - req: Request; - params: Record; - state: State = {} as State; + readonly config: ResolvedFreshConfig; + readonly url: URL; + readonly req: Request; + readonly params: Record; + readonly state: State = {} as State; data: unknown = undefined; error: unknown | null = null; - info: Deno.ServeHandlerInfo | Deno.ServeHandlerInfo; + readonly info: Deno.ServeHandlerInfo | Deno.ServeHandlerInfo; + readonly isPartial: boolean; next: FreshContext["next"]; @@ -126,6 +135,7 @@ export class FreshReqContext this.info = info; this.params = params; this.config = config; + this.isPartial = url.searchParams.has(PARTIAL_SEARCH_PARAM); this.next = next; this.#islandRegistry = islandRegistry; this.#buildCache = buildCache; @@ -181,7 +191,7 @@ export class FreshReqContext }; let partialId = ""; - if (this.url.searchParams.has("fresh-partial")) { + if (this.url.searchParams.has(PARTIAL_SEARCH_PARAM)) { partialId = crypto.randomUUID(); headers.set("X-Fresh-Id", partialId); } diff --git a/src/context_test.tsx b/src/context_test.tsx index 692ce01f..7a413b36 100644 --- a/src/context_test.tsx +++ b/src/context_test.tsx @@ -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]); +}); diff --git a/src/runtime/client/partials.ts b/src/runtime/client/partials.ts index febffaef..ab98aa09 100644 --- a/src/runtime/client/partials.ts +++ b/src/runtime/client/partials.ts @@ -21,10 +21,9 @@ import { import { createRootFragment, isCommentNode, isElementNode } from "./reviver.ts"; import type { PartialStateJson } from "../server/preact_hooks.tsx"; import { parse } from "../../jsonify/parse.ts"; -import { INTERNAL_PREFIX } from "../../constants.ts"; +import { INTERNAL_PREFIX, PARTIAL_SEARCH_PARAM } from "../../constants.ts"; export const PARTIAL_ATTR = "f-partial"; -export const PARTIAL_SEARCH_PARAM = "fresh-partial"; class NoPartialsError extends Error {} diff --git a/src/runtime/server/preact_hooks.tsx b/src/runtime/server/preact_hooks.tsx index 8a4de3db..8564c8a8 100644 --- a/src/runtime/server/preact_hooks.tsx +++ b/src/runtime/server/preact_hooks.tsx @@ -25,7 +25,10 @@ import { } from "../shared_internal.tsx"; import type { BuildCache } from "../../build_cache.ts"; import { BUILD_ID } from "../build_id.ts"; -import { DEV_ERROR_OVERLAY_URL } from "../../constants.ts"; +import { + DEV_ERROR_OVERLAY_URL, + PARTIAL_SEARCH_PARAM, +} from "../../constants.ts"; import * as colors from "@std/fmt/colors"; import { escape as escapeHtml } from "@std/html"; import { HttpError } from "../../error.ts"; @@ -427,7 +430,7 @@ function FreshRuntimeScript() { const islandArr = Array.from(islands); - if (ctx.url.searchParams.has("fresh-partial")) { + if (ctx.url.searchParams.has(PARTIAL_SEARCH_PARAM)) { const islands = islandArr.map((island) => { const chunk = buildCache.getIslandChunkName(island.name); if (chunk === null) {