@decocms/tanstack 7.42.0 → 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.42.0",
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.42.0",
28
- "@decocms/blocks-admin": "7.42.0",
29
- "@decocms/blocks-cli": "7.42.0",
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";
@@ -243,6 +244,15 @@ export interface DecoWorkerEntryOptions {
243
244
  */
244
245
  asJson?: boolean;
245
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
+
246
256
  /**
247
257
  * Build a full segment key from the incoming request.
248
258
  *
@@ -873,6 +883,37 @@ let _redirectMapRevision: string | null = null;
873
883
  // Factory
874
884
  // ---------------------------------------------------------------------------
875
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
+
876
917
  /**
877
918
  * Creates a Cloudflare Worker fetch handler that wraps a TanStack Start
878
919
  * server entry with intelligent edge caching.
@@ -1758,6 +1799,18 @@ export function createDecoWorkerEntry(
1758
1799
  // ?renderJson — structured JSON of the page for the mobile app. Lean shape
1759
1800
  // ({ name, path, sections:[{ component, props }] }), each section projected
1760
1801
  // through its `renderJson` export. Wins over ?asJson when both are present.
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
+
1761
1814
  if (
1762
1815
  options.renderJson !== false &&
1763
1816
  url.searchParams.has("renderJson") &&
@@ -1777,14 +1830,9 @@ export function createDecoWorkerEntry(
1777
1830
  request,
1778
1831
  };
1779
1832
 
1780
- // Reflect Origin (not literal "*") so credentialed mobile fetches work.
1781
- const corsHeaders: Record<string, string> = {
1782
- "Access-Control-Allow-Origin": request.headers.get("origin") ?? "*",
1783
- "Access-Control-Allow-Credentials": "true",
1784
- "Access-Control-Allow-Methods": "GET, OPTIONS",
1785
- "Access-Control-Allow-Headers": "Content-Type, If-None-Match, Authorization",
1786
- "Access-Control-Expose-Headers": "ETag",
1787
- };
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);
1788
1836
 
1789
1837
  const page = await resolveDecoPage(basePath, matcherCtx);
1790
1838
  if (!page) {
@@ -1857,7 +1905,7 @@ export function createDecoWorkerEntry(
1857
1905
  { status: 404, headers: corsHeaders },
1858
1906
  );
1859
1907
  }
1860
- return withEtag(JSON.stringify(serialized));
1908
+ return withEtag(stringifyWithoutSecrets(serialized));
1861
1909
  }
1862
1910
 
1863
1911
  // Whole page: eager sections are resolved + projected inline; deferred
@@ -1883,7 +1931,7 @@ export function createDecoWorkerEntry(
1883
1931
  },
1884
1932
  );
1885
1933
 
1886
- return withEtag(JSON.stringify({ name: page.name, path: page.path, sections }));
1934
+ return withEtag(stringifyWithoutSecrets({ name: page.name, path: page.path, sections }));
1887
1935
  }
1888
1936
 
1889
1937
  // ?asJson — return resolved page data as JSON (legacy deco compat)