@decocms/tanstack 7.41.1 → 7.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/tanstack",
3
- "version": "7.41.1",
3
+ "version": "7.43.0",
4
4
  "type": "module",
5
5
  "description": "Deco framework binding for TanStack Start + Cloudflare Workers",
6
6
  "repository": {
@@ -24,9 +24,9 @@
24
24
  "lint:unused": "knip"
25
25
  },
26
26
  "dependencies": {
27
- "@decocms/blocks": "7.41.1",
28
- "@decocms/blocks-admin": "7.41.1",
29
- "@decocms/blocks-cli": "7.41.1",
27
+ "@decocms/blocks": "7.43.0",
28
+ "@decocms/blocks-admin": "7.43.0",
29
+ "@decocms/blocks-cli": "7.43.0",
30
30
  "@deco-cx/warp-node": "^0.3.16",
31
31
  "fast-json-patch": "^3.1.0",
32
32
  "ws": "^8.18.0"
@@ -37,6 +37,7 @@ import {
37
37
  runSectionLoaders,
38
38
  runSingleSectionLoader,
39
39
  serializeRenderJson,
40
+ stringifyWithoutSecrets,
40
41
  } from "@decocms/blocks/cms";
41
42
  import { DECO_MATCHERS_OVERRIDE_PARAM } from "@decocms/blocks/matchers/override";
42
43
  import { clearLoaderCache } from "@decocms/blocks/sdk/cachedLoader";
@@ -228,6 +229,30 @@ export interface DecoWorkerEntryOptions {
228
229
  */
229
230
  deviceSpecificKeys?: boolean;
230
231
 
232
+ /**
233
+ * Serve pages as structured per-section JSON on `?renderJson` (the mobile
234
+ * page-as-JSON path). Set to `false` to disable the endpoint site-wide —
235
+ * `?renderJson` then just renders the normal HTML page.
236
+ * @default true
237
+ */
238
+ renderJson?: boolean;
239
+
240
+ /**
241
+ * Serve the raw resolved page as JSON on `?asJson` (legacy admin-preview
242
+ * path). Set to `false` to disable it site-wide.
243
+ * @default true
244
+ */
245
+ asJson?: boolean;
246
+
247
+ /**
248
+ * Cross-origin origins allowed to read the `?renderJson` response. CORS only
249
+ * matters for browser clients on another origin — a native app needs none.
250
+ * - unset (default) → no CORS headers (same-origin + native apps only).
251
+ * - `string[]` → reflect a matching request Origin, with credentials.
252
+ * - `"*"` → allow any origin (without credentials, per the spec).
253
+ */
254
+ pageJsonCors?: string[] | "*";
255
+
231
256
  /**
232
257
  * Build a full segment key from the incoming request.
233
258
  *
@@ -858,6 +883,37 @@ let _redirectMapRevision: string | null = null;
858
883
  // Factory
859
884
  // ---------------------------------------------------------------------------
860
885
 
886
+ /**
887
+ * CORS headers for the `?renderJson` response, from
888
+ * {@link DecoWorkerEntryOptions.pageJsonCors}. Off by default (native apps need
889
+ * no CORS); a list reflects an allow-listed request Origin with credentials;
890
+ * `"*"` allows any origin without credentials (per the CORS spec).
891
+ */
892
+ function pageJsonCorsHeaders(
893
+ request: Request,
894
+ cors: string[] | "*" | undefined,
895
+ ): Record<string, string> {
896
+ if (!cors) return {};
897
+ const base = {
898
+ "Access-Control-Allow-Methods": "GET, OPTIONS",
899
+ "Access-Control-Allow-Headers": "Content-Type, If-None-Match, Authorization",
900
+ "Access-Control-Expose-Headers": "ETag",
901
+ };
902
+ if (cors === "*") {
903
+ return { ...base, "Access-Control-Allow-Origin": "*" };
904
+ }
905
+ const origin = request.headers.get("origin");
906
+ if (origin && cors.includes(origin)) {
907
+ return {
908
+ ...base,
909
+ "Access-Control-Allow-Origin": origin,
910
+ "Access-Control-Allow-Credentials": "true",
911
+ Vary: "Origin",
912
+ };
913
+ }
914
+ return {}; // origin not allow-listed → no CORS headers
915
+ }
916
+
861
917
  /**
862
918
  * Creates a Cloudflare Worker fetch handler that wraps a TanStack Start
863
919
  * server entry with intelligent edge caching.
@@ -1743,7 +1799,23 @@ export function createDecoWorkerEntry(
1743
1799
  // ?renderJson — structured JSON of the page for the mobile app. Lean shape
1744
1800
  // ({ name, path, sections:[{ component, props }] }), each section projected
1745
1801
  // through its `renderJson` export. Wins over ?asJson when both are present.
1746
- if (url.searchParams.has("renderJson") && request.method === "GET") {
1802
+ // CORS preflight for ?renderJson (only emits headers when pageJsonCors is set).
1803
+ if (
1804
+ options.renderJson !== false &&
1805
+ request.method === "OPTIONS" &&
1806
+ url.searchParams.has("renderJson")
1807
+ ) {
1808
+ return new Response(null, {
1809
+ status: 204,
1810
+ headers: pageJsonCorsHeaders(request, options.pageJsonCors),
1811
+ });
1812
+ }
1813
+
1814
+ if (
1815
+ options.renderJson !== false &&
1816
+ url.searchParams.has("renderJson") &&
1817
+ request.method === "GET"
1818
+ ) {
1747
1819
  const basePath = url.pathname;
1748
1820
  const cookies: Record<string, string> = {};
1749
1821
  for (const pair of (request.headers.get("cookie") ?? "").split(";")) {
@@ -1758,14 +1830,9 @@ export function createDecoWorkerEntry(
1758
1830
  request,
1759
1831
  };
1760
1832
 
1761
- // Reflect Origin (not literal "*") so credentialed mobile fetches work.
1762
- const corsHeaders: Record<string, string> = {
1763
- "Access-Control-Allow-Origin": request.headers.get("origin") ?? "*",
1764
- "Access-Control-Allow-Credentials": "true",
1765
- "Access-Control-Allow-Methods": "GET, OPTIONS",
1766
- "Access-Control-Allow-Headers": "Content-Type, If-None-Match, Authorization",
1767
- "Access-Control-Expose-Headers": "ETag",
1768
- };
1833
+ // CORS for ?renderJson — off unless the site allow-lists origins via
1834
+ // options.pageJsonCors (native apps need none; browsers get same-origin).
1835
+ const corsHeaders = pageJsonCorsHeaders(request, options.pageJsonCors);
1769
1836
 
1770
1837
  const page = await resolveDecoPage(basePath, matcherCtx);
1771
1838
  if (!page) {
@@ -1838,7 +1905,7 @@ export function createDecoWorkerEntry(
1838
1905
  { status: 404, headers: corsHeaders },
1839
1906
  );
1840
1907
  }
1841
- return withEtag(JSON.stringify(serialized));
1908
+ return withEtag(stringifyWithoutSecrets(serialized));
1842
1909
  }
1843
1910
 
1844
1911
  // Whole page: eager sections are resolved + projected inline; deferred
@@ -1864,11 +1931,15 @@ export function createDecoWorkerEntry(
1864
1931
  },
1865
1932
  );
1866
1933
 
1867
- return withEtag(JSON.stringify({ name: page.name, path: page.path, sections }));
1934
+ return withEtag(stringifyWithoutSecrets({ name: page.name, path: page.path, sections }));
1868
1935
  }
1869
1936
 
1870
1937
  // ?asJson — return resolved page data as JSON (legacy deco compat)
1871
- if (url.searchParams.has("asJson") && request.method === "GET") {
1938
+ if (
1939
+ options.asJson !== false &&
1940
+ url.searchParams.has("asJson") &&
1941
+ request.method === "GET"
1942
+ ) {
1872
1943
  const basePath = url.pathname;
1873
1944
  const cookies: Record<string, string> = {};
1874
1945
  for (const pair of (request.headers.get("cookie") ?? "").split(";")) {