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. 
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user