You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add first draft version
* Fix badge text position within tab items
* Update code & content
+ add noTooltip feature for badges
* Re-arrange sections
* Fix typo
* separate live preview section
* Use numbers instead of letters for numerals
* Update capitalization
* Add TODO for dark mode GIF
---------
Co-authored-by: Rémi de Juvigny <[email protected]>
@@ -293,80 +293,164 @@ On the Strapi side, [the `allowedOrigins` configuration parameter](#allowed-orig
293
293
294
294
This requires the front-end application to have its own header directive, the CSP `frame-ancestors` directive. Setting this directive up depends on how your website is built. For instance, setting this up in Next.js requires a middleware configuration (see [Next.js docs](https://nextjs.org/docs/app/building-your-application/configuring/content-security-policy)).
295
295
296
-
#### 6. [Front end]Detect changes in Strapi and refresh the front-end {#6-refresh-frontend}
296
+
#### 6. [Front end]Adapt data fetching for draft content {#6-fetch-draft-content}
297
297
298
-
Strapi emits a `strapiUpdate` message to inform the front end that data has changed.
298
+
Once the preview system is set up, you need to adapt your data fetching logic to handle draft content appropriately. This involves the following steps:
299
+
300
+
1. Create or adapt your data fetching utility to check if draft mode is enabled
301
+
2. Update your API calls to include the draft status parameter when appropriate
302
+
303
+
The following, taken from the <ExternalLinkto="https://github.com/strapi/LaunchPad/tree/feat/preview"text="Launchpad" /> Strapi demo application, is an example of how to implement draft-aware data fetching in your Next.js front-end application:
### Live Preview implementation <GrowthBadge/> <EnterpriseBadge />
348
+
349
+
After setting up the basic Preview feature, you can enhance the experience by implementing Live Preview.
350
+
351
+
#### Window messages
299
352
300
-
To track this, within your front-end application, add an event listener to listen to events posted through [the `postMessage()` API](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) on the `window` object. The listener needs to filter through messages and react only to Strapi-initiated messages, then refresh the iframe content.
353
+
Live Preview creates a more interactive experience by communicating between the admin and your frontend. It relies on events posted through [the `postMessage()` API](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) on the `window` object.
354
+
355
+
You need to add an event listener in your application. It should be present on all pages, ideally in a layout component that wraps your entire application. The listener needs to filter through messages and react only to Strapi-initiated messages.
356
+
357
+
There are 2 messages to listen to:
358
+
359
+
-`strapiUpdate`: sent by Strapi when a content update has been saved to the database. It's an opportunity to fetch the updated version of the content and refresh the preview. With Next.js, the recommended way to refresh the iframe content is with <ExternalLinkto="https://nextjs.org/docs/app/building-your-application/caching#routerrefresh"text="the `router.refresh()` method" />.
360
+
-`previewScript`: sent by Strapi to give you a script that powers the Live Preview functionality. This script should be injected into the page's `<head>` tag. It handles highlighting editable areas in the preview and sending messages back to Strapi when an area is double-clicked for editing.
361
+
362
+
In order to receive the `previewScript` message, you need to let Strapi know that your frontend is ready to receive it. This is done by posting a `previewReady` message to the parent window.
363
+
364
+
When putting it all together, a component ready to be added to your global layout could look like:
301
365
302
-
With Next.js, the recommended way to refresh the iframe content is with <ExternalLinkto="https://nextjs.org/docs/app/building-your-application/caching#routerrefresh"text="the `router.refresh()` method" />.
Live Preview is able to identify the parts of your frontend that correspond to fields in Strapi. This is done through content source maps, which are metadata encoded as hidden characters in your string-based content (e.g., text fields). It uses the <ExternalLink to="https://www.npmjs.com/package/@vercel/stega" text="@vercel/stega"/> library to encode and decode this metadata.
Metadatas will only be added in your Content API responses when the `strapi-encode-source-maps` header is set to `true`. You can set this header in your data fetching utility. Make sure to only pass the header when you detect that your site is rendered in a preview context.
For a Next.js application, you may use the `draftMode()` method from `next/headers` to detect if draft mode is enabled, and set the header accordingly in all your API calls:
388
471
389
-
```typescript {8-18}
472
+
```typescript {20-23}
390
473
import { draftMode } from"next/headers";
391
474
importqsfrom"qs";
392
475
@@ -395,7 +478,7 @@ export default async function fetchContentType(
- With the <GrowthBadge /> and <EnterpriseBadge /> plans, you get access to an enhanced experience called Live Preview. With Live Preview, you can see the Preview alongside the Edit view of the Content Manager, and you can also edit the content directly within the preview itself by double-clicking on any content.
439
518
:::
440
519
441
520
Once the Preview feature is properly set up, an **Open preview** button is visible on the right side of the [Content Manager's edit view](/cms/features/content-manager#overview). Clicking it will display the preview of your content as it will appear in your front-end application, but directly within Strapi's the admin panel.
@@ -444,25 +523,45 @@ Once the Preview feature is properly set up, an **Open preview** button is visib
- click the close button <Icon name="x" classes="ph-bold" /> in the upper left corner to go back to the Edit View of the Content Manager,
534
+
- switch between the Desktop and Mobile preview using the dropdown above the previewed content,
455
535
- switch between previewing the draft and the published version (if [Draft & Publish](/cms/features/draft-and-publish) is enabled for the content-type),
456
536
- and click the link icon <Icon name="link" classes="ph-bold"/> in the upper right corner to copy the preview link. Depending on the preview tab you are currently viewing, this will either copy the link to the preview of the draft or the published version.
457
537
458
-
Additionally, withLivePreview, youcan:
459
-
- with <GrowthBadge/> and <EnterpriseBadge /> plans, expandthesidepanelbyclickingonthe <Iconname="arrow-line-left"classes="ph-bold"/>buttontomakethePreviewfullscreen,
In the Edit view of the Content Manager, the Open preview button will be disabled if you have unsaved changes. Save your latest changes and you should be able to preview content again.
Live Preview is the enhanced Preview experience available with Strapi’s paid CMS plans.
546
+
547
+
With Live Preview, in addition to what’s included in the Free plan, you can:
548
+
549
+
* Use the Side Editor to view both the entry’s Edit view in the Content Manager and the front-end preview side by side. You can also switch between full-screen and side-by-side preview using the <Icon name="arrow-line-left" classes="ph-bold" /> and <Icon name="arrow-line-right" classes="ph-bold" /> buttons.
550
+
* Double-click any content in the preview pane to edit it in place. This opens a popover that syncs the front-end content with the corresponding field in Strapi.
This feature is currently experimental. Feel free to share <ExternalLink to="https://feedback.strapi.io/" text="feedback"/> or <ExternalLink to="https://github.com/strapi/strapi/issues" text="issues" /> with the Strapi team.
563
+
564
+
The current version of Live Preview comes with the following limitations:
565
+
* Blocks fields are not detected, and changing them in the Side Editor won’t be reflected in the preview. Clicking on Save after updates should however still work.
566
+
* Media assets and fields in dynamic zones are not handled.
0 commit comments