@contentstack/delivery-plugin-release-preview
v1.0.6
Published
This is a SDK plugin for [Release Preview](https://docs.google.com/document/d/1XQD4o1aFjhkF8NU5_sVXjWqwHtnv0WydrKfwWHrqkPM/edit#) App. This plugin allows you to preview your Contentstack website for the upcoming releases. Also, it will provide the Compare
Downloads
877
Readme
Description
This is a SDK plugin for Release Preview App. This plugin allows you to preview your Contentstack website for the upcoming releases. Also, it will provide the Compare Release Preview Setup to compare the current website with the upcoming release website.
About Release Preview App
The Release Preview app displays the release date and time in a calendar format within the Contentstack Dashboard. It also includes a release timeline view that shows all the release specific details such as entries or assets that are added to a release with their specific titles, content types, and versions within your Contentstack environment.
How to use Release Preview SDK Plugin
Here is a detailed step-by-step guide on how to set up Release Preview for your stack using two set up methods:
- Client-side Rendering (CSR)
- Server-side Rendering (SSR)
1. Client-side Rendering (CSR):
a. Installation:
To install it, you can either use npm or import it using the script tag in your HTML page code.
- Using script tag: To import the Release Preview Middleware using the script tag of the HTML file, add the following code:
<script src="https://unpkg.com/@contentstack/release-preview-middleware/dist/index.js"></script>
- Using npm: Alternatively, you can install the Release Preview Middleware package via npm using the following command:
npm install @contentstack/delivery-plugin-release-preview
b. Import the required functions from the above installed package.
// sdk.ts
import { releaseReplaceAlgorithm, releaseReplacePreReq } from "@contentstack/delivery-plugin-release-preview";
c. Create and add the Release Preview plugin to the Contentstack.Stack() method of Contentstack's Delivery SDK.
// sdk.ts
class ReleasePreviewPlugin {
onRequest (stack, request) {
releaseReplacePreReq(stack, request);
return request
}
async onResponse(stack, request, response, data) {
const _data = data['entries'] || data['assets'] || data['entry'] || data['asset'];
await releaseReplaceAlgorithm(_data, stack);
return data
}
}
export const stack = contentstack.Stack({
/* YOUR CONFIG HERE */,
live_preview: {
/* YOUR LIVE PREVIEW CONFIG HERE */
/* NOTE: Release preview also relies on this config to work */
},
plugins: [
new ReleasePreviewPlugin()
]
});
d. Store the Release Preview config in a session:
Create an utility function named getReleasePreviewSession which remembers the Release Preview related query parameters so that Release Preview still works when page location changes.
// utils.ts
export const getReleasePreviewSession = (searchParams: URLSearchParams) => {
const release_session_key = 'release_preview_session';
const tentativeReleases = searchParams.get('tentativeReleases');
const release_id = searchParams.get('release_preview_id');
const params = {
enabled: true,
release_id,
tentativeReleases: tentativeReleases && JSON.parse(decodeURIComponent(tentativeReleases)),
}
const releaseSessionInfo = sessionStorage.getItem(release_session_key);
if(release_id || !releaseSessionInfo) {
sessionStorage.setItem(release_session_key, JSON.stringify(params));
return params;
}
return JSON.parse(releaseSessionInfo);
}
e. Initialise ReleasePreview at root level of website/app before any stack request methods are called. Using ReleasePreview.init(stack, config)
method from @contentstack/delivery-plugin-release-preview
, pass the stack and config created in previous steps as parameters.
import ReleasePreview from "@contentstack/delivery-plugin-release-preview";
import { useSearchParams } from "react-router-dom";
import { stack } from "./sdk";
import { getReleasePreviewSession } from "./utils";
function App() {
const [isLoading, setLoading] = useState(true);
const [searchParams] = useSearchParams();
useEffect(() => {
(async () => {
const release_preview_options = getReleasePreviewSession(searchParams);
/* stack here is the created Contentstack Stack Instance */
await ReleasePreview.init(stack, release_preview_options);
setLoading(false);
})()
}, []);
return (isLoading ? null : /*YOUR COMPONENT HERE*/)
}
Example Code:
// sdk.ts
import * as contentstack from "contentstack";
import { releaseReplaceAlgorithm, releaseReplacePreReq } from "@contentstack/delivery-plugin-release-preview";
class ReleasePreviewPlugin {
onRequest(stack, request) {
releaseReplacePreReq(stack, request);
return request
}
async onResponse(stack, request, response, data) {
const _data =
data["entries"] || data["assets"] || data["entry"] || data["asset"];
await releaseReplaceAlgorithm(_data, stack);
return data;
}
}
export const stack = contentstack.Stack({
/* YOUR CONFIG HERE */,
plugins: [new ReleasePreviewPlugin()],
});
// utils.ts
export const getReleasePreviewSession = (searchParams: URLSearchParams) => {
const release_session_key = 'release_preview_session';
const tentativeReleases = searchParams.get('tentativeReleases');
const release_id = searchParams.get('release_preview_id');
const params = {
enabled: true,
release_id,
tentativeReleases: tentativeReleases && JSON.parse(decodeURIComponent(tentativeReleases)),
}
const releaseSessionInfo = sessionStorage.getItem(release_session_key);
if(release_id || !releaseSessionInfo) {
sessionStorage.setItem(release_session_key, JSON.stringify(params));
return params;
}
return JSON.parse(releaseSessionInfo);
}
// App.tsx
import ReleasePreview from "@contentstack/delivery-plugin-release-preview";
import { useSearchParams } from "react-router-dom";
import { stack } from "./sdk";
import { getReleasePreviewSession } from "./utils";
function App() {
const [isLoading, setLoading] = useState(true);
const [searchParams] = useSearchParams();
useEffect(() => {
(async () => {
const release_preview_options = getReleasePreviewSession(searchParams);
/* stack here is the created Contentstack Stack Instance */
await ReleasePreview.init(stack, release_preview_options);
setLoading(false);
})()
}, []);
return (isLoading ? null : /*YOUR COMPONENT HERE*/)
}
2. Server-side Rendering (SSR):
a. Installation:
To install it, you can either use npm or import it using the script tag in your HTML page code.
- Using script tag: To import the Release Preview Middleware using the script tag of the HTML file, add the following code:
<script src="https://unpkg.com/@contentstack/release-preview-middleware/dist/index.js"></script>
- Using npm: Alternatively, you can install the Release Preview Middleware package via npm using the following command:
npm install @contentstack/delivery-plugin-release-preview
b. Import the required functions from the above installed package.
// sdk.js
const { releaseReplaceAlgorithm, releaseReplacePreReq } = require("@contentstack/delivery-plugin-release-preview");
c. Create and add the Release Preview plugin to the Contentstack.Stack() method of Contentstack's Delivery SDK.
// sdk.js
class ReleasePreviewPlugin {
onRequest (stack, request) {
releaseReplacePreReq(stack, request);
return request;
}
async onResponse(stack, request, response, data) {
const _data = data['entries'] || data['assets'] || data['entry'] || data['asset'];
await releaseReplaceAlgorithm(_data, stack);
return data;
}
}
export const stack = contentstack.Stack({
/* YOUR CONFIG HERE */,
live_preview: {
/* YOUR LIVE PREVIEW CONFIG HERE */
/* NOTE: Release preview also relies on this config to work */
},
plugins: [
new ReleasePreviewPlugin()
]
});
d. Store the Release Preview config in a session:
Create an utility function named getReleasePreviewSession which remembers the Release Preview related query parameters so that Release Preview still works when page location changes.
// utils.js
const release_session_key = 'release_preview_session';
export const getReleasePreviewSession = (req, res) => {
const searchParams = req.query;
const tentativeReleases = searchParams['tentativeReleases'];
const release_id = searchParams['release_preview_id'];
const params = {
enabled: true,
release_id,
tentativeReleases: tentativeReleases && JSON.parse(decodeURIComponent(tentativeReleases)),
}
const releaseSessionInfo = req.session[release_session_key];
if(release_id || !releaseSessionInfo) {
req.session[release_session_key] = JSON.stringify(params);
return params;
}
return JSON.parse(releaseSessionInfo);
}
e. Initialize Release Preview Middleware:
Setup the Release Preview plugin to process the response with release specific content. Create config using the query params passed for Release Preview and pass as parameter to the ReleasePreview.init(stack, config)
method from @contentstack/delivery-plugin-release-preview
along with the stack instance created in the previous step.
// app.js
const { default: ReleasePreview } = require('@contentstack/delivery-plugin-release-preview');
const { stack } = require('./sdk');
const { getReleasePreviewSession } = require('./utils');
app.use(async (req, res, next) => {
try {
const release_preview_options = getReleasePreviewSession(req, res)
await ReleasePreview.init(stack, release_preview_options);
} catch(err) {
console.error('error while setting release preview', err);
}
next();
})
Example Code:
//sdk.js
const contentstack = require("contentstack");
const { releaseReplaceAlgorithm, releaseReplacePreReq } = require("@contentstack/delivery-plugin-release-preview");
class ReleasePreviewPlugin {
onRequest (stack, request) {
releaseReplacePreReq(stack, request);
return request;
}
async onResponse(stack, request, response, data) {
const _data = data['entries'] || data['assets'] || data['entry'] || data['asset'];
await releaseReplaceAlgorithm(_data, stack);
return data
}
}
export const stack = contentstack.Stack({
/* YOUR CONFIG HERE */,
live_preview: {
/* YOUR LIVE PREVIEW CONFIG HERE */
/* NOTE: Release preview also relies on this config to work */
},
plugins: [
new ReleasePreviewPlugin()
]
});
// utils.js
const release_session_key = 'release_preview_session';
export const getReleasePreviewSession = (req, res) => {
const searchParams = req.query;
const tentativeReleases = searchParams['tentativeReleases'];
const release_id = searchParams['release_preview_id'];
const params = {
enabled: true,
release_id,
tentativeReleases: tentativeReleases && JSON.parse(decodeURIComponent(tentativeReleases)),
}
const releaseSessionInfo = req.session[release_session_key];
if(release_id || !releaseSessionInfo) {
req.session[release_session_key] = JSON.stringify(params);
return params;
}
return JSON.parse(releaseSessionInfo);
}
// app.js
const { default: ReleasePreview } = require('@contentstack/delivery-plugin-release-preview');
const { stack } = require('./sdk');
const { getReleasePreviewSession } = require('./utils')
app.use(async (req, res, next) => {
try {
const release_preview_options = getReleasePreviewSession(req, res)
await ReleasePreview.init(stack, release_preview_options);
} catch(err) {
console.error('error while setting release preview', err);
}
next();
})
You can use this plugin to configure and preview your website in the Release Preview Dashboard.
Compare Release Preview Setup
The Compare Utils SDK listens to compare requests from Release Preview App compare view. Therefore, this SDK needs to be executed on the client side.
To install it, you can either use npm or import it directly in your HTML page code.
Using html tag: To import the Release Preview Utils SDK JavaScript and CSS code, add the following lines in your html page:
<link src="https://cdn.jsdelivr.net/npm/@contentstack/delivery-plugin-release-preview/dist/compareUtilsStyle.css" />
<script src="https://cdn.jsdelivr.net/npm/@contentstack/delivery-plugin-release-preview/dist/compareUtils.browser.min.js"></script>
Using npm:
import '@contentstack/delivery-plugin-release-preview/dist/compareUtils.browser.min.js'
import '@contentstack/delivery-plugin-release-preview/dist/compareUtilsStyle.css'
Now you can compare the website changes between the current release and the upcoming release from the Release Preview Full Page Widget.