@preprio/toolkit 0.5.0-rc.4 → 0.5.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 +226 -153
- package/dist/astro/index.cjs +1 -1
- package/dist/astro/index.js +1 -1
- package/dist/{chunk-AHBN6FQP.js → chunk-3X55JL52.js} +1 -1
- package/dist/{chunk-46ISDRHT.js → chunk-CMUFTCG3.js} +1 -1
- package/dist/{chunk-PDNCNTDB.js → chunk-SBLZANL5.js} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/dist/nextjs/components.cjs +1 -1
- package/dist/nextjs/components.js +2 -2
- package/dist/nextjs/index.cjs +1 -1
- package/dist/nextjs/index.d.cts +2 -0
- package/dist/nextjs/index.d.ts +2 -0
- package/dist/nextjs/index.js +1 -1
- package/dist/nextjs/live-action.js +2 -2
- package/dist/nextjs/live.cjs +4 -1
- package/dist/nextjs/live.d.cts +3 -1
- package/dist/nextjs/live.d.ts +3 -1
- package/dist/nextjs/live.js +4 -1
- package/dist/nuxt/index.cjs +1 -1
- package/dist/nuxt/index.js +1 -1
- 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-preview-O2ZTJNW4.js +1520 -0
- package/dist/sveltekit/live.cjs +1731 -0
- package/dist/sveltekit/live.d.cts +59 -0
- package/dist/sveltekit/live.d.ts +59 -0
- package/dist/sveltekit/live.js +97 -0
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -17,8 +17,9 @@ Every integration is the same three steps:
|
|
|
17
17
|
1. **Middleware** — resolves the visitor's customer ID, segment, and A/B variant into request headers.
|
|
18
18
|
2. **Layout** — mounts the preview toolbar and the tracking pixel.
|
|
19
19
|
3. **Data fetching** — forwards the Prepr headers on your GraphQL requests, so Prepr returns personalized content.
|
|
20
|
+
4. **Live preview** (optional) — shows the editor's unsaved changes in Prepr's side-by-side preview while they type. See [Live preview](#live-preview).
|
|
20
21
|
|
|
21
|
-
Jump to your framework: [Next.js](#nextjs) · [Astro](#astro) · [SvelteKit](#sveltekit) · [Nuxt](#nuxt) · [React without a framework](#react-no-framework) · [anything else](#any-other-framework).
|
|
22
|
+
Jump to your framework: [Next.js](#nextjs) · [Astro](#astro) · [SvelteKit](#sveltekit) · [Nuxt](#nuxt) · [React without a framework](#react-no-framework) · [anything else](#any-other-framework) · [Live preview](#live-preview).
|
|
22
23
|
|
|
23
24
|
Coming from `@preprio/prepr-nextjs`? See the [migration guide](https://github.com/preprio/prepr-toolkit/blob/main/MIGRATION.md).
|
|
24
25
|
|
|
@@ -122,9 +123,12 @@ Per-framework peer dependencies — all optional; installing without them is fin
|
|
|
122
123
|
| `@preprio/toolkit` (vanilla core) | none |
|
|
123
124
|
| `@preprio/toolkit/react` | `react` >= 17, `react-dom` >= 17 |
|
|
124
125
|
| `@preprio/toolkit/nextjs` | `next` >= 13, `react` >= 17, `react-dom` >= 17 |
|
|
126
|
+
| `@preprio/toolkit/nextjs/live` | `next` >= 13, `react` >= 18 (live preview) |
|
|
125
127
|
| `@preprio/toolkit/astro` | none (`.astro` components compile in your own pipeline) |
|
|
126
128
|
| `@preprio/toolkit/sveltekit` | `svelte` (only for the `.svelte` components) |
|
|
129
|
+
| `@preprio/toolkit/sveltekit/live` | none (returns a plain Svelte store) |
|
|
127
130
|
| `@preprio/toolkit/nuxt` | `vue` (only for the `.vue` components) |
|
|
131
|
+
| `@preprio/toolkit/nuxt/live` | `vue` >= 3.3 |
|
|
128
132
|
|
|
129
133
|
## The preview gate
|
|
130
134
|
|
|
@@ -250,156 +254,83 @@ The loader rewrites `w_` to the width Next asks for and scales `h_` alongside it
|
|
|
250
254
|
|
|
251
255
|
See the runnable example at `examples/nextjs` for the full source of truth.
|
|
252
256
|
|
|
253
|
-
### Live preview
|
|
257
|
+
### Live preview in Next.js
|
|
254
258
|
|
|
255
|
-
|
|
256
|
-
|
|
259
|
+
Two additions turn on [live preview](#live-preview) for an App Router site.
|
|
260
|
+
Both take your preview flag, the same one you pass the middleware.
|
|
257
261
|
|
|
258
|
-
**
|
|
259
|
-
|
|
260
|
-
in `proxy.ts`). Make sure your middleware `matcher` covers every page you
|
|
261
|
-
preview: on a page it skips, live preview does nothing and shows no error.
|
|
262
|
-
Pass your preview flag as `enabled` to every live preview entry point below;
|
|
263
|
-
each one does nothing without it.
|
|
262
|
+
**1. Run each query result through `normalizeQuery`**, with whatever client
|
|
263
|
+
you fetch with:
|
|
264
264
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
saving:
|
|
265
|
+
```ts
|
|
266
|
+
import { normalizeQuery } from '@preprio/toolkit/nextjs';
|
|
268
267
|
|
|
269
|
-
|
|
270
|
-
that should live-update. Items without `_id` silently stay static.
|
|
271
|
-
- In dynamic content fields, select `_id` on every entry type (`Text`,
|
|
272
|
-
`Quote`, …). `Assets` entries have no `_id` of their own: select
|
|
273
|
-
`items { _id }` instead.
|
|
274
|
-
- A `Text` entry's content updates whether the query selects `body`,
|
|
275
|
-
`html` or `text` (the latter with tags stripped).
|
|
276
|
-
- Render each dynamic content entry inside an element marked with
|
|
277
|
-
`data-prepr-edit-target="<field>.<index>"` and
|
|
278
|
-
`data-prepr-entry-id="<entry _id>"` (for `Assets`, the first asset's
|
|
279
|
-
`_id`). The index is the fallback when no id matches.
|
|
280
|
-
- Values without a hidden edit link (images, videos, other URLs) are made
|
|
281
|
-
click-to-edit with `previewAttrs({ id, field }, { enabled: isPreview })`
|
|
282
|
-
on the element that renders them.
|
|
283
|
-
- Where the page renders a linked item whose content comes from items nested
|
|
284
|
-
inside it (for example a personalization or a shared section referenced
|
|
285
|
-
from a stack), wrap it in an element with
|
|
286
|
-
`data-prepr-reference="<linked item _id>"`. The editor only knows the items
|
|
287
|
-
the open item links to directly; content inside the wrapper is outlined as
|
|
288
|
-
that linked item, and clicking it opens the link in the editor.
|
|
268
|
+
const preview = process.env.VERCEL_ENV !== 'production';
|
|
289
269
|
|
|
290
|
-
|
|
270
|
+
const data = await fetchFromPrepr(GetPageDocument, { slug }); // any client
|
|
271
|
+
const page = await normalizeQuery(GetPageDocument, data, { enabled: preview });
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
**2. Mount `PreprRefresh` once in the root layout:**
|
|
291
275
|
|
|
292
276
|
```tsx
|
|
293
|
-
|
|
294
|
-
import { useEffect } from 'react';
|
|
295
|
-
import { initLivePreview } from '@preprio/toolkit/nextjs/live';
|
|
277
|
+
import { PreprRefresh } from '@preprio/toolkit/nextjs/live';
|
|
296
278
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
initLivePreview({
|
|
300
|
-
// Your preview flag. Without it nothing starts.
|
|
301
|
-
enabled: preview,
|
|
302
|
-
// Runs a registered query against Prepr GraphQL. `query` is whatever
|
|
303
|
-
// you registered (string or DocumentNode) — you own the transport.
|
|
304
|
-
fetcher: async (query, variables) => {
|
|
305
|
-
/* POST to your Prepr GraphQL endpoint, return json.data */
|
|
306
|
-
},
|
|
307
|
-
});
|
|
308
|
-
}, [preview]);
|
|
309
|
-
return null;
|
|
279
|
+
{
|
|
280
|
+
preview && <PreprRefresh enabled />;
|
|
310
281
|
}
|
|
311
282
|
```
|
|
312
283
|
|
|
313
|
-
|
|
284
|
+
While an editor types, `PreprRefresh` sends the unsaved changes to a server
|
|
285
|
+
action and the route re-renders with them; on save it calls
|
|
286
|
+
`router.refresh()` for the stored content. Server components need nothing
|
|
287
|
+
else. Requires Next.js 16 or later; on Next.js 15 and older pages update on
|
|
288
|
+
save only.
|
|
289
|
+
|
|
290
|
+
**Client components** can skip the server round trip: wrap the data they
|
|
291
|
+
render in `useLive`, and keystrokes patch it in the browser. Import it from
|
|
292
|
+
`@preprio/toolkit/nextjs/live` — the `/nextjs` barrel also exports
|
|
293
|
+
server-only helpers (`next/headers`), which a client module graph must not
|
|
294
|
+
include.
|
|
314
295
|
|
|
315
296
|
```tsx
|
|
316
297
|
'use client';
|
|
317
298
|
import { useLive } from '@preprio/toolkit/nextjs/live';
|
|
318
299
|
|
|
319
|
-
export default function
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
variables: { slug },
|
|
323
|
-
});
|
|
324
|
-
// render from liveData — it re-renders on editor keystrokes and saves
|
|
300
|
+
export default function Hero({ hero }: { hero: HeroFragment }) {
|
|
301
|
+
const live = useLive(hero);
|
|
302
|
+
return <h1>{live.title}</h1>;
|
|
325
303
|
}
|
|
326
304
|
```
|
|
327
305
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
Omitting `query`/`variables` degrades that registration to keystroke pushes
|
|
333
|
-
only. For server-component pages, mount `<PreprRefresh enabled={preview} />`
|
|
334
|
-
instead — it calls `router.refresh()` on every save. Don't combine
|
|
335
|
-
`PreprRefresh` with `useLive` registrations that pass a `query`: a save would
|
|
336
|
-
both refetch and refresh. `useLive` without a `query` works alongside it.
|
|
337
|
-
|
|
338
|
-
Without `enabled`, and outside the Prepr editor iframe, all of this is a
|
|
339
|
-
passthrough with zero overhead. The CMS editor must emit `prepr:fieldChange` (per keystroke) and
|
|
340
|
-
`prepr:itemChanged` (on save, with the root content item id) into the preview
|
|
341
|
-
iframe — `examples/editor-emulator` emulates this for local development.
|
|
342
|
-
Unsaved structural changes (adding/reordering components) appear after save;
|
|
343
|
-
typed text is live immediately.
|
|
344
|
-
|
|
345
|
-
#### Unsaved changes in server components: `normalizeQuery` (experimental, Next.js 16+)
|
|
346
|
-
|
|
347
|
-
Server components show the editor's unsaved changes when you pass their query
|
|
348
|
-
results through `normalizeQuery`, with whatever client you fetch with:
|
|
306
|
+
**Client-fetched data** (no server components): instead of `PreprRefresh`,
|
|
307
|
+
call `initLivePreview({ enabled: preview, fetcher })` once in a client
|
|
308
|
+
component, and pass `{ query, variables }` to `useLive`. On save the toolkit
|
|
309
|
+
runs the query again through your `fetcher`:
|
|
349
310
|
|
|
350
|
-
```
|
|
351
|
-
|
|
311
|
+
```tsx
|
|
312
|
+
'use client';
|
|
313
|
+
import { useEffect } from 'react';
|
|
314
|
+
import { initLivePreview, useLive } from '@preprio/toolkit/nextjs/live';
|
|
352
315
|
|
|
353
|
-
|
|
316
|
+
export function LivePreviewInit({ preview }: { preview: boolean }) {
|
|
317
|
+
useEffect(() => {
|
|
318
|
+
initLivePreview({
|
|
319
|
+
enabled: preview,
|
|
320
|
+
// Runs a registered query against Prepr GraphQL; return `json.data`.
|
|
321
|
+
fetcher: (query, variables) => fetchFromPrepr(query, variables),
|
|
322
|
+
});
|
|
323
|
+
}, [preview]);
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
354
326
|
|
|
355
|
-
|
|
356
|
-
const
|
|
327
|
+
// In a client component:
|
|
328
|
+
const liveData = useLive(data, { query: GetPageDocument, variables: { slug } });
|
|
357
329
|
```
|
|
358
330
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
- The query (source text, a `DocumentNode`, or a typed document string) tells
|
|
364
|
-
the toolkit which response key holds which field. Changes land under
|
|
365
|
-
aliases, inside single components, on dynamic content entries, and on every
|
|
366
|
-
place the same item appears. Fields the query did not select are left
|
|
367
|
-
alone. Select `_id` on every item and component that should update.
|
|
368
|
-
- Changed text keeps its hidden edit link, so it stays click-to-edit.
|
|
369
|
-
- `useLive` data keeps the typed values across these re-renders.
|
|
370
|
-
|
|
371
|
-
**Production safety.** Pass `enabled` from the same preview flag you give the
|
|
372
|
-
middleware, and never `true` on a production deployment:
|
|
373
|
-
|
|
374
|
-
- With `enabled: false` (the default) the result is returned untouched and
|
|
375
|
-
the request is never read, so static and ISR pages stay static.
|
|
376
|
-
`PreprRefresh`, `initLivePreview` and `useLive` don't listen to the editor
|
|
377
|
-
at all.
|
|
378
|
-
- Next.js registers the toolkit's server action in every build that imports
|
|
379
|
-
`@preprio/toolkit/nextjs/live`, so anyone can call it, browsers and scripts
|
|
380
|
-
alike. It does nothing (no parsing, no storage, no re-render) unless the
|
|
381
|
-
Prepr middleware handled the request with `preview: true`. The middleware
|
|
382
|
-
drops `Prepr-*` headers a client sends, so this can't be forged on a page
|
|
383
|
-
the middleware runs for. On a page its `matcher` skips, a forged header can
|
|
384
|
-
at most trigger an extra render, never change content, because
|
|
385
|
-
`normalizeQuery` there still needs `enabled`.
|
|
386
|
-
- Changes only reach the re-render of the editor's own server action call.
|
|
387
|
-
The action stores them in the server process under a random, single-use
|
|
388
|
-
token and sets only that token as a cookie that expires immediately. Any
|
|
389
|
-
other request carrying the cookie, such as a plain or cacheable GET that
|
|
390
|
-
replays a token, sees nothing.
|
|
391
|
-
- Changes are only accepted from the Prepr editor (`https://*.prepr.io`, or
|
|
392
|
-
your `allowedEditorOrigins` via `initLivePreview`), never from another page
|
|
393
|
-
that frames your site. They are validated as hostile input: link fields
|
|
394
|
-
never take `javascript:`, `vbscript:` or `data:` URLs. HTML text is passed
|
|
395
|
-
on as typed, like rich text from the API.
|
|
396
|
-
|
|
397
|
-
On Next.js 15 and older the action does nothing and pages update on save.
|
|
398
|
-
|
|
399
|
-
For Astro and Nuxt, see their sections. The framework-free core exports,
|
|
400
|
-
`normalizeQuery(query, result, { log })`, `startHtmlLivePreview()` and
|
|
401
|
-
`readLiveLogFromRequest(request)` from `@preprio/toolkit`, cover any other
|
|
402
|
-
server-rendered stack.
|
|
331
|
+
Don't combine `PreprRefresh` with `useLive` registrations that pass a
|
|
332
|
+
`query`: a save would both refetch and refresh. `useLive` without a `query`
|
|
333
|
+
works alongside it.
|
|
403
334
|
|
|
404
335
|
## Astro
|
|
405
336
|
|
|
@@ -452,11 +383,11 @@ The `.astro` components ship as source, compiled by your own Astro/Vite pipeline
|
|
|
452
383
|
|
|
453
384
|
See the runnable example at `examples/astro` for the full source of truth.
|
|
454
385
|
|
|
455
|
-
### Live preview
|
|
386
|
+
### Live preview in Astro
|
|
456
387
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
388
|
+
To turn on [live preview](#live-preview), pass `preview` to `onPreprRequest`
|
|
389
|
+
as above, and run each query result through `normalizeQuery` with the same
|
|
390
|
+
flag:
|
|
460
391
|
|
|
461
392
|
```astro
|
|
462
393
|
---
|
|
@@ -472,13 +403,8 @@ const page = normalizeQuery(GetPageDocument, data, {
|
|
|
472
403
|
|
|
473
404
|
`PreprToolbar.astro` does the rest. Inside the Prepr editor it requests the
|
|
474
405
|
page again with the unsaved changes and updates only the parts of the DOM that
|
|
475
|
-
changed; on save it shows the stored content. Preview must render on
|
|
476
|
-
(`output: 'server'`).
|
|
477
|
-
|
|
478
|
-
Safety: the middleware only accepts changes in preview, from a same-origin
|
|
479
|
-
`POST` that carries the toolkit's header and a JSON body, and marks that
|
|
480
|
-
response `Cache-Control: private, no-store`. With `enabled: false` the result
|
|
481
|
-
is returned untouched. Changes are accepted only from the Prepr editor.
|
|
406
|
+
changed; on save it shows the stored content. Preview pages must render on
|
|
407
|
+
demand (`output: 'server'`, or `export const prerender = false`).
|
|
482
408
|
|
|
483
409
|
## SvelteKit
|
|
484
410
|
|
|
@@ -540,6 +466,35 @@ The `.svelte` components ship as source (compiled by your own Vite/Svelte pipeli
|
|
|
540
466
|
|
|
541
467
|
See the runnable example at `examples/sveltekit` for the full source of truth.
|
|
542
468
|
|
|
469
|
+
### Live preview in SvelteKit
|
|
470
|
+
|
|
471
|
+
To turn on [live preview](#live-preview), return your preview flag from
|
|
472
|
+
`load`, then run the query result through `normalizedQuery`. It applies the
|
|
473
|
+
editor's changes in the browser, with no server round trip:
|
|
474
|
+
|
|
475
|
+
```svelte
|
|
476
|
+
<script lang="ts">
|
|
477
|
+
import { invalidateAll } from '$app/navigation';
|
|
478
|
+
import { normalizedQuery } from '@preprio/toolkit/sveltekit/live';
|
|
479
|
+
|
|
480
|
+
let { data } = $props();
|
|
481
|
+
|
|
482
|
+
const result = $derived(
|
|
483
|
+
normalizedQuery(GetPageDocument, data.result, {
|
|
484
|
+
enabled: data.preview,
|
|
485
|
+
onSave: invalidateAll,
|
|
486
|
+
}),
|
|
487
|
+
);
|
|
488
|
+
</script>
|
|
489
|
+
|
|
490
|
+
<h1>{$result.Page.title}</h1>
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
`normalizedQuery` returns a Svelte store: render from `$result`. Pass the
|
|
494
|
+
result in the shape the query returns (`{ Page: … }`, not just the page), so
|
|
495
|
+
the toolkit can tell which response key holds which field. On save,
|
|
496
|
+
`invalidateAll` reruns your `load` functions for the stored content.
|
|
497
|
+
|
|
543
498
|
## Nuxt
|
|
544
499
|
|
|
545
500
|
Requires Nitro's Node.js preset (the Nuxt default).
|
|
@@ -591,24 +546,25 @@ The `.vue` components ship as source (compiled by your own Vite/Vue pipeline) an
|
|
|
591
546
|
|
|
592
547
|
See the runnable example at `examples/nuxt` for the full source of truth.
|
|
593
548
|
|
|
594
|
-
### Live preview
|
|
549
|
+
### Live preview in Nuxt
|
|
595
550
|
|
|
596
|
-
|
|
551
|
+
To turn on [live preview](#live-preview), run the `useAsyncData` result
|
|
552
|
+
through `useNormalizedQuery` with your preview flag. It applies the editor's
|
|
553
|
+
changes in the browser, with no server round trip:
|
|
597
554
|
|
|
598
555
|
```ts
|
|
599
556
|
import { useNormalizedQuery } from '@preprio/toolkit/nuxt/live';
|
|
600
557
|
|
|
601
558
|
const { data, refresh } = await useAsyncData('page', () => fetchPage(slug));
|
|
602
559
|
const page = useNormalizedQuery(GetPageDocument, data, {
|
|
603
|
-
enabled: preview,
|
|
560
|
+
enabled: preview,
|
|
604
561
|
onSave: refresh,
|
|
605
562
|
});
|
|
606
563
|
```
|
|
607
564
|
|
|
608
565
|
Render from `page`. Nuxt hydrates the whole page, so every component that
|
|
609
|
-
shows a changed field updates.
|
|
610
|
-
|
|
611
|
-
the Prepr editor.
|
|
566
|
+
shows a changed field updates. On save, `refresh` refetches the stored
|
|
567
|
+
content.
|
|
612
568
|
|
|
613
569
|
## React (no framework)
|
|
614
570
|
|
|
@@ -731,6 +687,115 @@ const controller = createPreprPreview({ props: toolbarProps });
|
|
|
731
687
|
|
|
732
688
|
Serialize the server-computed props into the page — a `<script type="application/json">` tag works well — and read them back on the client. See the runnable example at `examples/express` for the full source of truth.
|
|
733
689
|
|
|
690
|
+
## Live preview
|
|
691
|
+
|
|
692
|
+
Inside Prepr's side-by-side preview, live preview shows what an editor types
|
|
693
|
+
before they save: text, links and other fields update on the page as they
|
|
694
|
+
type, and a save shows the stored content. Outside the editor, and when it is
|
|
695
|
+
not enabled, it does nothing and costs nothing: the runtime loads only inside
|
|
696
|
+
the editor iframe.
|
|
697
|
+
|
|
698
|
+
| Framework | Entry point | Applies changes | Setup |
|
|
699
|
+
| --------- | --------------------------------- | ------------------------------------- | --------------------------------------- |
|
|
700
|
+
| Next.js | `@preprio/toolkit/nextjs/live` | On the server (`normalizeQuery`) | [Next.js](#live-preview-in-nextjs) |
|
|
701
|
+
| Astro | `@preprio/toolkit/astro` | On the server (`normalizeQuery`) | [Astro](#live-preview-in-astro) |
|
|
702
|
+
| SvelteKit | `@preprio/toolkit/sveltekit/live` | In the browser (`normalizedQuery`) | [SvelteKit](#live-preview-in-sveltekit) |
|
|
703
|
+
| Nuxt | `@preprio/toolkit/nuxt/live` | In the browser (`useNormalizedQuery`) | [Nuxt](#live-preview-in-nuxt) |
|
|
704
|
+
|
|
705
|
+
Other server-rendered stacks can use the framework-free core exports from
|
|
706
|
+
`@preprio/toolkit`: `readLiveLogFromRequest(request)` in the server handler,
|
|
707
|
+
`normalizeQuery(query, result, { log })` on each result, and
|
|
708
|
+
`startHtmlLivePreview()` in the browser. The Astro wrapper is built from
|
|
709
|
+
exactly these.
|
|
710
|
+
|
|
711
|
+
### Before you start
|
|
712
|
+
|
|
713
|
+
1. **Set up the toolbar for your framework first.** Live preview runs only
|
|
714
|
+
where the Prepr middleware handles the request with `preview: true`. Make
|
|
715
|
+
sure the middleware covers every page you preview: on a page it skips,
|
|
716
|
+
live preview does nothing and shows no error.
|
|
717
|
+
2. **Pass your preview flag as `enabled`** to every live preview entry point.
|
|
718
|
+
Each one does nothing without it. Never pass `true` on a production
|
|
719
|
+
deployment.
|
|
720
|
+
3. **Point Prepr at your preview deployment.** In Prepr, set the preview URL
|
|
721
|
+
to a deployment that uses the **GraphQL Preview** access token, with
|
|
722
|
+
**Enable edit mode** checked.
|
|
723
|
+
4. **Allow Prepr to frame your site.** If your server sends a
|
|
724
|
+
`Content-Security-Policy`, add your Prepr environments to
|
|
725
|
+
`frame-ancestors`, for example
|
|
726
|
+
`frame-ancestors https://acme.prepr.io https://dev-acme.prepr.io`.
|
|
727
|
+
|
|
728
|
+
### Preparing your queries
|
|
729
|
+
|
|
730
|
+
Live updates and click-to-edit find content by id, never by position, so
|
|
731
|
+
they stay correct while an editor adds, removes or reorders blocks before
|
|
732
|
+
saving:
|
|
733
|
+
|
|
734
|
+
- Select `_id` (and ideally `__typename`) on every model and component level
|
|
735
|
+
that should live-update. Items without `_id` stay static.
|
|
736
|
+
- In dynamic content fields, select `_id` on every entry type (`Text`,
|
|
737
|
+
`Quote`, …). `Assets` entries have no `_id` of their own: select
|
|
738
|
+
`items { _id }` instead.
|
|
739
|
+
- A `Text` entry's content updates whether the query selects `body`,
|
|
740
|
+
`html` or `text` (the latter with tags stripped).
|
|
741
|
+
- Render each dynamic content entry inside an element marked with
|
|
742
|
+
`data-prepr-edit-target="<field>.<index>"` and
|
|
743
|
+
`data-prepr-entry-id="<entry _id>"` (for `Assets`, the first asset's
|
|
744
|
+
`_id`). The index is the fallback when no id matches.
|
|
745
|
+
- Values without a hidden edit link (images, videos, other URLs) are made
|
|
746
|
+
click-to-edit with `previewAttrs({ id, field }, { enabled: preview })`
|
|
747
|
+
on the element that renders them.
|
|
748
|
+
- Where the page renders a linked item whose content comes from items nested
|
|
749
|
+
inside it (for example a personalization or a shared section referenced
|
|
750
|
+
from a stack), wrap it in an element with
|
|
751
|
+
`data-prepr-reference="<linked item _id>"`. The editor only knows the items
|
|
752
|
+
the open item links to directly; content inside the wrapper is outlined as
|
|
753
|
+
that linked item, and clicking it opens the link in the editor.
|
|
754
|
+
|
|
755
|
+
Typed text and changed fields show immediately. Structural changes, such as
|
|
756
|
+
adding or reordering components, appear after save.
|
|
757
|
+
|
|
758
|
+
### Options
|
|
759
|
+
|
|
760
|
+
`PreprRefresh`, `initLivePreview`, `normalizedQuery` and `useNormalizedQuery`
|
|
761
|
+
take the same two options as the toolbar (in Astro, set them on the
|
|
762
|
+
toolbar's `options`):
|
|
763
|
+
|
|
764
|
+
- **`allowedEditorOrigins`** — exact editor origins, replacing the
|
|
765
|
+
`https://*.prepr.io` default. Only needed when Prepr runs on your own domain.
|
|
766
|
+
- **`strictMode`** — set it when your preview token uses GraphQL strict mode
|
|
767
|
+
(required fields never null). The editor then holds back new entries until
|
|
768
|
+
their required fields are filled, and never sends a cleared required field
|
|
769
|
+
as null.
|
|
770
|
+
|
|
771
|
+
### Security
|
|
772
|
+
|
|
773
|
+
- **Disabled means untouched.** With `enabled: false` (the default) results
|
|
774
|
+
are returned as they are and the request is never read, so static and ISR
|
|
775
|
+
pages stay static.
|
|
776
|
+
- **Only the Prepr editor can send changes.** Changes are accepted only from
|
|
777
|
+
`https://*.prepr.io` (or your `allowedEditorOrigins`), never from another
|
|
778
|
+
page that frames your site. They are validated as hostile input: link
|
|
779
|
+
fields never take `javascript:`, `vbscript:` or `data:` URLs. HTML text is
|
|
780
|
+
passed on as typed, like rich text from the API.
|
|
781
|
+
- **One editor's changes never reach anyone else.** On the server-side
|
|
782
|
+
frameworks, changes are read only in preview. Astro accepts them only from
|
|
783
|
+
a same-origin `POST` carrying the toolkit's header and marks that response
|
|
784
|
+
`Cache-Control: private, no-store`. Next.js stores them in the server
|
|
785
|
+
process under a random, single-use token, so a replayed or cached request
|
|
786
|
+
sees nothing.
|
|
787
|
+
- **The Next.js server action is public but inert.** Next.js registers the
|
|
788
|
+
toolkit's server action in every build that imports
|
|
789
|
+
`@preprio/toolkit/nextjs/live`, so anyone can call it. It does nothing
|
|
790
|
+
unless the Prepr middleware handled the request with `preview: true`, and
|
|
791
|
+
the middleware drops `Prepr-*` headers a client sends.
|
|
792
|
+
|
|
793
|
+
### Trying it locally
|
|
794
|
+
|
|
795
|
+
The Next.js, Astro, SvelteKit and Nuxt examples all have live preview wired
|
|
796
|
+
up. Open them from a Prepr environment whose preview URL points at your local
|
|
797
|
+
server.
|
|
798
|
+
|
|
734
799
|
## API Reference
|
|
735
800
|
|
|
736
801
|
### Framework server helpers
|
|
@@ -892,15 +957,15 @@ createPreprMiddleware(request, response, { preview });
|
|
|
892
957
|
createPreprPreview({ props, options: { debug: true, locale: 'nl' } });
|
|
893
958
|
```
|
|
894
959
|
|
|
895
|
-
| Option | Type | Default | Description
|
|
896
|
-
| ---------------------- | ------------------------------ | ------------- |
|
|
897
|
-
| `debug` | `boolean` | `false` | Enable debug logging.
|
|
898
|
-
| `locale` | `'en' \| 'nl' \| 'fr' \| 'de'` | auto-detected | UI language. Falls back to the first supported browser language, then `en`. Inside the Prepr editor, the editor's interface language wins.
|
|
899
|
-
| `features` | `PreprFeatures` | all enabled | Which features run. See [Feature flags](#feature-flags).
|
|
900
|
-
| `ui` | `boolean` | `true` | Mount the visible toolbar. See [Headless preview](#headless-preview-no-toolbar-ui).
|
|
901
|
-
| `allowedEditorOrigins` | `string[]` | `*.prepr.io` | Exact editor origins allowed to drive this preview, for self-hosted or local editors. Replaces the wildcard. `['*']` accepts any origin — development only.
|
|
902
|
-
| `strictMode` | `boolean` | `false` | Your preview token uses GraphQL strict mode (required fields never null). The editor then holds back new entries until their required fields are filled and never sends a cleared required field as null. Also on `PreprRefresh`, `initLivePreview` and `useNormalizedQuery`. |
|
|
903
|
-
| `autoClean` | `boolean` | `true` | Strip the invisible stega characters from visible text. See [Stega auto-cleaning](#stega-auto-cleaning).
|
|
960
|
+
| Option | Type | Default | Description |
|
|
961
|
+
| ---------------------- | ------------------------------ | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
962
|
+
| `debug` | `boolean` | `false` | Enable debug logging. |
|
|
963
|
+
| `locale` | `'en' \| 'nl' \| 'fr' \| 'de'` | auto-detected | UI language. Falls back to the first supported browser language, then `en`. Inside the Prepr editor, the editor's interface language wins. |
|
|
964
|
+
| `features` | `PreprFeatures` | all enabled | Which features run. See [Feature flags](#feature-flags). |
|
|
965
|
+
| `ui` | `boolean` | `true` | Mount the visible toolbar. See [Headless preview](#headless-preview-no-toolbar-ui). |
|
|
966
|
+
| `allowedEditorOrigins` | `string[]` | `*.prepr.io` | Exact editor origins allowed to drive this preview, for self-hosted or local editors. Replaces the wildcard. `['*']` accepts any origin — development only. |
|
|
967
|
+
| `strictMode` | `boolean` | `false` | Your preview token uses GraphQL strict mode (required fields never null). The editor then holds back new entries until their required fields are filled and never sends a cleared required field as null. Also on `PreprRefresh`, `initLivePreview`, `normalizedQuery` and `useNormalizedQuery`. |
|
|
968
|
+
| `autoClean` | `boolean` | `true` | Strip the invisible stega characters from visible text. See [Stega auto-cleaning](#stega-auto-cleaning). |
|
|
904
969
|
|
|
905
970
|
`features` and `ui` are independent: `features` decides _what runs_, `ui` decides _whether the toolbar is visible_.
|
|
906
971
|
|
|
@@ -1036,6 +1101,14 @@ createPreprPreview({ props, options: { locale: 'nl' } });
|
|
|
1036
1101
|
- **API calls**: personalization only works if you spread `getPreprHeaders()` into your GraphQL fetch. Missing headers is the most common reason segments appear to have no effect.
|
|
1037
1102
|
- **Chained middleware**: when chaining, pass the existing `NextResponse` as the second argument. Creating a fresh `NextResponse.next()` afterwards discards Prepr's request headers.
|
|
1038
1103
|
|
|
1104
|
+
### Live preview does not update
|
|
1105
|
+
|
|
1106
|
+
- **Check `enabled`**: every live preview entry point needs your preview flag, and the middleware must run with `preview: true` for that page.
|
|
1107
|
+
- **Check the preview URL in Prepr**: it must point at a deployment that uses the GraphQL Preview token. A production build with preview off shows saved content only.
|
|
1108
|
+
- **Check `_id`**: an item or component whose query does not select `_id` stays static. See [Preparing your queries](#preparing-your-queries).
|
|
1109
|
+
- **Check the frame**: a `Refused to frame` error in the browser console means your `Content-Security-Policy` `frame-ancestors` does not list your Prepr environment.
|
|
1110
|
+
- **Next.js only shows changes on save**: `normalizeQuery` with `PreprRefresh` needs Next.js 16 or later.
|
|
1111
|
+
|
|
1039
1112
|
### Segments switch but content does not change
|
|
1040
1113
|
|
|
1041
1114
|
The toolbar sets the segment; your data fetch decides what to do with it. Check that the fetch forwards `getPreprHeaders()` and is not being served from cache — use `cache: 'no-store'`, or Apollo's `fetchPolicy: 'no-cache'`, on personalized queries.
|
package/dist/astro/index.cjs
CHANGED
package/dist/astro/index.js
CHANGED
package/dist/index.cjs
CHANGED
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 = "0.5.0-rc.
|
|
8
|
+
declare const VERSION = "0.5.0-rc.5";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Resolve a `PreprFeatures` bag to plain booleans, defaulting every omitted key
|
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 = "0.5.0-rc.
|
|
8
|
+
declare const VERSION = "0.5.0-rc.5";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Resolve a `PreprFeatures` bag to plain booleans, defaulting every omitted key
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
setTrackingParam,
|
|
18
18
|
stegaClean,
|
|
19
19
|
trackEvent
|
|
20
|
-
} from "./chunk-
|
|
20
|
+
} from "./chunk-SBLZANL5.js";
|
|
21
21
|
import {
|
|
22
22
|
PreprError,
|
|
23
23
|
extractAccessToken,
|
|
@@ -28,11 +28,11 @@ import {
|
|
|
28
28
|
getPreprUUIDFromHeaders,
|
|
29
29
|
getToolbarPropsFromHeaders,
|
|
30
30
|
validatePreprToken
|
|
31
|
-
} from "./chunk-
|
|
31
|
+
} from "./chunk-3X55JL52.js";
|
|
32
32
|
import {
|
|
33
33
|
VERSION,
|
|
34
34
|
resolveFeatures
|
|
35
|
-
} from "./chunk-
|
|
35
|
+
} from "./chunk-CMUFTCG3.js";
|
|
36
36
|
import {
|
|
37
37
|
createLiveBridge,
|
|
38
38
|
createLiveStore
|
|
@@ -3517,7 +3517,7 @@ function createPreprPreview(opts = {}) {
|
|
|
3517
3517
|
var import_react = require("react");
|
|
3518
3518
|
|
|
3519
3519
|
// src/version.ts
|
|
3520
|
-
var VERSION = "0.5.0-rc.
|
|
3520
|
+
var VERSION = "0.5.0-rc.5";
|
|
3521
3521
|
|
|
3522
3522
|
// src/core/pixel.ts
|
|
3523
3523
|
var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
|
package/dist/nextjs/index.cjs
CHANGED
|
@@ -49,7 +49,7 @@ module.exports = __toCommonJS(nextjs_exports);
|
|
|
49
49
|
var import_server = require("next/server");
|
|
50
50
|
|
|
51
51
|
// src/version.ts
|
|
52
|
-
var VERSION = "0.5.0-rc.
|
|
52
|
+
var VERSION = "0.5.0-rc.5";
|
|
53
53
|
|
|
54
54
|
// src/core/constants.ts
|
|
55
55
|
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
package/dist/nextjs/index.d.cts
CHANGED
|
@@ -311,6 +311,8 @@ interface LivePreviewConfig {
|
|
|
311
311
|
interface PreprRefreshProps {
|
|
312
312
|
/** Pass your preview flag; never `true` on a production deployment. */
|
|
313
313
|
enabled?: boolean;
|
|
314
|
+
/** Exact editor origins, replacing the `*.prepr.io` default. */
|
|
315
|
+
allowedEditorOrigins?: string[];
|
|
314
316
|
/** Your preview token uses GraphQL strict mode; see the toolbar option. */
|
|
315
317
|
strictMode?: boolean;
|
|
316
318
|
}
|
package/dist/nextjs/index.d.ts
CHANGED
|
@@ -311,6 +311,8 @@ interface LivePreviewConfig {
|
|
|
311
311
|
interface PreprRefreshProps {
|
|
312
312
|
/** Pass your preview flag; never `true` on a production deployment. */
|
|
313
313
|
enabled?: boolean;
|
|
314
|
+
/** Exact editor origins, replacing the `*.prepr.io` default. */
|
|
315
|
+
allowedEditorOrigins?: string[];
|
|
314
316
|
/** Your preview token uses GraphQL strict mode; see the toolbar option. */
|
|
315
317
|
strictMode?: boolean;
|
|
316
318
|
}
|