Skip to content

Commit 67d5c90

Browse files
Live preview using SSR mode
1 parent 1c9441e commit 67d5c90

File tree

3 files changed

+30
-48
lines changed

3 files changed

+30
-48
lines changed

next.config.sample.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// filename should be next.config.js
2+
module.exports = {
3+
env: {
4+
api_key: "api_key",
5+
delivery_token: "delivery_token",
6+
environment: "environment",
7+
management_token: "management_token",
8+
content_type_uid: "content_type_uid",
9+
entry_uid: "entry_uid"
10+
}
11+
}
12+

pages/api/utils.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Contentstack from 'contentstack'
22
import ContentstackLivePreview from "@contentstack/live-preview-utils";
3-
console.log("params", {
3+
4+
export const Stack = Contentstack.Stack(
5+
{
46
api_key: process.env.api_key,
57
delivery_token: process.env.delivery_token,
68
environment: process.env.environment,
@@ -9,22 +11,10 @@ console.log("params", {
911
enable: true,
1012
host: 'api.contentstack.io'
1113
}
12-
})
13-
export const Stack = Contentstack.Stack(
14-
{
15-
api_key: process.env.api_key,
16-
delivery_token: process.env.delivery_token,
17-
environment: process.env.environment,
18-
// live_preview: {
19-
// // management_token: process.env.management_token,
20-
// enable: true,
21-
// host: 'api.contentstack.io'
22-
// }
2314
}
2415
)
2516

26-
2717
Stack.setHost("api.contentstack.io")
28-
ContentstackLivePreview.init({enable: true, stackSdk: Stack, ssr:false});
29-
30-
export const onEntryChange = ContentstackLivePreview.onEntryChange;
18+
ContentstackLivePreview.init({enable: true,ssr: true, debug: true, stackDetails: {
19+
apiKey: process.env.api_key
20+
}});

pages/index.js

+12-32
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
1-
import Contentstack from "contentstack"
2-
import { useEffect, useState } from "react";
3-
import { onEntryChange } from "./api/utils";
41
import {Stack} from './api/utils'
52

6-
7-
// let apiUrl = `https://api.contentstack.io/v3/content_types/ashwini_live_preview/entries/blt8a69169a533e195a?`
8-
// const Stack = Contentstack.Stack(
9-
// {
10-
// api_key: process.env.api_key,
11-
// delivery_token: process.env.delivery_token,
12-
// environment: process.env.environment
13-
// }
14-
// );
15-
16-
173
function fetchData (contentType, entryId){
184
try{
195
if(entryId){
206
let Query = Stack.ContentType(contentType).Entry(entryId);
21-
let entry = Query.fetch()
7+
return Query.fetch()
228
.then(function success(entry) {
239
return entry.toJSON();
2410
}, function error(err) {
2511
console.log("Error", err);
2612
});
27-
return entry;
2813
}
2914
else{
3015
let Query = Stack.ContentType(contentType).Query();
@@ -40,29 +25,24 @@ function fetchData (contentType, entryId){
4025
}
4126

4227
export default function Home(props) {
43-
console.log("PROPS", props)
44-
const [entry, setEntry] = useState(props)
45-
46-
const updatedData = async() => {
47-
let updatedEntry = await fetchData("ashwini_live_preview", "blt8a69169a533e195a")
48-
setEntry(updatedEntry)
49-
}
50-
51-
useEffect(() => {
52-
onEntryChange(updatedData)
53-
}, [])
54-
console.log("updated entry", entry)
28+
const {entry} = props
5529
return (
5630
<div>
57-
<h1>{"HELLO WORLD"}</h1>
31+
<h1>{entry?.title}</h1>
32+
<p>{entry?.single_line}</p>
5833
</div>
5934
)
6035
}
6136

62-
export const getStaticProps = async (context) => {
63-
let entry = await fetchData("ashwini_live_preview", "blt8a69169a533e195a");
37+
export const getServerSideProps = async (context) => {
38+
if(context.query){
39+
Stack.livePreviewQuery(context.query)
40+
}
41+
let entry = await fetchData(process.env.content_type_uid, process.env.entry_uid);
6442
return { props:
65-
{...entry}
43+
{
44+
entry
45+
}
6646
}
6747

6848
}

0 commit comments

Comments
 (0)