Add a preview button and live view button to the content manager edit view to connect with your frontend app.
A plugin for Strapi CMS that adds a preview button and live view button to the content manager edit view.
@strapi/plugin-i18n
plugin.yarn add strapi-plugin-preview-button@latest
Don't forget to restart or rebuild your Strapi app when installing a new plugin.
property | type (default) | description |
---|---|---|
contentTypes | array ([] ) | An array of objects describing which content types should use the preview button. |
contentTypes[].uid | string | The uid value of either a single or collection type. |
contentTypes[].draft | object ({} ) | A configuration object to enable a draft preview button. |
contentTypes[].published | object ({} ) | A configuration object to enable a live view button. |
contentTypes
An array of objects describing which content types should use the preview button.
Each object in the array requires the uid
as well as draft
and/or published
props, which require a url
prop at minimum.
property | type (default) | description |
---|---|---|
url | string | The destination URL. See section about mapping data into the URLs for greater customization. |
query | object ({} ) | Optional query string params to include in the final URL. |
openTarget | string (StrapiPreview ) | Set to any custom string. Optionally set to _blank to always open the preview page in a new tab or window. Otherwise the preview will re-use the same preview tab or window. |
copy | boolean (true ) | Set to false to disable the copy link button that appears below the preview button. |
alwaysVisible | boolean (false ) | Set to true to enable both preview and live buttons to render together in a published state. This option only applies to the draft prop. |
Consider we have a Home
single type as well as Page
and Post
collection types, where each has a simple title
and content
field. The Page
and Post
models will also use a uid
field named slug
so they can create many entries with unique paths. Here is the minimum recommended config for this scenario.
1// ./config/plugins.js
2'use strict';
3
4module.exports = {
5 'preview-button': {
6 config: {
7 contentTypes: [
8 {
9 uid: 'api::home.home',
10 published: {
11 url: 'http://localhost:3000',
12 },
13 },
14 {
15 uid: 'api::page.page',
16 draft: {
17 url: 'http://localhost:3000/api/preview',
18 query: {
19 type: 'page',
20 slug: '{slug}',
21 },
22 },
23 published: {
24 url: 'http://localhost:3000/{slug}',
25 },
26 },
27 {
28 uid: 'api::post.post',
29 draft: {
30 url: 'http://localhost:3000/api/preview',
31 query: {
32 type: 'post',
33 slug: '{slug}',
34 },
35 },
36 published: {
37 url: 'http://localhost:3000/blog/{slug}',
38 },
39 },
40 ],
41 },
42 },
43};
In this example, the Home
page is a single type and does not have draftAndPublish
enabled so we do not use the draft
prop.
For Pages
and Posts
, the slug
value from the entry data is dynamically populating into the url
and query
props. This allows you to populate any number
or string
property from the entry data into the preview URL by using curly braces around the property name.
This configuration will result in the following preview URLs for Home
, Pages
, and Posts
.
1// Draft URLs
2http://localhost:3000/api/preview?slug=my-page&type=page
3http://localhost:3000/api/preview?slug=my-post&type=post
4
5// Published URLs
6http://localhost:3000/
7http://localhost:3000/my-page
8http://localhost:3000/blog/my-post
The
query
prop can actually take any params you want to pass to your frontend app.
By using {curly_braces}
, you can map values from the entry data into your preview URLs to customize the URL however you like.
For example, depending on how you are choosing to handle your preview method, you could pass an id
value to your draft preview but pass a slug
value to your live view.
Unmatched values will be replaced with an empty string.
1// ./config/plugins.js
2{
3 uid: 'api::page.page',
4 draft: {
5 url: 'http://localhost:3000/api/preview',
6 query: {
7 type: 'page',
8 id: '{id}',
9 },
10 },
11 published: {
12 url: 'http://localhost:3000/{slug}',
13 },
14}
If you have localization enabled for a content type, the locale
value will be included in the entry data and replaced like the rest. You can simply use {locale}
to include it where you like in the URL or query string.
1// ./config/plugins.js
2{
3 uid: 'api::page.page',
4 draft: {
5 url: 'http://localhost:3000/api/preview',
6 query: {
7 type: 'page',
8 locale: '{locale}',
9 slug: '{slug}',
10 },
11 },
12 published: {
13 url: 'http://localhost:3000/{locale}/{slug}',
14 },
15}
You can optionally use a secret key with your preview URLs by taking advantage of environment vars and the query
prop. See example below.
1// ./config/plugins.js
2'use strict';
3
4module.exports = ({ env }) => ({
5 'preview-button': {
6 config: {
7 contentTypes: [
8 {
9 uid: 'api::page.page',
10 draft: {
11 url: 'http://localhost:3000/api/preview',
12 query: {
13 type: 'page',
14 slug: '{slug}',
15 secret: env('STRAPI_PREVIEW_SECRET'),
16 },
17 },
18 published: {
19 url: 'http://localhost:3000/{slug}',
20 },
21 },
22 ],
23 },
24 },
25});
This configuration will result in the following preview URLs for Pages
.
1// Draft URL
2http://localhost:3000/api/preview?slug=my-page&type=page&secret=YOUR_SECRET_KEY
3
4// Published URL
5http://localhost:3000/my-page
Before granting access to the preview in your frontend app, you will want to compare and validate the secret key between both Strapi and frontend apps.
It may be important to note that this plugin does not offer any validation or other handling for a secret key. The goal of including a secret key is simply to give your frontend app some way of "shaking hands" with your backend app to approve of the preview access.
By default this value is set to StrapiPreview
but it can be any custom string. It is used in the window.open
function for the preview button to always open in the same tab.
If you would rather have the preview button always open in a new tab, you could use _blank
as the value. Special target keywords such as _blank
, _top
, _self
, or _parent
are also acceptable values.
1// ./config/plugins.js
2{
3 uid: 'api::page.page',
4 draft: {
5 url: 'http://localhost:3000/api/preview',
6 query: {
7 type: 'page',
8 slug: '{slug}',
9 },
10 openTarget: '_blank',
11 },
12 published: {
13 url: 'http://localhost:3000/{slug}',
14 openTarget: '_blank',
15 },
16},
You could also use a different openTarget
for each model schema if you wanted to have each collection type open in it's own designated tab or window, like in the example below.
1// ./config/plugins.js
2'use strict';
3
4module.exports = {
5 'preview-button': {
6 config: {
7 contentTypes: [
8 {
9 uid: 'api::page.page',
10 draft: {
11 url: 'http://localhost:3000/api/preview',
12 query: {
13 type: 'page',
14 slug: '{slug}',
15 },
16 openTarget: 'StrapiPreviewPage',
17 },
18 published: {
19 url: 'http://localhost:3000/{slug}',
20 openTarget: 'StrapiPage',
21 },
22 },
23 {
24 uid: 'api::post.post',
25 draft: {
26 url: 'http://localhost:3000/api/preview',
27 query: {
28 type: 'post',
29 slug: '{slug}',
30 },
31 openTarget: 'StrapiPreviewPost',
32 },
33 published: {
34 url: 'http://localhost:3000/blog/{slug}',
35 openTarget: 'StrapiPost',
36 },
37 },
38 ],
39 },
40 },
41};
The "copy link" button located beneath the preview button can be disabled with the copy: false
prop applied to draft
and published
configurations. This value is true
by default.
1// ./config/plugins.js
2{
3 uid: 'api::home.home',
4 published: {
5 url: 'http://localhost:3000',
6 copy: false,
7 },
8}
In published mode, the preview button changes to a "live view" button. If you want to keep the preview button alongside the live view button in published mode, set draft.alwaysVisible
to true
.
In draft mode, the live view buttons will not appear.
1// ./config/plugins.js
2{
3 uid: 'api::page.page',
4 draft: {
5 url: 'http://localhost:3000/api/preview',
6 query: {
7 type: 'page',
8 slug: '{slug}',
9 },
10 openTarget: 'StrapiPreviewPage',
11 alwaysVisible: true,
12 },
13 published: {
14 url: 'http://localhost:3000/{slug}',
15 openTarget: 'StrapiPage',
16 },
17}
property | type (default) | description |
---|---|---|
listViewColumn | boolean (true ) | Set to false to disable the preview and copy link buttons from displaying in list view. |
listViewColumn
Set to false
to disable the preview and copy link buttons from displaying in list view.
1// ./src/api/page/content-types/page/schema.js
2{
3 "kind": "collectionType",
4 "collectionName": "pages",
5 "info": {
6 "singularName": "page",
7 "pluralName": "pages",
8 "displayName": "Page",
9 "description": ""
10 },
11 "options": {
12 "draftAndPublish": true
13 },
14 "pluginOptions": {
15 "preview-button": {
16 "listViewColumn": false
17 }
18 },
19 "attributes": {
20 // etc.
21 }
22}
Ideally, the preview and copy link buttons in list view should appear alongside the other action icons for each row in the table. However, Strapi does not currently provide a hook to append new icons to that column. For now, this plugin will add its own "Preview" column with the extra icon actions.
If you need to apply more advanced logic to the preview URL, you can accomplish this with the plugin/preview-button/before-build-url
hook included with this plugin.
Your Strapi app will need a custom plugin in order to use this hook.
See Plugins Development in Strapi docs for more info.
In this example, we will create the bare minimum for a Strapi plugin that allows us to run our custom hook. The file structure for the plugin will look like the code below.
1/src/plugins/example
2 /admin
3 /src
4 index.js
5 package.json
6 strapi-admin.js
The package.json
is required for a Strapi plugin.
1// ./package.json
2{
3 "name": "example",
4 "version": "0.1.0",
5 "description": "Example.",
6 "strapi": {
7 "displayName": "Example",
8 "name": "example",
9 "description": "Example",
10 "kind": "plugin"
11 },
12 "dependencies": {}
13}
1// ./strapi-admin.js
2'use strict';
3
4module.exports = require('./admin/src').default;
In the main plugin file below, we register the plugin in the register
method and we register the hook with the bootstrap
method.
The hook provides draft
and published
parameters which are the same as the UID configs from config/plugins.js
. So if you are editing a Page
, you will get the draft
and published
configs for api::page.page
from your plugin config passed into the callback.
Here you will modify and return draft
and published
while using data
however you like. In this example, we are just adding on a foo=bar
query parameter to demonstrate how this hook can be utilized for more dynamic URLs.
1// ./admin/src/index.js
2export default {
3 register(app) {
4 app.registerPlugin({
5 id: 'example',
6 name: 'example',
7 });
8 },
9
10 bootstrap(app) {
11 app.registerHook('plugin/preview-button/before-build-url', ({ data, draft, published }) => {
12 const draftQuery = draft?.query ?? {};
13
14 // Return an object with modified `draft` and `published` props using `data` however you like.
15 return {
16 draft: {
17 ...draft,
18 query: {
19 ...draftQuery,
20 foo: 'bar',
21 },
22 },
23 published,
24 };
25 });
26 },
27};
Finally, don't forget to enable your plugin in your app by adding it to config/plugins.js
.
1// ./config/plugins.js
2'use strict';
3
4module.exports = {
5 example: {
6 enabled: true,
7 resolve: './src/plugins/example',
8 },
9};
The Open live view button will lead directly to the live page URL.
The Open draft preview button should lead to an endpoint that redirects to the appropriate preview page based on the query parameters passed to it.
For in-depth examples and instructions, please reference the links below to learn how this can be accomplished with Next.js and Strapi.
Remember to rebuild your app after making changes to some config or other code.
yarn build
# OR
yarn develop
Follow the migration guides to keep your preview button plugin up-to-date.
If you are enjoying this plugin and feel extra appreciative, you can buy me a beer or 3 🍺🍺🍺.
npm install strapi-plugin-preview-button
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.