@preprio/toolkit 1.0.0-rc.3 → 1.0.0-rc.5

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/README.md CHANGED
@@ -179,6 +179,8 @@ const config = {
179
179
  };
180
180
  ```
181
181
 
182
+ Test fixtures written by hand against the generated types then need an `_id` too. Use `createPreprIds({ optionalInTypes: true })` instead to type it as optional, see [Hand-written test data](./docs/live-preview.md#hand-written-test-data).
183
+
182
184
  No Codegen? See [the other two ways](./docs/live-preview.md#preparing-your-queries).
183
185
 
184
186
  **5. Run your query results through the live preview helper**, with your preview flag as `enabled`:
@@ -34,7 +34,7 @@ __export(astro_exports, {
34
34
  module.exports = __toCommonJS(astro_exports);
35
35
 
36
36
  // src/version.ts
37
- var VERSION = "1.0.0-rc.3";
37
+ var VERSION = "1.0.0-rc.5";
38
38
 
39
39
  // src/core/constants.ts
40
40
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1480,34 +1480,53 @@ function entryIdOf(element) {
1480
1480
  const first = element.items[0];
1481
1481
  return isObject(first) && typeof first._id === "string" ? first._id : void 0;
1482
1482
  }
1483
- function annotate(value, ctx) {
1484
- if (!ctx.entryId) return value;
1485
- const { cleaned, encoded } = (0, import_stega4.vercelStegaSplit)(value);
1486
- if (!encoded) return value;
1483
+ function encodeLink(payload) {
1484
+ try {
1485
+ return (0, import_stega4.legacyStegaEncode)(payload);
1486
+ } catch {
1487
+ return (0, import_stega4.vercelStegaEncode)(payload);
1488
+ }
1489
+ }
1490
+ function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
1487
1491
  let payload;
1488
1492
  try {
1489
1493
  payload = (0, import_stega4.vercelStegaDecode)(encoded);
1490
1494
  } catch {
1491
- return value;
1495
+ return "";
1492
1496
  }
1493
- if (typeof payload?.href !== "string") return value;
1497
+ if (typeof payload?.href !== "string") return "";
1494
1498
  let url;
1495
1499
  try {
1496
1500
  url = new URL(payload.href);
1497
1501
  } catch {
1498
- return value;
1502
+ return "";
1499
1503
  }
1500
- if (url.searchParams.has(ENTRY_PARAM)) return value;
1501
- url.searchParams.set(ENTRY_PARAM, ctx.entryId);
1504
+ if (url.searchParams.has(ENTRY_PARAM)) return "";
1505
+ url.searchParams.set(ENTRY_PARAM, entryId);
1502
1506
  const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
1503
- if (!url.searchParams.has("field") && ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) && itemId === ctx.entryOwnerId) {
1504
- url.searchParams.set("field", ctx.entryField);
1507
+ if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
1508
+ url.searchParams.set("field", entryField);
1505
1509
  }
1506
- return (0, import_stega4.vercelStegaCombine)(
1507
- cleaned,
1508
- { ...payload, href: url.toString() },
1509
- false
1510
- );
1510
+ return encodeLink({ ...payload, href: url.toString() });
1511
+ }
1512
+ var MAX_REWRITTEN = 1e4;
1513
+ var rewritten = /* @__PURE__ */ new Map();
1514
+ function annotate(value, ctx) {
1515
+ if (!ctx.entryId) return value;
1516
+ const { cleaned, encoded } = (0, import_stega4.vercelStegaSplit)(value);
1517
+ if (!encoded) return value;
1518
+ const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
1519
+ const key = `${ctx.entryId}
1520
+ ${entryField}
1521
+ ${ctx.entryOwnerId ?? ""}
1522
+ ${encoded}`;
1523
+ let link = rewritten.get(key);
1524
+ if (link === void 0) {
1525
+ link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
1526
+ if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
1527
+ rewritten.set(key, link);
1528
+ }
1529
+ return link ? cleaned + link : value;
1511
1530
  }
1512
1531
  function walk(value, selections, ctx) {
1513
1532
  if (typeof value === "string") return annotate(value, ctx);
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "1.0.0-rc.3";
2
+ var VERSION = "1.0.0-rc.5";
3
3
 
4
4
  // src/core/constants.ts
5
5
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1438,8 +1438,9 @@ async function readLiveLogFromRequest(request) {
1438
1438
 
1439
1439
  // src/core/live-preview/edit-links.ts
1440
1440
  import {
1441
- vercelStegaCombine as vercelStegaCombine2,
1441
+ legacyStegaEncode,
1442
1442
  vercelStegaDecode as vercelStegaDecode2,
1443
+ vercelStegaEncode,
1443
1444
  vercelStegaSplit as vercelStegaSplit4
1444
1445
  } from "@vercel/stega";
1445
1446
  var ENTRY_PARAM = "entry";
@@ -1453,34 +1454,53 @@ function entryIdOf(element) {
1453
1454
  const first = element.items[0];
1454
1455
  return isObject(first) && typeof first._id === "string" ? first._id : void 0;
1455
1456
  }
1456
- function annotate(value, ctx) {
1457
- if (!ctx.entryId) return value;
1458
- const { cleaned, encoded } = vercelStegaSplit4(value);
1459
- if (!encoded) return value;
1457
+ function encodeLink(payload) {
1458
+ try {
1459
+ return legacyStegaEncode(payload);
1460
+ } catch {
1461
+ return vercelStegaEncode(payload);
1462
+ }
1463
+ }
1464
+ function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
1460
1465
  let payload;
1461
1466
  try {
1462
1467
  payload = vercelStegaDecode2(encoded);
1463
1468
  } catch {
1464
- return value;
1469
+ return "";
1465
1470
  }
1466
- if (typeof payload?.href !== "string") return value;
1471
+ if (typeof payload?.href !== "string") return "";
1467
1472
  let url;
1468
1473
  try {
1469
1474
  url = new URL(payload.href);
1470
1475
  } catch {
1471
- return value;
1476
+ return "";
1472
1477
  }
1473
- if (url.searchParams.has(ENTRY_PARAM)) return value;
1474
- url.searchParams.set(ENTRY_PARAM, ctx.entryId);
1478
+ if (url.searchParams.has(ENTRY_PARAM)) return "";
1479
+ url.searchParams.set(ENTRY_PARAM, entryId);
1475
1480
  const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
1476
- if (!url.searchParams.has("field") && ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) && itemId === ctx.entryOwnerId) {
1477
- url.searchParams.set("field", ctx.entryField);
1481
+ if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
1482
+ url.searchParams.set("field", entryField);
1478
1483
  }
1479
- return vercelStegaCombine2(
1480
- cleaned,
1481
- { ...payload, href: url.toString() },
1482
- false
1483
- );
1484
+ return encodeLink({ ...payload, href: url.toString() });
1485
+ }
1486
+ var MAX_REWRITTEN = 1e4;
1487
+ var rewritten = /* @__PURE__ */ new Map();
1488
+ function annotate(value, ctx) {
1489
+ if (!ctx.entryId) return value;
1490
+ const { cleaned, encoded } = vercelStegaSplit4(value);
1491
+ if (!encoded) return value;
1492
+ const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
1493
+ const key = `${ctx.entryId}
1494
+ ${entryField}
1495
+ ${ctx.entryOwnerId ?? ""}
1496
+ ${encoded}`;
1497
+ let link = rewritten.get(key);
1498
+ if (link === void 0) {
1499
+ link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
1500
+ if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
1501
+ rewritten.set(key, link);
1502
+ }
1503
+ return link ? cleaned + link : value;
1484
1504
  }
1485
1505
  function walk(value, selections, ctx) {
1486
1506
  if (typeof value === "string") return annotate(value, ctx);
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "1.0.0-rc.3";
2
+ var VERSION = "1.0.0-rc.5";
3
3
 
4
4
  // src/core/features.ts
5
5
  function isEnabled(config) {
@@ -2,7 +2,7 @@ import {
2
2
  ALL_FEATURES_ENABLED,
3
3
  VERSION,
4
4
  resolveFeatures
5
- } from "./chunk-EJDHVIFU.js";
5
+ } from "./chunk-A4J3JRMY.js";
6
6
  import {
7
7
  createIframeBridge,
8
8
  isLocale,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  VERSION,
3
3
  resolveFeatures
4
- } from "./chunk-EJDHVIFU.js";
4
+ } from "./chunk-A4J3JRMY.js";
5
5
 
6
6
  // src/core/server.ts
7
7
  var PreprError = class extends Error {
package/dist/index.cjs CHANGED
@@ -51,7 +51,7 @@ __export(src_exports, {
51
51
  module.exports = __toCommonJS(src_exports);
52
52
 
53
53
  // src/version.ts
54
- var VERSION = "1.0.0-rc.3";
54
+ var VERSION = "1.0.0-rc.5";
55
55
 
56
56
  // src/core/features.ts
57
57
  function isEnabled(config) {
@@ -5018,34 +5018,53 @@ function entryIdOf(element) {
5018
5018
  const first = element.items[0];
5019
5019
  return isObject(first) && typeof first._id === "string" ? first._id : void 0;
5020
5020
  }
5021
- function annotate(value, ctx) {
5022
- if (!ctx.entryId) return value;
5023
- const { cleaned, encoded } = (0, import_stega8.vercelStegaSplit)(value);
5024
- if (!encoded) return value;
5021
+ function encodeLink(payload) {
5022
+ try {
5023
+ return (0, import_stega8.legacyStegaEncode)(payload);
5024
+ } catch {
5025
+ return (0, import_stega8.vercelStegaEncode)(payload);
5026
+ }
5027
+ }
5028
+ function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
5025
5029
  let payload;
5026
5030
  try {
5027
5031
  payload = (0, import_stega8.vercelStegaDecode)(encoded);
5028
5032
  } catch {
5029
- return value;
5033
+ return "";
5030
5034
  }
5031
- if (typeof payload?.href !== "string") return value;
5035
+ if (typeof payload?.href !== "string") return "";
5032
5036
  let url;
5033
5037
  try {
5034
5038
  url = new URL(payload.href);
5035
5039
  } catch {
5036
- return value;
5040
+ return "";
5037
5041
  }
5038
- if (url.searchParams.has(ENTRY_PARAM)) return value;
5039
- url.searchParams.set(ENTRY_PARAM, ctx.entryId);
5042
+ if (url.searchParams.has(ENTRY_PARAM)) return "";
5043
+ url.searchParams.set(ENTRY_PARAM, entryId);
5040
5044
  const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
5041
- if (!url.searchParams.has("field") && ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) && itemId === ctx.entryOwnerId) {
5042
- url.searchParams.set("field", ctx.entryField);
5045
+ if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
5046
+ url.searchParams.set("field", entryField);
5043
5047
  }
5044
- return (0, import_stega8.vercelStegaCombine)(
5045
- cleaned,
5046
- { ...payload, href: url.toString() },
5047
- false
5048
- );
5048
+ return encodeLink({ ...payload, href: url.toString() });
5049
+ }
5050
+ var MAX_REWRITTEN = 1e4;
5051
+ var rewritten = /* @__PURE__ */ new Map();
5052
+ function annotate(value, ctx) {
5053
+ if (!ctx.entryId) return value;
5054
+ const { cleaned, encoded } = (0, import_stega8.vercelStegaSplit)(value);
5055
+ if (!encoded) return value;
5056
+ const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
5057
+ const key = `${ctx.entryId}
5058
+ ${entryField}
5059
+ ${ctx.entryOwnerId ?? ""}
5060
+ ${encoded}`;
5061
+ let link = rewritten.get(key);
5062
+ if (link === void 0) {
5063
+ link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
5064
+ if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
5065
+ rewritten.set(key, link);
5066
+ }
5067
+ return link ? cleaned + link : value;
5049
5068
  }
5050
5069
  function walk(value, selections, ctx) {
5051
5070
  if (typeof value === "string") return annotate(value, ctx);
@@ -5405,6 +5424,7 @@ function startHtmlLivePreview(options = {}) {
5405
5424
  strictMode: () => options.strictMode
5406
5425
  });
5407
5426
  bridge.start();
5427
+ const debounceMs = typeof options.syncDebounceMs === "number" && Number.isFinite(options.syncDebounceMs) && options.syncDebounceMs >= 0 ? options.syncDebounceMs : SYNC_DEBOUNCE_MS;
5408
5428
  let timer = null;
5409
5429
  let inFlight = false;
5410
5430
  let pending = null;
@@ -5446,7 +5466,7 @@ function startHtmlLivePreview(options = {}) {
5446
5466
  timer = null;
5447
5467
  if (inFlight) pending = log;
5448
5468
  else void render(log);
5449
- }, SYNC_DEBOUNCE_MS);
5469
+ }, debounceMs);
5450
5470
  };
5451
5471
  const offLog = store.onLogChange((log) => {
5452
5472
  if (log.length) schedule(log);
package/dist/index.d.cts CHANGED
@@ -5,7 +5,7 @@ export { g as PreprEnvironment, h as PreprEventType, i as PreprFeatureConfig, j
5
5
  * Package version; must match package.json. Lives in its own leaf module so
6
6
  * core can import it without cycling through the barrel.
7
7
  */
8
- declare const VERSION = "1.0.0-rc.3";
8
+ declare const VERSION = "1.0.0-rc.5";
9
9
 
10
10
  /**
11
11
  * Resolve a `PreprFeatures` bag to plain booleans, defaulting every omitted key
@@ -447,6 +447,13 @@ interface HtmlLivePreviewOptions {
447
447
  allowedEditorOrigins?: string[];
448
448
  /** See the toolbar's `strictMode`. */
449
449
  strictMode?: boolean;
450
+ /**
451
+ * How long typing must pause, in milliseconds, before the page is
452
+ * requested with the unsaved changes. Default 150. Lower shows changes
453
+ * sooner at the cost of more server renders; raise it when a render is
454
+ * expensive.
455
+ */
456
+ syncDebounceMs?: number;
450
457
  }
451
458
  /** Handle on a running HTML live preview. */
452
459
  interface HtmlLivePreview {
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export { g as PreprEnvironment, h as PreprEventType, i as PreprFeatureConfig, j
5
5
  * Package version; must match package.json. Lives in its own leaf module so
6
6
  * core can import it without cycling through the barrel.
7
7
  */
8
- declare const VERSION = "1.0.0-rc.3";
8
+ declare const VERSION = "1.0.0-rc.5";
9
9
 
10
10
  /**
11
11
  * Resolve a `PreprFeatures` bag to plain booleans, defaulting every omitted key
@@ -447,6 +447,13 @@ interface HtmlLivePreviewOptions {
447
447
  allowedEditorOrigins?: string[];
448
448
  /** See the toolbar's `strictMode`. */
449
449
  strictMode?: boolean;
450
+ /**
451
+ * How long typing must pause, in milliseconds, before the page is
452
+ * requested with the unsaved changes. Default 150. Lower shows changes
453
+ * sooner at the cost of more server renders; raise it when a render is
454
+ * expensive.
455
+ */
456
+ syncDebounceMs?: number;
450
457
  }
451
458
  /** Handle on a running HTML live preview. */
452
459
  interface HtmlLivePreview {
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  setTrackingParam,
9
9
  stegaClean,
10
10
  trackEvent
11
- } from "./chunk-PI5HUQ6G.js";
11
+ } from "./chunk-AWFJ67AQ.js";
12
12
  import {
13
13
  PreprError,
14
14
  extractAccessToken,
@@ -19,11 +19,11 @@ import {
19
19
  getPreprUUIDFromHeaders,
20
20
  getToolbarPropsFromHeaders,
21
21
  validatePreprToken
22
- } from "./chunk-N6CXRQQP.js";
22
+ } from "./chunk-GRFTP62X.js";
23
23
  import {
24
24
  VERSION,
25
25
  resolveFeatures
26
- } from "./chunk-EJDHVIFU.js";
26
+ } from "./chunk-A4J3JRMY.js";
27
27
  import {
28
28
  createLiveBridge,
29
29
  createLiveStore
@@ -88,8 +88,9 @@ function stegaTarget(value, options = {}) {
88
88
 
89
89
  // src/core/live-preview/edit-links.ts
90
90
  import {
91
- vercelStegaCombine,
91
+ legacyStegaEncode,
92
92
  vercelStegaDecode,
93
+ vercelStegaEncode,
93
94
  vercelStegaSplit
94
95
  } from "@vercel/stega";
95
96
  var ENTRY_PARAM = "entry";
@@ -103,34 +104,53 @@ function entryIdOf(element) {
103
104
  const first = element.items[0];
104
105
  return isObject(first) && typeof first._id === "string" ? first._id : void 0;
105
106
  }
106
- function annotate(value, ctx) {
107
- if (!ctx.entryId) return value;
108
- const { cleaned, encoded } = vercelStegaSplit(value);
109
- if (!encoded) return value;
107
+ function encodeLink(payload) {
108
+ try {
109
+ return legacyStegaEncode(payload);
110
+ } catch {
111
+ return vercelStegaEncode(payload);
112
+ }
113
+ }
114
+ function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
110
115
  let payload;
111
116
  try {
112
117
  payload = vercelStegaDecode(encoded);
113
118
  } catch {
114
- return value;
119
+ return "";
115
120
  }
116
- if (typeof payload?.href !== "string") return value;
121
+ if (typeof payload?.href !== "string") return "";
117
122
  let url;
118
123
  try {
119
124
  url = new URL(payload.href);
120
125
  } catch {
121
- return value;
126
+ return "";
122
127
  }
123
- if (url.searchParams.has(ENTRY_PARAM)) return value;
124
- url.searchParams.set(ENTRY_PARAM, ctx.entryId);
128
+ if (url.searchParams.has(ENTRY_PARAM)) return "";
129
+ url.searchParams.set(ENTRY_PARAM, entryId);
125
130
  const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
126
- if (!url.searchParams.has("field") && ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) && itemId === ctx.entryOwnerId) {
127
- url.searchParams.set("field", ctx.entryField);
131
+ if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
132
+ url.searchParams.set("field", entryField);
128
133
  }
129
- return vercelStegaCombine(
130
- cleaned,
131
- { ...payload, href: url.toString() },
132
- false
133
- );
134
+ return encodeLink({ ...payload, href: url.toString() });
135
+ }
136
+ var MAX_REWRITTEN = 1e4;
137
+ var rewritten = /* @__PURE__ */ new Map();
138
+ function annotate(value, ctx) {
139
+ if (!ctx.entryId) return value;
140
+ const { cleaned, encoded } = vercelStegaSplit(value);
141
+ if (!encoded) return value;
142
+ const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
143
+ const key = `${ctx.entryId}
144
+ ${entryField}
145
+ ${ctx.entryOwnerId ?? ""}
146
+ ${encoded}`;
147
+ let link = rewritten.get(key);
148
+ if (link === void 0) {
149
+ link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
150
+ if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
151
+ rewritten.set(key, link);
152
+ }
153
+ return link ? cleaned + link : value;
134
154
  }
135
155
  function walk(value, selections, ctx) {
136
156
  if (typeof value === "string") return annotate(value, ctx);
@@ -262,6 +282,7 @@ function startHtmlLivePreview(options = {}) {
262
282
  strictMode: () => options.strictMode
263
283
  });
264
284
  bridge.start();
285
+ const debounceMs = typeof options.syncDebounceMs === "number" && Number.isFinite(options.syncDebounceMs) && options.syncDebounceMs >= 0 ? options.syncDebounceMs : SYNC_DEBOUNCE_MS;
265
286
  let timer = null;
266
287
  let inFlight = false;
267
288
  let pending = null;
@@ -303,7 +324,7 @@ function startHtmlLivePreview(options = {}) {
303
324
  timer = null;
304
325
  if (inFlight) pending = log;
305
326
  else void render(log);
306
- }, SYNC_DEBOUNCE_MS);
327
+ }, debounceMs);
307
328
  };
308
329
  const offLog = store.onLogChange((log) => {
309
330
  if (log.length) schedule(log);
@@ -3707,7 +3707,7 @@ function createPreprPreview(opts = {}) {
3707
3707
  var import_react = require("react");
3708
3708
 
3709
3709
  // src/version.ts
3710
- var VERSION = "1.0.0-rc.3";
3710
+ var VERSION = "1.0.0-rc.5";
3711
3711
 
3712
3712
  // src/core/pixel.ts
3713
3713
  var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
@@ -2,8 +2,8 @@
2
2
  import {
3
3
  createPreprPreview,
4
4
  loadTrackingPixel
5
- } from "../chunk-PI5HUQ6G.js";
6
- import "../chunk-EJDHVIFU.js";
5
+ } from "../chunk-AWFJ67AQ.js";
6
+ import "../chunk-A4J3JRMY.js";
7
7
  import "../chunk-Y7JWV7WV.js";
8
8
  import "../chunk-UTKH7DOI.js";
9
9
  import "../chunk-V7MCHPE7.js";
@@ -40,6 +40,7 @@ __export(nextjs_exports, {
40
40
  getPreprHeaders: () => getPreprHeaders,
41
41
  getPreprUUID: () => getPreprUUID,
42
42
  getToolbarProps: () => getToolbarProps,
43
+ livePreviewFetch: () => livePreviewFetch,
43
44
  normalizeQuery: () => normalizeQuery,
44
45
  validatePreprToken: () => validatePreprToken
45
46
  });
@@ -49,7 +50,7 @@ module.exports = __toCommonJS(nextjs_exports);
49
50
  var import_server = require("next/server");
50
51
 
51
52
  // src/version.ts
52
- var VERSION = "1.0.0-rc.3";
53
+ var VERSION = "1.0.0-rc.5";
53
54
 
54
55
  // src/core/constants.ts
55
56
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1356,34 +1357,53 @@ function entryIdOf(element) {
1356
1357
  const first = element.items[0];
1357
1358
  return isObject(first) && typeof first._id === "string" ? first._id : void 0;
1358
1359
  }
1359
- function annotate(value, ctx) {
1360
- if (!ctx.entryId) return value;
1361
- const { cleaned, encoded } = (0, import_stega4.vercelStegaSplit)(value);
1362
- if (!encoded) return value;
1360
+ function encodeLink(payload) {
1361
+ try {
1362
+ return (0, import_stega4.legacyStegaEncode)(payload);
1363
+ } catch {
1364
+ return (0, import_stega4.vercelStegaEncode)(payload);
1365
+ }
1366
+ }
1367
+ function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
1363
1368
  let payload;
1364
1369
  try {
1365
1370
  payload = (0, import_stega4.vercelStegaDecode)(encoded);
1366
1371
  } catch {
1367
- return value;
1372
+ return "";
1368
1373
  }
1369
- if (typeof payload?.href !== "string") return value;
1374
+ if (typeof payload?.href !== "string") return "";
1370
1375
  let url;
1371
1376
  try {
1372
1377
  url = new URL(payload.href);
1373
1378
  } catch {
1374
- return value;
1379
+ return "";
1375
1380
  }
1376
- if (url.searchParams.has(ENTRY_PARAM)) return value;
1377
- url.searchParams.set(ENTRY_PARAM, ctx.entryId);
1381
+ if (url.searchParams.has(ENTRY_PARAM)) return "";
1382
+ url.searchParams.set(ENTRY_PARAM, entryId);
1378
1383
  const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
1379
- if (!url.searchParams.has("field") && ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) && itemId === ctx.entryOwnerId) {
1380
- url.searchParams.set("field", ctx.entryField);
1384
+ if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
1385
+ url.searchParams.set("field", entryField);
1381
1386
  }
1382
- return (0, import_stega4.vercelStegaCombine)(
1383
- cleaned,
1384
- { ...payload, href: url.toString() },
1385
- false
1386
- );
1387
+ return encodeLink({ ...payload, href: url.toString() });
1388
+ }
1389
+ var MAX_REWRITTEN = 1e4;
1390
+ var rewritten = /* @__PURE__ */ new Map();
1391
+ function annotate(value, ctx) {
1392
+ if (!ctx.entryId) return value;
1393
+ const { cleaned, encoded } = (0, import_stega4.vercelStegaSplit)(value);
1394
+ if (!encoded) return value;
1395
+ const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
1396
+ const key = `${ctx.entryId}
1397
+ ${entryField}
1398
+ ${ctx.entryOwnerId ?? ""}
1399
+ ${encoded}`;
1400
+ let link = rewritten.get(key);
1401
+ if (link === void 0) {
1402
+ link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
1403
+ if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
1404
+ rewritten.set(key, link);
1405
+ }
1406
+ return link ? cleaned + link : value;
1387
1407
  }
1388
1408
  function walk(value, selections, ctx) {
1389
1409
  if (typeof value === "string") return annotate(value, ctx);
@@ -1464,11 +1484,17 @@ function takeLiveLog(token) {
1464
1484
  if (!token || !TOKEN_PATTERN.test(token)) return [];
1465
1485
  const map = slots();
1466
1486
  const slot = map.get(token);
1467
- map.delete(token);
1468
1487
  const now = Date.now();
1469
1488
  prune(map, now);
1470
1489
  if (!slot || slot.expires <= now) return [];
1471
- return slot.log;
1490
+ const { log } = slot;
1491
+ slot.log = [];
1492
+ return log;
1493
+ }
1494
+ function hasLiveLog(token) {
1495
+ if (!token || !TOKEN_PATTERN.test(token)) return false;
1496
+ const slot = slots().get(token);
1497
+ return slot !== void 0 && slot.expires > Date.now();
1472
1498
  }
1473
1499
 
1474
1500
  // src/core/server.ts
@@ -1680,6 +1706,59 @@ async function normalizeQuery(query, result, options = {}) {
1680
1706
  if (!log.length) return annotated;
1681
1707
  return applyPatchLog(query, annotated, log, options.operationName);
1682
1708
  }
1709
+ var MAX_STORED_RESPONSES = 30;
1710
+ var STORED_RESPONSE_TTL_MS = 5 * 6e4;
1711
+ var storedResponses = /* @__PURE__ */ new Map();
1712
+ var documentIds = /* @__PURE__ */ new WeakMap();
1713
+ var nextDocumentId = 0;
1714
+ function queryKey(query) {
1715
+ if (typeof query === "string") return query;
1716
+ let id = documentIds.get(query);
1717
+ if (id === void 0) documentIds.set(query, id = nextDocumentId++);
1718
+ return `#${id}`;
1719
+ }
1720
+ function responseKey(key, preprHeaders) {
1721
+ try {
1722
+ return JSON.stringify([
1723
+ queryKey(key.query),
1724
+ key.variables ?? null,
1725
+ // Segment, A/B variant and visitor decide what Prepr returns.
1726
+ Object.entries(preprHeaders).sort(([a], [b]) => a < b ? -1 : 1)
1727
+ ]);
1728
+ } catch {
1729
+ return null;
1730
+ }
1731
+ }
1732
+ async function livePreviewFetch(key, fetcher, options = {}) {
1733
+ if (!options.enabled) return fetcher();
1734
+ let unsaved;
1735
+ let preprHeaders;
1736
+ try {
1737
+ const h = await readHeaders();
1738
+ preprHeaders = getPreprHeadersFromHeaders(h);
1739
+ unsaved = h.has(HEADER_NEXT_ACTION) && hasLiveLog((await (0, import_headers.cookies)()).get(LIVE_LOG_COOKIE)?.value);
1740
+ } catch (error) {
1741
+ navigation.unstable_rethrow?.(error);
1742
+ return fetcher();
1743
+ }
1744
+ const id = responseKey(key, preprHeaders);
1745
+ if (id === null) return fetcher();
1746
+ const now = Date.now();
1747
+ if (unsaved) {
1748
+ const stored = storedResponses.get(id);
1749
+ if (stored && stored.expires > now) return stored.value;
1750
+ }
1751
+ const value = await fetcher();
1752
+ storedResponses.delete(id);
1753
+ for (const [other, stored] of storedResponses) {
1754
+ if (stored.expires > now && storedResponses.size < MAX_STORED_RESPONSES) {
1755
+ break;
1756
+ }
1757
+ storedResponses.delete(other);
1758
+ }
1759
+ storedResponses.set(id, { value, expires: now + STORED_RESPONSE_TTL_MS });
1760
+ return value;
1761
+ }
1683
1762
 
1684
1763
  // src/nextjs/index.ts
1685
1764
  var import_components = require("./components.cjs");
@@ -1695,6 +1774,7 @@ var import_components = require("./components.cjs");
1695
1774
  getPreprHeaders,
1696
1775
  getPreprUUID,
1697
1776
  getToolbarProps,
1777
+ livePreviewFetch,
1698
1778
  normalizeQuery,
1699
1779
  validatePreprToken
1700
1780
  });