@decocms/blocks 8.1.0-next.0 → 8.1.0-next.1

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.
Files changed (114) hide show
  1. package/package.json +6 -92
  2. package/src/index.ts +2 -12
  3. package/src/protocol/storage/memory.ts +1 -1
  4. package/src/v8/background.ts +3 -3
  5. package/src/v8/builtins/legacy.ts +1 -1
  6. package/src/v8/cli/index.ts +1 -1
  7. package/src/v8/fetch.ts +8 -27
  8. package/src/v8/remoteLoader.ts +1 -1
  9. package/src/v8/telemetry.ts +1 -1
  10. package/src/cms/applySectionConventions.ts +0 -148
  11. package/src/cms/blockSource.ts +0 -228
  12. package/src/cms/client.ts +0 -85
  13. package/src/cms/deferredTrigger.ts +0 -58
  14. package/src/cms/draftSource.ts +0 -476
  15. package/src/cms/index.ts +0 -180
  16. package/src/cms/loadDecofileDirectory.ts +0 -89
  17. package/src/cms/loader.ts +0 -563
  18. package/src/cms/registry.ts +0 -264
  19. package/src/cms/renderJson.ts +0 -179
  20. package/src/cms/resolve.ts +0 -2583
  21. package/src/cms/schema.ts +0 -1219
  22. package/src/cms/sectionLoaderContext.ts +0 -113
  23. package/src/cms/sectionLoaders.ts +0 -686
  24. package/src/cms/sectionMixins.ts +0 -153
  25. package/src/flags/audience.ts +0 -56
  26. package/src/flags/everyone.ts +0 -29
  27. package/src/flags/flag.ts +0 -15
  28. package/src/flags/multivariate/image.ts +0 -11
  29. package/src/flags/multivariate/message.ts +0 -11
  30. package/src/flags/multivariate/page.ts +0 -16
  31. package/src/flags/multivariate/section.ts +0 -16
  32. package/src/flags/multivariate.ts +0 -1
  33. package/src/flags/types.ts +0 -59
  34. package/src/flags/utils/multivariate.ts +0 -20
  35. package/src/hooks/Image.tsx +0 -314
  36. package/src/hooks/JsonLd.tsx +0 -353
  37. package/src/hooks/LazySection.tsx +0 -121
  38. package/src/hooks/LiveControls.tsx +0 -144
  39. package/src/hooks/Picture.tsx +0 -127
  40. package/src/hooks/RenderSection.tsx +0 -91
  41. package/src/hooks/SectionErrorFallback.tsx +0 -97
  42. package/src/hooks/Stats.tsx +0 -134
  43. package/src/hooks/index.ts +0 -35
  44. package/src/hooks/useLoadMore.ts +0 -100
  45. package/src/matchers/builtins.ts +0 -459
  46. package/src/matchers/countryNames.ts +0 -104
  47. package/src/matchers/override.ts +0 -136
  48. package/src/matchers/posthog.ts +0 -154
  49. package/src/middleware/decoState.ts +0 -55
  50. package/src/middleware/healthMetrics.ts +0 -133
  51. package/src/middleware/hydrationContext.ts +0 -79
  52. package/src/middleware/index.ts +0 -88
  53. package/src/middleware/liveness.ts +0 -21
  54. package/src/middleware/observability.ts +0 -800
  55. package/src/middleware/validateSection.ts +0 -100
  56. package/src/preview/DraftPreviewBadge.tsx +0 -306
  57. package/src/preview/decoMark.ts +0 -11
  58. package/src/preview/index.ts +0 -8
  59. package/src/sdk/abTesting.ts +0 -556
  60. package/src/sdk/analytics.ts +0 -147
  61. package/src/sdk/cacheHeaders.ts +0 -633
  62. package/src/sdk/cacheStorage.ts +0 -287
  63. package/src/sdk/cachedLoader.ts +0 -556
  64. package/src/sdk/clx.ts +0 -5
  65. package/src/sdk/cn.ts +0 -28
  66. package/src/sdk/composite.ts +0 -114
  67. package/src/sdk/cookie.ts +0 -129
  68. package/src/sdk/crypto.ts +0 -199
  69. package/src/sdk/csp.ts +0 -59
  70. package/src/sdk/deepOmit.ts +0 -52
  71. package/src/sdk/detectDevice.ts +0 -45
  72. package/src/sdk/djb2.ts +0 -20
  73. package/src/sdk/encoding.ts +0 -44
  74. package/src/sdk/env.ts +0 -54
  75. package/src/sdk/experiments.ts +0 -314
  76. package/src/sdk/fetchCache.ts +0 -269
  77. package/src/sdk/fetchTimeout.ts +0 -58
  78. package/src/sdk/flags.ts +0 -138
  79. package/src/sdk/http.ts +0 -124
  80. package/src/sdk/index.ts +0 -97
  81. package/src/sdk/inflightTimeout.ts +0 -53
  82. package/src/sdk/instrumentedFetch.ts +0 -362
  83. package/src/sdk/invoke.ts +0 -298
  84. package/src/sdk/logger.ts +0 -304
  85. package/src/sdk/mergeCacheControl.ts +0 -150
  86. package/src/sdk/nonce.ts +0 -46
  87. package/src/sdk/normalizeUrls.ts +0 -91
  88. package/src/sdk/observability.ts +0 -116
  89. package/src/sdk/otel.ts +0 -1118
  90. package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
  91. package/src/sdk/otelAdapters.ts +0 -144
  92. package/src/sdk/otelHttpLog.ts +0 -419
  93. package/src/sdk/otelHttpMeter.ts +0 -532
  94. package/src/sdk/otelHttpTracer.ts +0 -543
  95. package/src/sdk/redirects.ts +0 -336
  96. package/src/sdk/requestContext.ts +0 -281
  97. package/src/sdk/requestContextStorage.browser.ts +0 -43
  98. package/src/sdk/requestContextStorage.ts +0 -35
  99. package/src/sdk/responseCache.ts +0 -82
  100. package/src/sdk/retry.ts +0 -45
  101. package/src/sdk/serverTimings.ts +0 -68
  102. package/src/sdk/signal.ts +0 -42
  103. package/src/sdk/sitemap.ts +0 -160
  104. package/src/sdk/urlRedaction.ts +0 -82
  105. package/src/sdk/urlUtils.ts +0 -183
  106. package/src/sdk/useDevice.ts +0 -89
  107. package/src/sdk/useDeviceContext.tsx +0 -108
  108. package/src/sdk/useId.ts +0 -7
  109. package/src/sdk/useScript.ts +0 -210
  110. package/src/sdk/useSuggestions.ts +0 -188
  111. package/src/sdk/wrapCaughtErrors.ts +0 -107
  112. package/src/setup.ts +0 -120
  113. package/src/types/index.ts +0 -53
  114. package/src/types/widgets.ts +0 -14
@@ -1,82 +0,0 @@
1
- import type { CacheStorage } from "./cacheStorage";
2
- import { decodeBase64 } from "./encoding";
3
-
4
- interface StoredResponse {
5
- status: number;
6
- statusText: string;
7
- headers: [string, string][];
8
- body: string;
9
- }
10
-
11
- /** HTTP serialization belongs to the cache consumer, independent of its storage adapter. */
12
- export function createResponseCache(storage: CacheStorage, scope: string) {
13
- const keyFor = (request: Request) => JSON.stringify([scope, "responses", request.url]);
14
- return {
15
- async match(request: Request): Promise<Response | undefined> {
16
- const raw = await storage.get(keyFor(request));
17
- if (raw === null) return undefined;
18
- try {
19
- const stored = JSON.parse(raw) as StoredResponse;
20
- // Uint8Array.from(string, mapper) creates a large temporary iterable array.
21
- // Decode straight into the byte buffer to keep cache hits proportional to body size.
22
- const body = decodeBase64(stored.body);
23
- return new Response([204, 205, 304].includes(stored.status) ? null : body, {
24
- status: stored.status,
25
- statusText: stored.statusText,
26
- headers: stored.headers,
27
- });
28
- } catch {
29
- return undefined;
30
- }
31
- },
32
- async put(request: Request, response: Response): Promise<void> {
33
- const cc = response.headers.get("Cache-Control") ?? "";
34
- const ttl = Number(/(?:^|[,\s])max-age=(\d+)/i.exec(cc)?.[1] ?? 0);
35
- if (!ttl || /(?:private|no-store|no-cache)/i.test(cc) || response.headers.has("set-cookie"))
36
- return;
37
- const expiresAt = Date.now() + ttl * 1000;
38
- // Bound buffering before serialization; an oversized page can still stream to its caller.
39
- const chunks: Uint8Array[] = [];
40
- let size = 0;
41
- const reader = response.body?.getReader();
42
- if (reader) {
43
- while (true) {
44
- const { done, value } = await reader.read();
45
- if (done) break;
46
- size += value.byteLength;
47
- if (size > 8 * 1024 * 1024) {
48
- void reader.cancel().catch(() => {});
49
- return;
50
- }
51
- chunks.push(value);
52
- }
53
- }
54
- const bytes = new Uint8Array(size);
55
- let offset = 0;
56
- for (const chunk of chunks) {
57
- bytes.set(chunk, offset);
58
- offset += chunk.byteLength;
59
- }
60
- let binary = "";
61
- for (let i = 0; i < bytes.length; i += 8192) {
62
- binary += String.fromCharCode(...bytes.subarray(i, i + 8192));
63
- }
64
- const value: StoredResponse = {
65
- status: response.status,
66
- statusText: response.statusText,
67
- headers: [...response.headers.entries()],
68
- body: btoa(binary),
69
- };
70
- try {
71
- await storage.set(keyFor(request), JSON.stringify(value), expiresAt);
72
- } catch {
73
- /* Cache writes must not fail the response or its waitUntil task. */
74
- }
75
- },
76
- async delete(request: Request): Promise<boolean> {
77
- // KV deletion has no "existed" result. Report an accepted deletion, avoiding an extra read.
78
- await storage.delete(keyFor(request));
79
- return true;
80
- },
81
- };
82
- }
package/src/sdk/retry.ts DELETED
@@ -1,45 +0,0 @@
1
- export const CONNECTION_CLOSED_MESSAGE = "connection closed before message completed";
2
-
3
- function sleep(ms: number): Promise<void> {
4
- return new Promise((resolve) => setTimeout(resolve, ms));
5
- }
6
-
7
- /**
8
- * Simple retry utility — replaces cockatiel to avoid module-level AbortController
9
- * (cockatiel's abort.js creates new AbortController() at module scope, which is
10
- * forbidden in Cloudflare Workers global scope).
11
- *
12
- * Retries up to maxAttempts when the error matches the predicate.
13
- * Uses exponential backoff: delay = min(initialDelay * exponent^attempt, maxDelay).
14
- */
15
- export function retryExceptionOr500() {
16
- return {
17
- execute: async <T>(fn: () => Promise<T>): Promise<T> => {
18
- const maxAttempts = 3;
19
- const initialDelay = 100;
20
- const maxDelay = 5000;
21
- const exponent = 2;
22
-
23
- let lastErr: unknown;
24
- for (let attempt = 0; attempt < maxAttempts; attempt++) {
25
- try {
26
- return await fn();
27
- } catch (err) {
28
- const message = err instanceof Error ? err.message : String(err);
29
- if (!message.includes(CONNECTION_CLOSED_MESSAGE)) {
30
- throw err;
31
- }
32
- lastErr = err;
33
- try {
34
- console.error("retrying...", err);
35
- } catch (_) {}
36
- if (attempt < maxAttempts - 1) {
37
- const delay = Math.min(initialDelay * Math.pow(exponent, attempt), maxDelay);
38
- await sleep(delay);
39
- }
40
- }
41
- }
42
- throw lastErr;
43
- },
44
- };
45
- }
@@ -1,68 +0,0 @@
1
- /**
2
- * Server-Timing header builder.
3
- *
4
- * Tracks named timing entries during a request and serializes them
5
- * into the `Server-Timing` HTTP header for visibility in DevTools.
6
- *
7
- * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing
8
- *
9
- * @example
10
- * ```ts
11
- * const timings = createServerTimings();
12
- * const end = timings.start("resolve-cms");
13
- * await resolvePage();
14
- * end();
15
- * response.headers.set("Server-Timing", timings.toHeader());
16
- * ```
17
- */
18
- export interface ServerTimings {
19
- /** Start a named timing. Returns a function to call when done. */
20
- start(name: string, description?: string): () => void;
21
- /** Record a completed timing with a known duration. */
22
- record(name: string, durationMs: number, description?: string): void;
23
- /** Serialize all timings to a `Server-Timing` header value. */
24
- toHeader(): string;
25
- /** Get all recorded entries for diagnostics. */
26
- entries(): TimingEntry[];
27
- }
28
-
29
- export interface TimingEntry {
30
- name: string;
31
- durationMs: number;
32
- description?: string;
33
- }
34
-
35
- export function createServerTimings(): ServerTimings {
36
- const timingEntries: TimingEntry[] = [];
37
-
38
- return {
39
- start(name: string, description?: string) {
40
- const startTime = performance.now();
41
- return () => {
42
- const durationMs = performance.now() - startTime;
43
- timingEntries.push({ name, durationMs, description });
44
- };
45
- },
46
-
47
- record(name: string, durationMs: number, description?: string) {
48
- timingEntries.push({ name, durationMs, description });
49
- },
50
-
51
- toHeader(): string {
52
- return timingEntries
53
- .map((entry) => {
54
- let value = entry.name;
55
- if (entry.description) {
56
- value += `;desc="${entry.description}"`;
57
- }
58
- value += `;dur=${entry.durationMs.toFixed(1)}`;
59
- return value;
60
- })
61
- .join(", ");
62
- },
63
-
64
- entries() {
65
- return [...timingEntries];
66
- },
67
- };
68
- }
package/src/sdk/signal.ts DELETED
@@ -1,42 +0,0 @@
1
- /**
2
- * Reactive signal backed by @tanstack/store.
3
- *
4
- * Drop-in replacement for the preact signals shim.
5
- * Preserves the .value getter/setter API so consumers don't need to change.
6
- *
7
- * For React components that need to re-render on state changes,
8
- * use the useStore() hook from @tanstack/react-store:
9
- *
10
- * import { useStore } from "@tanstack/react-store";
11
- * const value = useStore(mySignal.store);
12
- */
13
- import { Store } from "@tanstack/store";
14
-
15
- export interface ReactiveSignal<T> {
16
- readonly store: Store<T>;
17
- value: T;
18
- peek(): T;
19
- subscribe(fn: () => void): () => void;
20
- }
21
-
22
- export function signal<T>(initialValue: T): ReactiveSignal<T> {
23
- const store = new Store<T>(initialValue);
24
-
25
- return {
26
- store,
27
- get value(): T {
28
- return store.state;
29
- },
30
- set value(v: T) {
31
- store.setState(() => v);
32
- },
33
- peek(): T {
34
- return store.state;
35
- },
36
- subscribe(fn: () => void): () => void {
37
- const sub = store.subscribe(() => fn());
38
- return () => sub.unsubscribe();
39
- },
40
- };
41
- }
42
-
@@ -1,160 +0,0 @@
1
- /**
2
- * Sitemap generation utilities.
3
- *
4
- * Provides XML sitemap generation from CMS page blocks and arbitrary
5
- * URL lists. Designed to be composed with commerce-specific sitemap
6
- * sources (VTEX, Shopify) in a TanStack Start API route.
7
- *
8
- * @example
9
- * ```ts
10
- * // src/routes/sitemap[.]xml.ts
11
- * import { createAPIFileRoute } from "@tanstack/react-start/api";
12
- * import { getCMSSitemapEntries, generateSitemapXml } from "@decocms/start/sdk/sitemap";
13
- * import { getVtexSitemapEntries } from "@decocms/apps/vtex/utils/sitemap";
14
- *
15
- * export const APIRoute = createAPIFileRoute("/sitemap.xml")({
16
- * GET: async ({ request }) => {
17
- * const origin = new URL(request.url).origin;
18
- * const cmsEntries = getCMSSitemapEntries(origin);
19
- * const vtexEntries = await getVtexSitemapEntries(origin);
20
- * const xml = generateSitemapXml([...cmsEntries, ...vtexEntries]);
21
- * return new Response(xml, {
22
- * headers: {
23
- * "Content-Type": "application/xml",
24
- * "Cache-Control": "public, s-maxage=3600, stale-while-revalidate=86400",
25
- * },
26
- * });
27
- * },
28
- * });
29
- * ```
30
- */
31
-
32
- import { getAllPages, loadBlocks } from "../cms/loader";
33
-
34
- // -------------------------------------------------------------------------
35
- // Types
36
- // -------------------------------------------------------------------------
37
-
38
- export interface SitemapEntry {
39
- loc: string;
40
- lastmod?: string;
41
- changefreq?: "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never";
42
- priority?: number;
43
- }
44
-
45
- export interface SitemapOptions {
46
- /** Maximum entries per sitemap (Google limit is 50,000). @default 50000 */
47
- maxEntries?: number;
48
- }
49
-
50
- export interface CMSSitemapOptions {
51
- /** Default changefreq for non-home pages. @default "weekly" */
52
- defaultChangefreq?: SitemapEntry["changefreq"];
53
- /** Default priority for non-home pages (0.0–1.0). @default 0.7 */
54
- defaultPriority?: number;
55
- /** Changefreq for the home page. @default "daily" */
56
- homeChangefreq?: SitemapEntry["changefreq"];
57
- /** Priority for the home page (0.0–1.0). @default 1.0 */
58
- homePriority?: number;
59
- }
60
-
61
- // -------------------------------------------------------------------------
62
- // CMS page entries
63
- // -------------------------------------------------------------------------
64
-
65
- /**
66
- * Extract sitemap entries from CMS page blocks.
67
- *
68
- * Reads all pages from the block store and generates URLs from their
69
- * path patterns (excluding wildcard-only patterns like `/*`).
70
- */
71
- export function getCMSSitemapEntries(origin: string, options?: CMSSitemapOptions): SitemapEntry[] {
72
- const pages = getAllPages();
73
- const entries: SitemapEntry[] = [];
74
- const today = new Date().toISOString().split("T")[0];
75
-
76
- const defaultChangefreq = options?.defaultChangefreq ?? "weekly";
77
- const defaultPriority = options?.defaultPriority ?? 0.7;
78
- const homeChangefreq = options?.homeChangefreq ?? "daily";
79
- const homePriority = options?.homePriority ?? 1.0;
80
-
81
- for (const { page } of pages) {
82
- if (!page.path) continue;
83
-
84
- if (page.path.includes("*") || page.path.includes(":")) continue;
85
-
86
- const isHome = page.path === "/";
87
- const loc = `${origin}${isHome ? "" : page.path}`;
88
- entries.push({
89
- loc: loc || origin,
90
- lastmod: today,
91
- changefreq: isHome ? homeChangefreq : defaultChangefreq,
92
- priority: isHome ? homePriority : defaultPriority,
93
- });
94
- }
95
-
96
- return entries;
97
- }
98
-
99
- // -------------------------------------------------------------------------
100
- // XML generation
101
- // -------------------------------------------------------------------------
102
-
103
- function escapeXml(str: string): string {
104
- return str
105
- .replace(/&/g, "&amp;")
106
- .replace(/</g, "&lt;")
107
- .replace(/>/g, "&gt;")
108
- .replace(/"/g, "&quot;")
109
- .replace(/'/g, "&apos;");
110
- }
111
-
112
- /**
113
- * Generate a sitemap XML string from entries.
114
- */
115
- export function generateSitemapXml(entries: SitemapEntry[], options?: SitemapOptions): string {
116
- const max = options?.maxEntries ?? 50000;
117
- const limited = entries.slice(0, max);
118
-
119
- const urls = limited.map((entry) => {
120
- let url = ` <url>\n <loc>${escapeXml(entry.loc)}</loc>`;
121
- if (entry.lastmod) url += `\n <lastmod>${entry.lastmod}</lastmod>`;
122
- if (entry.changefreq) url += `\n <changefreq>${entry.changefreq}</changefreq>`;
123
- if (entry.priority != null) url += `\n <priority>${entry.priority.toFixed(1)}</priority>`;
124
- url += "\n </url>";
125
- return url;
126
- });
127
-
128
- return [
129
- '<?xml version="1.0" encoding="UTF-8"?>',
130
- '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
131
- ...urls,
132
- "</urlset>",
133
- ].join("\n");
134
- }
135
-
136
- /**
137
- * Generate a sitemap index XML for splitting large sitemaps.
138
- *
139
- * @example
140
- * ```ts
141
- * const index = generateSitemapIndexXml([
142
- * `${origin}/sitemap-pages.xml`,
143
- * `${origin}/sitemap-products.xml`,
144
- * ]);
145
- * ```
146
- */
147
- export function generateSitemapIndexXml(sitemapUrls: string[]): string {
148
- const today = new Date().toISOString().split("T")[0];
149
- const sitemaps = sitemapUrls.map(
150
- (url) =>
151
- ` <sitemap>\n <loc>${escapeXml(url)}</loc>\n <lastmod>${today}</lastmod>\n </sitemap>`,
152
- );
153
-
154
- return [
155
- '<?xml version="1.0" encoding="UTF-8"?>',
156
- '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
157
- ...sitemaps,
158
- "</sitemapindex>",
159
- ].join("\n");
160
- }
@@ -1,82 +0,0 @@
1
- /**
2
- * URL redaction for observability surfaces.
3
- *
4
- * Strips secrets from outbound URLs before they land on a log line or a
5
- * span attribute. The redacted output preserves enough of the URL for
6
- * operators to recognize the call (host, path, which query keys were
7
- * present) without leaking values that may carry tokens, session IDs,
8
- * personal data, or other regulated content.
9
- *
10
- * Why a separate helper (instead of doing it in the ingestor):
11
- *
12
- * - Spans land in CF Workers Tracing before they ever reach our
13
- * ingestor; once the unredacted value is stamped on `http.url`, it's
14
- * in the CF dashboard whether we like it or not.
15
- * - Logs go through `console.log` which Cloudflare captures into
16
- * Workers Logs immediately — same problem.
17
- * - The ingestor's `redactSensitiveHeaders` only covers headers, not
18
- * URLs. Redacting URLs at the emit site is the only way to avoid
19
- * landing a token on a platform we don't control.
20
- *
21
- * Redaction rules:
22
- *
23
- * - Strip userinfo (`https://user:pass@host`) entirely.
24
- * - Drop the fragment (`#...`) — fragments are client-side only and
25
- * may carry SPA state that includes tokens.
26
- * - Replace every query value with `"REDACTED"` unless its key is in
27
- * `keepQueryKeys`. Empty values stay empty so dashboards can still
28
- * distinguish `?foo=` from `?foo=secret`.
29
- * - Preserve the host and path verbatim. Path normalization (e.g.
30
- * collapsing `/p/[slug]`) lives in `observability.ts:normalizePath`
31
- * on the metrics side and is intentionally NOT bundled here.
32
- * - Unparseable inputs fall back to a defensive substring before the
33
- * `?`, so a malformed URL never accidentally surfaces a query string.
34
- */
35
-
36
- const REDACTED = "REDACTED";
37
-
38
- export interface RedactUrlOptions {
39
- /**
40
- * Query parameter names whose value should be left intact. Useful for
41
- * structural / debugging params like `page`, `sort`, `view` that don't
42
- * carry secrets. Names are case-sensitive (matches `URLSearchParams`).
43
- *
44
- * By default this is empty — every value is redacted.
45
- */
46
- keepQueryKeys?: ReadonlyArray<string>;
47
- }
48
-
49
- export function redactUrl(input: string, options: RedactUrlOptions = {}): string {
50
- const keep = options.keepQueryKeys ? new Set(options.keepQueryKeys) : null;
51
-
52
- try {
53
- const u = new URL(input);
54
- // userinfo — never preserve.
55
- u.username = "";
56
- u.password = "";
57
- // fragment — drop. Browsers don't send it, but it can sneak into
58
- // server-side URLs via misconfigured proxies.
59
- u.hash = "";
60
-
61
- // searchParams.set() mutates URLSearchParams in place. Iterating a
62
- // snapshot via [...entries()] is the supported way to avoid mutating
63
- // the iterator we're walking.
64
- const entries = [...u.searchParams.entries()];
65
- for (const [key, value] of entries) {
66
- if (keep && keep.has(key)) continue;
67
- // Preserve empty values (`?k=` stays `?k=`) so dashboards can still
68
- // distinguish empty from redacted-with-content.
69
- if (value.length === 0) continue;
70
- u.searchParams.set(key, REDACTED);
71
- }
72
-
73
- return u.toString();
74
- } catch {
75
- // Unparseable URL — defensively drop everything from the first `?`
76
- // OR `#` onwards. Either can carry a secret (`?token=…`,
77
- // `#access_token=…`); cutting at whichever appears first preserves
78
- // the most context while leaking nothing.
79
- const idx = input.search(/[?#]/);
80
- return idx >= 0 ? input.slice(0, idx) : input;
81
- }
82
- }
@@ -1,183 +0,0 @@
1
- /**
2
- * URL utility functions.
3
- *
4
- * Handles UTM parameter stripping for cache-friendly URLs,
5
- * canonical URL generation, and other URL manipulation.
6
- */
7
-
8
- const UTM_PARAMS = new Set([
9
- "utm_source",
10
- "utm_medium",
11
- "utm_campaign",
12
- "utm_term",
13
- "utm_content",
14
- "utmi_campaign",
15
- "utmi_page",
16
- "utmi_part",
17
- "fbclid",
18
- "gclid",
19
- "gclsrc",
20
- "dclid",
21
- "msclkid",
22
- "twclid",
23
- "li_fat_id",
24
- "mc_cid",
25
- "mc_eid",
26
- "ttclid",
27
- "srsltid",
28
- // GA4's newer campaign params. The five classic `utm_*` above predate them,
29
- // so a GA4-tagged campaign was still fragmenting the cache key.
30
- "utm_id",
31
- "utm_source_platform",
32
- "utm_creative_format",
33
- "utm_marketing_tactic",
34
- // Google Ads click ids that replaced bare `gclid` for privacy-restricted
35
- // traffic (iOS/Safari), plus the source tag now appended to essentially every
36
- // Google Ads click. Measured on a production storefront: each of these missed
37
- // the edge cache on 3/3 routes probed, deterministically.
38
- "gad_source",
39
- "gad_campaignid",
40
- "gbraid",
41
- "wbraid",
42
- // Google Analytics cross-domain linker. Sites already strip this by hand in
43
- // client code, which is the tell that it shows up in real URLs.
44
- "_gl",
45
- "igshid",
46
- "epik",
47
- "mkt_tok",
48
- "yclid",
49
- ]);
50
-
51
- /**
52
- * Register additional tracking parameters to strip from cache keys.
53
- * Useful for proprietary attribution params (e.g., custom analytics tags).
54
- */
55
- export function registerTrackingParam(param: string): void {
56
- UTM_PARAMS.add(param.toLowerCase());
57
- }
58
-
59
- /**
60
- * Register multiple additional tracking parameters at once.
61
- */
62
- export function registerTrackingParams(params: string[]): void {
63
- for (const param of params) {
64
- UTM_PARAMS.add(param.toLowerCase());
65
- }
66
- }
67
-
68
- /**
69
- * Whether a search param is a known tracking/attribution param.
70
- *
71
- * Exposed so cache-key builders can filter params in place without having to
72
- * round-trip through a `URL` (server-fn payloads carry bare relative paths) and
73
- * without keeping a second copy of the list.
74
- */
75
- export function isTrackingParam(param: string): boolean {
76
- return UTM_PARAMS.has(param.toLowerCase());
77
- }
78
-
79
- /**
80
- * Strip UTM and tracking parameters from a URL.
81
- *
82
- * Used to normalize URLs for caching -- two requests that differ
83
- * only in UTM params should hit the same cache entry.
84
- *
85
- * @example
86
- * ```ts
87
- * stripTrackingParams("https://example.com/shoes?color=red&utm_source=google")
88
- * // => "https://example.com/shoes?color=red"
89
- * ```
90
- */
91
- export function stripTrackingParams(urlString: string): string {
92
- try {
93
- const url = new URL(urlString);
94
- let changed = false;
95
-
96
- for (const param of [...url.searchParams.keys()]) {
97
- if (UTM_PARAMS.has(param.toLowerCase())) {
98
- url.searchParams.delete(param);
99
- changed = true;
100
- }
101
- }
102
-
103
- return changed ? url.toString() : urlString;
104
- } catch {
105
- return urlString;
106
- }
107
- }
108
-
109
- /**
110
- * Strip tracking params from a URL and return just the pathname + non-UTM search.
111
- * Useful for cache key generation.
112
- */
113
- export function cleanPathForCacheKey(urlString: string): string {
114
- try {
115
- const url = new URL(urlString);
116
-
117
- for (const param of [...url.searchParams.keys()]) {
118
- if (UTM_PARAMS.has(param.toLowerCase())) {
119
- url.searchParams.delete(param);
120
- }
121
- }
122
-
123
- const search = url.searchParams.toString();
124
- return search ? `${url.pathname}?${search}` : url.pathname;
125
- } catch {
126
- return urlString;
127
- }
128
- }
129
-
130
- /**
131
- * Build a canonical URL from a request, stripping tracking params
132
- * and normalizing the protocol/host.
133
- *
134
- * @param request - The incoming request
135
- * @param baseUrl - Override origin (useful when behind a proxy)
136
- */
137
- export function canonicalUrl(request: Request, baseUrl?: string): string {
138
- const url = new URL(request.url);
139
- const origin = baseUrl ?? url.origin;
140
-
141
- for (const param of [...url.searchParams.keys()]) {
142
- if (UTM_PARAMS.has(param.toLowerCase())) {
143
- url.searchParams.delete(param);
144
- }
145
- }
146
-
147
- const search = url.searchParams.toString();
148
- const path = search ? `${url.pathname}?${search}` : url.pathname;
149
-
150
- return `${origin}${path}`;
151
- }
152
-
153
- /**
154
- * Extract the pathname from an absolute or relative URL, stripping query
155
- * string and hash. Falls back to manual slicing when `new URL()` would
156
- * throw (relative URL without a base). Used by operation routers that
157
- * need to match against URL structure without caring about the host.
158
- */
159
- export function extractPathname(url: string): string {
160
- try {
161
- return new URL(url).pathname;
162
- } catch {
163
- const qs = url.indexOf("?");
164
- const hash = url.indexOf("#");
165
- const end = [qs, hash].filter((i) => i >= 0).sort((a, b) => a - b)[0];
166
- return end === undefined ? url : url.slice(0, end);
167
- }
168
- }
169
-
170
- /**
171
- * Check if a URL has any tracking/UTM parameters.
172
- */
173
- export function hasTrackingParams(urlString: string): boolean {
174
- try {
175
- const url = new URL(urlString);
176
- for (const param of url.searchParams.keys()) {
177
- if (UTM_PARAMS.has(param.toLowerCase())) return true;
178
- }
179
- return false;
180
- } catch {
181
- return false;
182
- }
183
- }