@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 +2 -0
- package/dist/astro/index.cjs +36 -17
- package/dist/astro/index.js +38 -18
- package/dist/{chunk-EJDHVIFU.js → chunk-A4J3JRMY.js} +1 -1
- package/dist/{chunk-PI5HUQ6G.js → chunk-AWFJ67AQ.js} +1 -1
- package/dist/{chunk-N6CXRQQP.js → chunk-GRFTP62X.js} +1 -1
- package/dist/index.cjs +38 -18
- package/dist/index.d.cts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +42 -21
- package/dist/nextjs/components.cjs +1 -1
- package/dist/nextjs/components.js +2 -2
- package/dist/nextjs/index.cjs +99 -19
- package/dist/nextjs/index.d.cts +47 -1
- package/dist/nextjs/index.d.ts +47 -1
- package/dist/nextjs/index.js +100 -20
- package/dist/nextjs/live-action.js +2 -2
- package/dist/nextjs/live.cjs +25 -10
- package/dist/nextjs/live.d.cts +7 -1
- package/dist/nextjs/live.d.ts +7 -1
- package/dist/nextjs/live.js +25 -10
- package/dist/nuxt/index.cjs +1 -1
- package/dist/nuxt/index.js +1 -1
- package/dist/nuxt/live.cjs +35 -16
- package/dist/nuxt/live.js +37 -17
- package/dist/react/components.cjs +1 -1
- package/dist/react/components.js +1 -1
- package/dist/sveltekit/index.cjs +1 -1
- package/dist/sveltekit/index.js +1 -1
- package/dist/sveltekit/live.cjs +35 -16
- package/dist/sveltekit/live.js +37 -17
- package/package.json +1 -1
package/dist/nextjs/index.d.cts
CHANGED
|
@@ -236,6 +236,46 @@ interface NextNormalizeQueryOptions {
|
|
|
236
236
|
* component that should update. Server-only; Next.js 16+.
|
|
237
237
|
*/
|
|
238
238
|
declare function normalizeQuery<T>(query: QueryInput, result: T, options?: NextNormalizeQueryOptions): Promise<T>;
|
|
239
|
+
interface LivePreviewFetchKey {
|
|
240
|
+
/** The query the fetcher runs, string or document. */
|
|
241
|
+
query: QueryInput;
|
|
242
|
+
/** Its variables. */
|
|
243
|
+
variables?: Record<string, unknown>;
|
|
244
|
+
}
|
|
245
|
+
interface LivePreviewFetchOptions {
|
|
246
|
+
/**
|
|
247
|
+
* Pass the same preview flag as the middleware; never `true` on a
|
|
248
|
+
* production deployment. When false (the default) the fetcher runs as it
|
|
249
|
+
* is and the request is not read.
|
|
250
|
+
*/
|
|
251
|
+
enabled?: boolean;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Skips the Prepr request while an editor types. Wrap the fetch of a query
|
|
255
|
+
* whose result goes through `normalizeQuery`:
|
|
256
|
+
*
|
|
257
|
+
* ```ts
|
|
258
|
+
* const data = await livePreviewFetch(
|
|
259
|
+
* { query: GetPageDocument, variables: { slug } },
|
|
260
|
+
* () => fetchFromPrepr(GetPageDocument, { slug }),
|
|
261
|
+
* { enabled: isPreview },
|
|
262
|
+
* );
|
|
263
|
+
* const page = await normalizeQuery(GetPageDocument, data, { enabled: isPreview });
|
|
264
|
+
* ```
|
|
265
|
+
*
|
|
266
|
+
* A render for unsaved changes gets the response of the last full render of
|
|
267
|
+
* the same query, variables and Prepr headers; `normalizeQuery` puts the
|
|
268
|
+
* changes on top. Every other render (a page load, a save) runs the fetcher
|
|
269
|
+
* and stores what it returns. Stored content is not changed by typing, so
|
|
270
|
+
* the page shows the same as without this helper.
|
|
271
|
+
*
|
|
272
|
+
* Responses are kept in the memory of the server process, a few at a time
|
|
273
|
+
* and for a few minutes; another instance or an expired response runs the
|
|
274
|
+
* fetcher. Treat the returned value as read-only: renders share it. Keys
|
|
275
|
+
* must tell responses apart, so include every variable the fetcher uses.
|
|
276
|
+
* Server-only.
|
|
277
|
+
*/
|
|
278
|
+
declare function livePreviewFetch<T>(key: LivePreviewFetchKey, fetcher: () => Promise<T>, options?: LivePreviewFetchOptions): Promise<T>;
|
|
239
279
|
|
|
240
280
|
/** Error thrown by the token helpers and the segments fetch. */
|
|
241
281
|
declare class PreprError extends Error {
|
|
@@ -340,6 +380,12 @@ interface PreprRefreshProps {
|
|
|
340
380
|
allowedEditorOrigins?: string[];
|
|
341
381
|
/** Your preview token uses GraphQL strict mode; see the toolbar option. */
|
|
342
382
|
strictMode?: boolean;
|
|
383
|
+
/**
|
|
384
|
+
* How long typing must pause, in milliseconds, before the unsaved changes
|
|
385
|
+
* go to the server. Default 150. Lower shows changes sooner at the cost of
|
|
386
|
+
* more server renders; raise it when a render is expensive.
|
|
387
|
+
*/
|
|
388
|
+
syncDebounceMs?: number;
|
|
343
389
|
}
|
|
344
390
|
interface UseLiveOptions {
|
|
345
391
|
/** Query (string or DocumentNode) passed as-is to the fetcher. */
|
|
@@ -347,4 +393,4 @@ interface UseLiveOptions {
|
|
|
347
393
|
variables?: Record<string, unknown>;
|
|
348
394
|
}
|
|
349
395
|
|
|
350
|
-
export { type LiveFetcher, type LivePreviewConfig, type NextNormalizeQueryOptions, PreprError, type PreprMiddlewareOptions, type PreprRefreshProps, PreprToolbar, PreprTrackingPixel, type PreprTrackingPixelProps, type UseLiveOptions, createPreprMiddleware, extractAccessToken, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprUUID, getToolbarProps, normalizeQuery, validatePreprToken };
|
|
396
|
+
export { type LiveFetcher, type LivePreviewConfig, type LivePreviewFetchKey, type LivePreviewFetchOptions, type NextNormalizeQueryOptions, PreprError, type PreprMiddlewareOptions, type PreprRefreshProps, PreprToolbar, PreprTrackingPixel, type PreprTrackingPixelProps, type UseLiveOptions, createPreprMiddleware, extractAccessToken, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprUUID, getToolbarProps, livePreviewFetch, normalizeQuery, validatePreprToken };
|
package/dist/nextjs/index.d.ts
CHANGED
|
@@ -236,6 +236,46 @@ interface NextNormalizeQueryOptions {
|
|
|
236
236
|
* component that should update. Server-only; Next.js 16+.
|
|
237
237
|
*/
|
|
238
238
|
declare function normalizeQuery<T>(query: QueryInput, result: T, options?: NextNormalizeQueryOptions): Promise<T>;
|
|
239
|
+
interface LivePreviewFetchKey {
|
|
240
|
+
/** The query the fetcher runs, string or document. */
|
|
241
|
+
query: QueryInput;
|
|
242
|
+
/** Its variables. */
|
|
243
|
+
variables?: Record<string, unknown>;
|
|
244
|
+
}
|
|
245
|
+
interface LivePreviewFetchOptions {
|
|
246
|
+
/**
|
|
247
|
+
* Pass the same preview flag as the middleware; never `true` on a
|
|
248
|
+
* production deployment. When false (the default) the fetcher runs as it
|
|
249
|
+
* is and the request is not read.
|
|
250
|
+
*/
|
|
251
|
+
enabled?: boolean;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Skips the Prepr request while an editor types. Wrap the fetch of a query
|
|
255
|
+
* whose result goes through `normalizeQuery`:
|
|
256
|
+
*
|
|
257
|
+
* ```ts
|
|
258
|
+
* const data = await livePreviewFetch(
|
|
259
|
+
* { query: GetPageDocument, variables: { slug } },
|
|
260
|
+
* () => fetchFromPrepr(GetPageDocument, { slug }),
|
|
261
|
+
* { enabled: isPreview },
|
|
262
|
+
* );
|
|
263
|
+
* const page = await normalizeQuery(GetPageDocument, data, { enabled: isPreview });
|
|
264
|
+
* ```
|
|
265
|
+
*
|
|
266
|
+
* A render for unsaved changes gets the response of the last full render of
|
|
267
|
+
* the same query, variables and Prepr headers; `normalizeQuery` puts the
|
|
268
|
+
* changes on top. Every other render (a page load, a save) runs the fetcher
|
|
269
|
+
* and stores what it returns. Stored content is not changed by typing, so
|
|
270
|
+
* the page shows the same as without this helper.
|
|
271
|
+
*
|
|
272
|
+
* Responses are kept in the memory of the server process, a few at a time
|
|
273
|
+
* and for a few minutes; another instance or an expired response runs the
|
|
274
|
+
* fetcher. Treat the returned value as read-only: renders share it. Keys
|
|
275
|
+
* must tell responses apart, so include every variable the fetcher uses.
|
|
276
|
+
* Server-only.
|
|
277
|
+
*/
|
|
278
|
+
declare function livePreviewFetch<T>(key: LivePreviewFetchKey, fetcher: () => Promise<T>, options?: LivePreviewFetchOptions): Promise<T>;
|
|
239
279
|
|
|
240
280
|
/** Error thrown by the token helpers and the segments fetch. */
|
|
241
281
|
declare class PreprError extends Error {
|
|
@@ -340,6 +380,12 @@ interface PreprRefreshProps {
|
|
|
340
380
|
allowedEditorOrigins?: string[];
|
|
341
381
|
/** Your preview token uses GraphQL strict mode; see the toolbar option. */
|
|
342
382
|
strictMode?: boolean;
|
|
383
|
+
/**
|
|
384
|
+
* How long typing must pause, in milliseconds, before the unsaved changes
|
|
385
|
+
* go to the server. Default 150. Lower shows changes sooner at the cost of
|
|
386
|
+
* more server renders; raise it when a render is expensive.
|
|
387
|
+
*/
|
|
388
|
+
syncDebounceMs?: number;
|
|
343
389
|
}
|
|
344
390
|
interface UseLiveOptions {
|
|
345
391
|
/** Query (string or DocumentNode) passed as-is to the fetcher. */
|
|
@@ -347,4 +393,4 @@ interface UseLiveOptions {
|
|
|
347
393
|
variables?: Record<string, unknown>;
|
|
348
394
|
}
|
|
349
395
|
|
|
350
|
-
export { type LiveFetcher, type LivePreviewConfig, type NextNormalizeQueryOptions, PreprError, type PreprMiddlewareOptions, type PreprRefreshProps, PreprToolbar, PreprTrackingPixel, type PreprTrackingPixelProps, type UseLiveOptions, createPreprMiddleware, extractAccessToken, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprUUID, getToolbarProps, normalizeQuery, validatePreprToken };
|
|
396
|
+
export { type LiveFetcher, type LivePreviewConfig, type LivePreviewFetchKey, type LivePreviewFetchOptions, type NextNormalizeQueryOptions, PreprError, type PreprMiddlewareOptions, type PreprRefreshProps, PreprToolbar, PreprTrackingPixel, type PreprTrackingPixelProps, type UseLiveOptions, createPreprMiddleware, extractAccessToken, getActiveSegment, getActiveVariant, getPreprHeaders, getPreprUUID, getToolbarProps, livePreviewFetch, normalizeQuery, validatePreprToken };
|
package/dist/nextjs/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { NextResponse } from "next/server";
|
|
3
3
|
|
|
4
4
|
// src/version.ts
|
|
5
|
-
var VERSION = "1.0.0-rc.
|
|
5
|
+
var VERSION = "1.0.0-rc.5";
|
|
6
6
|
|
|
7
7
|
// src/core/constants.ts
|
|
8
8
|
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
@@ -1302,8 +1302,9 @@ function listKeysIn(result) {
|
|
|
1302
1302
|
|
|
1303
1303
|
// src/core/live-preview/edit-links.ts
|
|
1304
1304
|
import {
|
|
1305
|
-
|
|
1305
|
+
legacyStegaEncode,
|
|
1306
1306
|
vercelStegaDecode as vercelStegaDecode2,
|
|
1307
|
+
vercelStegaEncode,
|
|
1307
1308
|
vercelStegaSplit as vercelStegaSplit4
|
|
1308
1309
|
} from "@vercel/stega";
|
|
1309
1310
|
var ENTRY_PARAM = "entry";
|
|
@@ -1317,34 +1318,53 @@ function entryIdOf(element) {
|
|
|
1317
1318
|
const first = element.items[0];
|
|
1318
1319
|
return isObject(first) && typeof first._id === "string" ? first._id : void 0;
|
|
1319
1320
|
}
|
|
1320
|
-
function
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1321
|
+
function encodeLink(payload) {
|
|
1322
|
+
try {
|
|
1323
|
+
return legacyStegaEncode(payload);
|
|
1324
|
+
} catch {
|
|
1325
|
+
return vercelStegaEncode(payload);
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
|
|
1324
1329
|
let payload;
|
|
1325
1330
|
try {
|
|
1326
1331
|
payload = vercelStegaDecode2(encoded);
|
|
1327
1332
|
} catch {
|
|
1328
|
-
return
|
|
1333
|
+
return "";
|
|
1329
1334
|
}
|
|
1330
|
-
if (typeof payload?.href !== "string") return
|
|
1335
|
+
if (typeof payload?.href !== "string") return "";
|
|
1331
1336
|
let url;
|
|
1332
1337
|
try {
|
|
1333
1338
|
url = new URL(payload.href);
|
|
1334
1339
|
} catch {
|
|
1335
|
-
return
|
|
1340
|
+
return "";
|
|
1336
1341
|
}
|
|
1337
|
-
if (url.searchParams.has(ENTRY_PARAM)) return
|
|
1338
|
-
url.searchParams.set(ENTRY_PARAM,
|
|
1342
|
+
if (url.searchParams.has(ENTRY_PARAM)) return "";
|
|
1343
|
+
url.searchParams.set(ENTRY_PARAM, entryId);
|
|
1339
1344
|
const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
|
|
1340
|
-
if (!url.searchParams.has("field") &&
|
|
1341
|
-
url.searchParams.set("field",
|
|
1345
|
+
if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
|
|
1346
|
+
url.searchParams.set("field", entryField);
|
|
1342
1347
|
}
|
|
1343
|
-
return
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
+
return encodeLink({ ...payload, href: url.toString() });
|
|
1349
|
+
}
|
|
1350
|
+
var MAX_REWRITTEN = 1e4;
|
|
1351
|
+
var rewritten = /* @__PURE__ */ new Map();
|
|
1352
|
+
function annotate(value, ctx) {
|
|
1353
|
+
if (!ctx.entryId) return value;
|
|
1354
|
+
const { cleaned, encoded } = vercelStegaSplit4(value);
|
|
1355
|
+
if (!encoded) return value;
|
|
1356
|
+
const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
|
|
1357
|
+
const key = `${ctx.entryId}
|
|
1358
|
+
${entryField}
|
|
1359
|
+
${ctx.entryOwnerId ?? ""}
|
|
1360
|
+
${encoded}`;
|
|
1361
|
+
let link = rewritten.get(key);
|
|
1362
|
+
if (link === void 0) {
|
|
1363
|
+
link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
|
|
1364
|
+
if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
|
|
1365
|
+
rewritten.set(key, link);
|
|
1366
|
+
}
|
|
1367
|
+
return link ? cleaned + link : value;
|
|
1348
1368
|
}
|
|
1349
1369
|
function walk(value, selections, ctx) {
|
|
1350
1370
|
if (typeof value === "string") return annotate(value, ctx);
|
|
@@ -1425,11 +1445,17 @@ function takeLiveLog(token) {
|
|
|
1425
1445
|
if (!token || !TOKEN_PATTERN.test(token)) return [];
|
|
1426
1446
|
const map = slots();
|
|
1427
1447
|
const slot = map.get(token);
|
|
1428
|
-
map.delete(token);
|
|
1429
1448
|
const now = Date.now();
|
|
1430
1449
|
prune(map, now);
|
|
1431
1450
|
if (!slot || slot.expires <= now) return [];
|
|
1432
|
-
|
|
1451
|
+
const { log } = slot;
|
|
1452
|
+
slot.log = [];
|
|
1453
|
+
return log;
|
|
1454
|
+
}
|
|
1455
|
+
function hasLiveLog(token) {
|
|
1456
|
+
if (!token || !TOKEN_PATTERN.test(token)) return false;
|
|
1457
|
+
const slot = slots().get(token);
|
|
1458
|
+
return slot !== void 0 && slot.expires > Date.now();
|
|
1433
1459
|
}
|
|
1434
1460
|
|
|
1435
1461
|
// src/core/server.ts
|
|
@@ -1641,6 +1667,59 @@ async function normalizeQuery(query, result, options = {}) {
|
|
|
1641
1667
|
if (!log.length) return annotated;
|
|
1642
1668
|
return applyPatchLog(query, annotated, log, options.operationName);
|
|
1643
1669
|
}
|
|
1670
|
+
var MAX_STORED_RESPONSES = 30;
|
|
1671
|
+
var STORED_RESPONSE_TTL_MS = 5 * 6e4;
|
|
1672
|
+
var storedResponses = /* @__PURE__ */ new Map();
|
|
1673
|
+
var documentIds = /* @__PURE__ */ new WeakMap();
|
|
1674
|
+
var nextDocumentId = 0;
|
|
1675
|
+
function queryKey(query) {
|
|
1676
|
+
if (typeof query === "string") return query;
|
|
1677
|
+
let id = documentIds.get(query);
|
|
1678
|
+
if (id === void 0) documentIds.set(query, id = nextDocumentId++);
|
|
1679
|
+
return `#${id}`;
|
|
1680
|
+
}
|
|
1681
|
+
function responseKey(key, preprHeaders) {
|
|
1682
|
+
try {
|
|
1683
|
+
return JSON.stringify([
|
|
1684
|
+
queryKey(key.query),
|
|
1685
|
+
key.variables ?? null,
|
|
1686
|
+
// Segment, A/B variant and visitor decide what Prepr returns.
|
|
1687
|
+
Object.entries(preprHeaders).sort(([a], [b]) => a < b ? -1 : 1)
|
|
1688
|
+
]);
|
|
1689
|
+
} catch {
|
|
1690
|
+
return null;
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
async function livePreviewFetch(key, fetcher, options = {}) {
|
|
1694
|
+
if (!options.enabled) return fetcher();
|
|
1695
|
+
let unsaved;
|
|
1696
|
+
let preprHeaders;
|
|
1697
|
+
try {
|
|
1698
|
+
const h = await readHeaders();
|
|
1699
|
+
preprHeaders = getPreprHeadersFromHeaders(h);
|
|
1700
|
+
unsaved = h.has(HEADER_NEXT_ACTION) && hasLiveLog((await cookies()).get(LIVE_LOG_COOKIE)?.value);
|
|
1701
|
+
} catch (error) {
|
|
1702
|
+
navigation.unstable_rethrow?.(error);
|
|
1703
|
+
return fetcher();
|
|
1704
|
+
}
|
|
1705
|
+
const id = responseKey(key, preprHeaders);
|
|
1706
|
+
if (id === null) return fetcher();
|
|
1707
|
+
const now = Date.now();
|
|
1708
|
+
if (unsaved) {
|
|
1709
|
+
const stored = storedResponses.get(id);
|
|
1710
|
+
if (stored && stored.expires > now) return stored.value;
|
|
1711
|
+
}
|
|
1712
|
+
const value = await fetcher();
|
|
1713
|
+
storedResponses.delete(id);
|
|
1714
|
+
for (const [other, stored] of storedResponses) {
|
|
1715
|
+
if (stored.expires > now && storedResponses.size < MAX_STORED_RESPONSES) {
|
|
1716
|
+
break;
|
|
1717
|
+
}
|
|
1718
|
+
storedResponses.delete(other);
|
|
1719
|
+
}
|
|
1720
|
+
storedResponses.set(id, { value, expires: now + STORED_RESPONSE_TTL_MS });
|
|
1721
|
+
return value;
|
|
1722
|
+
}
|
|
1644
1723
|
|
|
1645
1724
|
// src/nextjs/index.ts
|
|
1646
1725
|
import {
|
|
@@ -1658,6 +1737,7 @@ export {
|
|
|
1658
1737
|
getPreprHeaders,
|
|
1659
1738
|
getPreprUUID,
|
|
1660
1739
|
getToolbarProps,
|
|
1740
|
+
livePreviewFetch,
|
|
1661
1741
|
normalizeQuery,
|
|
1662
1742
|
validatePreprToken
|
|
1663
1743
|
};
|
package/dist/nextjs/live.cjs
CHANGED
|
@@ -1798,6 +1798,7 @@ function createLiveStarter(options = {}) {
|
|
|
1798
1798
|
var import_live_action = require("./live-action.cjs");
|
|
1799
1799
|
var debug6 = createScopedLogger("live-preview:next");
|
|
1800
1800
|
var LOG_SYNC_DEBOUNCE_MS = 150;
|
|
1801
|
+
var debounceOf = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : LOG_SYNC_DEBOUNCE_MS;
|
|
1801
1802
|
var starter = createLiveStarter({
|
|
1802
1803
|
storeOptions() {
|
|
1803
1804
|
const skipped = new Set(quarantined());
|
|
@@ -1842,7 +1843,8 @@ function useLive(data, opts) {
|
|
|
1842
1843
|
function PreprRefresh({
|
|
1843
1844
|
enabled = false,
|
|
1844
1845
|
allowedEditorOrigins: origins,
|
|
1845
|
-
strictMode: strict = false
|
|
1846
|
+
strictMode: strict = false,
|
|
1847
|
+
syncDebounceMs
|
|
1846
1848
|
}) {
|
|
1847
1849
|
const router = (0, import_navigation.useRouter)();
|
|
1848
1850
|
const originsKey = origins?.join(" ");
|
|
@@ -1852,7 +1854,7 @@ function PreprRefresh({
|
|
|
1852
1854
|
strictMode: strict,
|
|
1853
1855
|
allowedEditorOrigins: originsKey?.split(" ")
|
|
1854
1856
|
});
|
|
1855
|
-
const sync = createLogSync();
|
|
1857
|
+
const sync = createLogSync(debounceOf(syncDebounceMs));
|
|
1856
1858
|
let unsubscribe = null;
|
|
1857
1859
|
let active = true;
|
|
1858
1860
|
void starter.start().then((started) => {
|
|
@@ -1872,7 +1874,7 @@ function PreprRefresh({
|
|
|
1872
1874
|
unsubscribe?.();
|
|
1873
1875
|
sync.cancel();
|
|
1874
1876
|
};
|
|
1875
|
-
}, [enabled, originsKey, strict, router]);
|
|
1877
|
+
}, [enabled, originsKey, strict, syncDebounceMs, router]);
|
|
1876
1878
|
return null;
|
|
1877
1879
|
}
|
|
1878
1880
|
var STEGA_CHARS = /[\u200b-\u200d\u2060-\u2064\ufeff]/;
|
|
@@ -1947,18 +1949,21 @@ function watchClientRenders(watched) {
|
|
|
1947
1949
|
}, CLIENT_RENDER_MS);
|
|
1948
1950
|
});
|
|
1949
1951
|
}
|
|
1950
|
-
function createLogSync() {
|
|
1952
|
+
function createLogSync(debounceMs) {
|
|
1951
1953
|
let timer = null;
|
|
1952
1954
|
let inFlight = false;
|
|
1953
1955
|
let pending = null;
|
|
1954
1956
|
let cancelled = false;
|
|
1955
1957
|
let lastGood = [];
|
|
1958
|
+
let stopSettling = null;
|
|
1956
1959
|
const send = async (incoming) => {
|
|
1957
1960
|
const skipped = new Set(quarantined());
|
|
1958
1961
|
const log = skipped.size ? incoming.filter((patch) => !skipped.has(patchKey(patch))) : incoming;
|
|
1959
1962
|
if (!log.length) return;
|
|
1960
1963
|
inFlight = true;
|
|
1961
|
-
const
|
|
1964
|
+
const watch = stopSettling !== null || showsPreprContent();
|
|
1965
|
+
stopSettling?.();
|
|
1966
|
+
const stopWatching = watch ? watchUntilSettled(() => recoverFrom(log, lastGood)) : () => {
|
|
1962
1967
|
};
|
|
1963
1968
|
let failed = false;
|
|
1964
1969
|
try {
|
|
@@ -1967,11 +1972,21 @@ function createLogSync() {
|
|
|
1967
1972
|
failed = true;
|
|
1968
1973
|
debug6.log("sending unsaved changes failed", error);
|
|
1969
1974
|
}
|
|
1970
|
-
await new Promise((resolve) => setTimeout(resolve, RENDER_SETTLE_MS));
|
|
1971
|
-
stopWatching();
|
|
1972
1975
|
inFlight = false;
|
|
1973
|
-
if (recovering)
|
|
1974
|
-
|
|
1976
|
+
if (recovering) {
|
|
1977
|
+
stopWatching();
|
|
1978
|
+
return;
|
|
1979
|
+
}
|
|
1980
|
+
const settle = setTimeout(() => {
|
|
1981
|
+
stopSettling = null;
|
|
1982
|
+
stopWatching();
|
|
1983
|
+
if (!failed && !recovering) lastGood = log;
|
|
1984
|
+
}, RENDER_SETTLE_MS);
|
|
1985
|
+
stopSettling = watch ? () => {
|
|
1986
|
+
clearTimeout(settle);
|
|
1987
|
+
stopWatching();
|
|
1988
|
+
stopSettling = null;
|
|
1989
|
+
} : null;
|
|
1975
1990
|
if (pending && !cancelled) {
|
|
1976
1991
|
const next = pending;
|
|
1977
1992
|
pending = null;
|
|
@@ -1986,7 +2001,7 @@ function createLogSync() {
|
|
|
1986
2001
|
timer = null;
|
|
1987
2002
|
if (inFlight) pending = log;
|
|
1988
2003
|
else void send(log);
|
|
1989
|
-
},
|
|
2004
|
+
}, debounceMs);
|
|
1990
2005
|
},
|
|
1991
2006
|
cancel() {
|
|
1992
2007
|
cancelled = true;
|
package/dist/nextjs/live.d.cts
CHANGED
|
@@ -23,6 +23,12 @@ interface PreprRefreshProps {
|
|
|
23
23
|
allowedEditorOrigins?: string[];
|
|
24
24
|
/** Your preview token uses GraphQL strict mode; see the toolbar option. */
|
|
25
25
|
strictMode?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* How long typing must pause, in milliseconds, before the unsaved changes
|
|
28
|
+
* go to the server. Default 150. Lower shows changes sooner at the cost of
|
|
29
|
+
* more server renders; raise it when a render is expensive.
|
|
30
|
+
*/
|
|
31
|
+
syncDebounceMs?: number;
|
|
26
32
|
}
|
|
27
33
|
interface UseLiveOptions {
|
|
28
34
|
/** Query (string or DocumentNode) passed as-is to the fetcher. */
|
|
@@ -49,6 +55,6 @@ declare function useLive<T>(data: T, opts?: UseLiveOptions): T;
|
|
|
49
55
|
* Use this or `useLive` with a `query`, not both: a save would refetch and
|
|
50
56
|
* refresh.
|
|
51
57
|
*/
|
|
52
|
-
declare function PreprRefresh({ enabled, allowedEditorOrigins: origins, strictMode: strict, }: PreprRefreshProps): null;
|
|
58
|
+
declare function PreprRefresh({ enabled, allowedEditorOrigins: origins, strictMode: strict, syncDebounceMs, }: PreprRefreshProps): null;
|
|
53
59
|
|
|
54
60
|
export { type LivePreviewConfig, PreprRefresh, type PreprRefreshProps, type UseLiveOptions, initLivePreview, useLive };
|
package/dist/nextjs/live.d.ts
CHANGED
|
@@ -23,6 +23,12 @@ interface PreprRefreshProps {
|
|
|
23
23
|
allowedEditorOrigins?: string[];
|
|
24
24
|
/** Your preview token uses GraphQL strict mode; see the toolbar option. */
|
|
25
25
|
strictMode?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* How long typing must pause, in milliseconds, before the unsaved changes
|
|
28
|
+
* go to the server. Default 150. Lower shows changes sooner at the cost of
|
|
29
|
+
* more server renders; raise it when a render is expensive.
|
|
30
|
+
*/
|
|
31
|
+
syncDebounceMs?: number;
|
|
26
32
|
}
|
|
27
33
|
interface UseLiveOptions {
|
|
28
34
|
/** Query (string or DocumentNode) passed as-is to the fetcher. */
|
|
@@ -49,6 +55,6 @@ declare function useLive<T>(data: T, opts?: UseLiveOptions): T;
|
|
|
49
55
|
* Use this or `useLive` with a `query`, not both: a save would refetch and
|
|
50
56
|
* refresh.
|
|
51
57
|
*/
|
|
52
|
-
declare function PreprRefresh({ enabled, allowedEditorOrigins: origins, strictMode: strict, }: PreprRefreshProps): null;
|
|
58
|
+
declare function PreprRefresh({ enabled, allowedEditorOrigins: origins, strictMode: strict, syncDebounceMs, }: PreprRefreshProps): null;
|
|
53
59
|
|
|
54
60
|
export { type LivePreviewConfig, PreprRefresh, type PreprRefreshProps, type UseLiveOptions, initLivePreview, useLive };
|
package/dist/nextjs/live.js
CHANGED
|
@@ -76,6 +76,7 @@ function createLiveStarter(options = {}) {
|
|
|
76
76
|
import { syncPreprLiveLog } from "./live-action.js";
|
|
77
77
|
var debug = createScopedLogger("live-preview:next");
|
|
78
78
|
var LOG_SYNC_DEBOUNCE_MS = 150;
|
|
79
|
+
var debounceOf = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : LOG_SYNC_DEBOUNCE_MS;
|
|
79
80
|
var starter = createLiveStarter({
|
|
80
81
|
storeOptions() {
|
|
81
82
|
const skipped = new Set(quarantined());
|
|
@@ -120,7 +121,8 @@ function useLive(data, opts) {
|
|
|
120
121
|
function PreprRefresh({
|
|
121
122
|
enabled = false,
|
|
122
123
|
allowedEditorOrigins: origins,
|
|
123
|
-
strictMode: strict = false
|
|
124
|
+
strictMode: strict = false,
|
|
125
|
+
syncDebounceMs
|
|
124
126
|
}) {
|
|
125
127
|
const router = useRouter();
|
|
126
128
|
const originsKey = origins?.join(" ");
|
|
@@ -130,7 +132,7 @@ function PreprRefresh({
|
|
|
130
132
|
strictMode: strict,
|
|
131
133
|
allowedEditorOrigins: originsKey?.split(" ")
|
|
132
134
|
});
|
|
133
|
-
const sync = createLogSync();
|
|
135
|
+
const sync = createLogSync(debounceOf(syncDebounceMs));
|
|
134
136
|
let unsubscribe = null;
|
|
135
137
|
let active = true;
|
|
136
138
|
void starter.start().then((started) => {
|
|
@@ -150,7 +152,7 @@ function PreprRefresh({
|
|
|
150
152
|
unsubscribe?.();
|
|
151
153
|
sync.cancel();
|
|
152
154
|
};
|
|
153
|
-
}, [enabled, originsKey, strict, router]);
|
|
155
|
+
}, [enabled, originsKey, strict, syncDebounceMs, router]);
|
|
154
156
|
return null;
|
|
155
157
|
}
|
|
156
158
|
var STEGA_CHARS = /[\u200b-\u200d\u2060-\u2064\ufeff]/;
|
|
@@ -225,18 +227,21 @@ function watchClientRenders(watched) {
|
|
|
225
227
|
}, CLIENT_RENDER_MS);
|
|
226
228
|
});
|
|
227
229
|
}
|
|
228
|
-
function createLogSync() {
|
|
230
|
+
function createLogSync(debounceMs) {
|
|
229
231
|
let timer = null;
|
|
230
232
|
let inFlight = false;
|
|
231
233
|
let pending = null;
|
|
232
234
|
let cancelled = false;
|
|
233
235
|
let lastGood = [];
|
|
236
|
+
let stopSettling = null;
|
|
234
237
|
const send = async (incoming) => {
|
|
235
238
|
const skipped = new Set(quarantined());
|
|
236
239
|
const log = skipped.size ? incoming.filter((patch) => !skipped.has(patchKey(patch))) : incoming;
|
|
237
240
|
if (!log.length) return;
|
|
238
241
|
inFlight = true;
|
|
239
|
-
const
|
|
242
|
+
const watch = stopSettling !== null || showsPreprContent();
|
|
243
|
+
stopSettling?.();
|
|
244
|
+
const stopWatching = watch ? watchUntilSettled(() => recoverFrom(log, lastGood)) : () => {
|
|
240
245
|
};
|
|
241
246
|
let failed = false;
|
|
242
247
|
try {
|
|
@@ -245,11 +250,21 @@ function createLogSync() {
|
|
|
245
250
|
failed = true;
|
|
246
251
|
debug.log("sending unsaved changes failed", error);
|
|
247
252
|
}
|
|
248
|
-
await new Promise((resolve) => setTimeout(resolve, RENDER_SETTLE_MS));
|
|
249
|
-
stopWatching();
|
|
250
253
|
inFlight = false;
|
|
251
|
-
if (recovering)
|
|
252
|
-
|
|
254
|
+
if (recovering) {
|
|
255
|
+
stopWatching();
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
const settle = setTimeout(() => {
|
|
259
|
+
stopSettling = null;
|
|
260
|
+
stopWatching();
|
|
261
|
+
if (!failed && !recovering) lastGood = log;
|
|
262
|
+
}, RENDER_SETTLE_MS);
|
|
263
|
+
stopSettling = watch ? () => {
|
|
264
|
+
clearTimeout(settle);
|
|
265
|
+
stopWatching();
|
|
266
|
+
stopSettling = null;
|
|
267
|
+
} : null;
|
|
253
268
|
if (pending && !cancelled) {
|
|
254
269
|
const next = pending;
|
|
255
270
|
pending = null;
|
|
@@ -264,7 +279,7 @@ function createLogSync() {
|
|
|
264
279
|
timer = null;
|
|
265
280
|
if (inFlight) pending = log;
|
|
266
281
|
else void send(log);
|
|
267
|
-
},
|
|
282
|
+
}, debounceMs);
|
|
268
283
|
},
|
|
269
284
|
cancel() {
|
|
270
285
|
cancelled = true;
|
package/dist/nuxt/index.cjs
CHANGED
package/dist/nuxt/index.js
CHANGED
package/dist/nuxt/live.cjs
CHANGED
|
@@ -1741,34 +1741,53 @@ function entryIdOf(element) {
|
|
|
1741
1741
|
const first = element.items[0];
|
|
1742
1742
|
return isObject(first) && typeof first._id === "string" ? first._id : void 0;
|
|
1743
1743
|
}
|
|
1744
|
-
function
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1744
|
+
function encodeLink(payload) {
|
|
1745
|
+
try {
|
|
1746
|
+
return (0, import_stega.legacyStegaEncode)(payload);
|
|
1747
|
+
} catch {
|
|
1748
|
+
return (0, import_stega.vercelStegaEncode)(payload);
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
|
|
1748
1752
|
let payload;
|
|
1749
1753
|
try {
|
|
1750
1754
|
payload = (0, import_stega.vercelStegaDecode)(encoded);
|
|
1751
1755
|
} catch {
|
|
1752
|
-
return
|
|
1756
|
+
return "";
|
|
1753
1757
|
}
|
|
1754
|
-
if (typeof payload?.href !== "string") return
|
|
1758
|
+
if (typeof payload?.href !== "string") return "";
|
|
1755
1759
|
let url;
|
|
1756
1760
|
try {
|
|
1757
1761
|
url = new URL(payload.href);
|
|
1758
1762
|
} catch {
|
|
1759
|
-
return
|
|
1763
|
+
return "";
|
|
1760
1764
|
}
|
|
1761
|
-
if (url.searchParams.has(ENTRY_PARAM)) return
|
|
1762
|
-
url.searchParams.set(ENTRY_PARAM,
|
|
1765
|
+
if (url.searchParams.has(ENTRY_PARAM)) return "";
|
|
1766
|
+
url.searchParams.set(ENTRY_PARAM, entryId);
|
|
1763
1767
|
const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
|
|
1764
|
-
if (!url.searchParams.has("field") &&
|
|
1765
|
-
url.searchParams.set("field",
|
|
1768
|
+
if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
|
|
1769
|
+
url.searchParams.set("field", entryField);
|
|
1766
1770
|
}
|
|
1767
|
-
return (
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1771
|
+
return encodeLink({ ...payload, href: url.toString() });
|
|
1772
|
+
}
|
|
1773
|
+
var MAX_REWRITTEN = 1e4;
|
|
1774
|
+
var rewritten = /* @__PURE__ */ new Map();
|
|
1775
|
+
function annotate(value, ctx) {
|
|
1776
|
+
if (!ctx.entryId) return value;
|
|
1777
|
+
const { cleaned, encoded } = (0, import_stega.vercelStegaSplit)(value);
|
|
1778
|
+
if (!encoded) return value;
|
|
1779
|
+
const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
|
|
1780
|
+
const key = `${ctx.entryId}
|
|
1781
|
+
${entryField}
|
|
1782
|
+
${ctx.entryOwnerId ?? ""}
|
|
1783
|
+
${encoded}`;
|
|
1784
|
+
let link = rewritten.get(key);
|
|
1785
|
+
if (link === void 0) {
|
|
1786
|
+
link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
|
|
1787
|
+
if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
|
|
1788
|
+
rewritten.set(key, link);
|
|
1789
|
+
}
|
|
1790
|
+
return link ? cleaned + link : value;
|
|
1772
1791
|
}
|
|
1773
1792
|
function walk(value, selections, ctx) {
|
|
1774
1793
|
if (typeof value === "string") return annotate(value, ctx);
|