This is an example of Live Preview SSR implementation.
- Contentstack account
- Stack Api Key
- Publish Environment
- Contentstack Delivery Token
- Contentstack Management Token
Please check this for more info on this.
- Step 1: Create a content type with
single line textboxfield and then create a entry with this content type - Step 2: Rename
.env.samplefile to.envfile and add you credentials - Step 3: Install all the dependencies
npm install
- Step 4: Start the example application
npm start
Note: The example will start on
localhost:3001
-
contentstack.config.jsThis file contain configuration related toContentstak SDKandContentstack live preview SDK -
app.jsThis file contain a middleware for live previewapp.use((req,res,next) => { Stack.livePreviewQuery(req.query); next(); })
-
routes/index.jsThis file contain code for fetching data from contentstack sdk and passing it to the ejs file. This file also contain a code related to live preview edit button functionalities. -
views/index.ejsThis file contain code the initialization of live preview for the route.<script src="https://unpkg.com/@contentstack/live-preview-utils@1.3.2/dist/index.js"></script> <link rel="stylesheet" href="https://unpkg.com/@contentstack/live-preview-utils/dist/main.css" />
ContentstackLivePreview.init({ cleanCslpOnProduction: true, enable: true, stackDetails: { apiKey: '<%= process.env.CONTENTSTACK_API_KEY %>', environment: '<%= process.env.CONTENTSTACK_ENVIRONMENT %>' }, clientUrlParams: { host: "app.contentstack.com", }, editButton: { enable: true, exclude: ["outsideLivePreviewPortal"], includeByQueryParameter: false, position: 'top-center', } });