Skip to content

Commit 278bae3

Browse files
initial commit for live preview ssr
1 parent 0bea113 commit 278bae3

File tree

3 files changed

+67
-65
lines changed

3 files changed

+67
-65
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ yarn-error.log*
3232

3333
# vercel
3434
.vercel
35+
36+
next.config.js

next.config.js

-3
This file was deleted.

pages/index.js

+65-62
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,72 @@
1-
import Head from 'next/head'
2-
import Image from 'next/image'
3-
import styles from '../styles/Home.module.css'
1+
import Contentstack from "contentstack"
2+
import { useEffect } from "react";
43

5-
export default function Home() {
6-
return (
7-
<div className={styles.container}>
8-
<Head>
9-
<title>Create Next App</title>
10-
<meta name="description" content="Generated by create next app" />
11-
<link rel="icon" href="/favicon.ico" />
12-
</Head>
13-
14-
<main className={styles.main}>
15-
<h1 className={styles.title}>
16-
Welcome to <a href="https://nextjs.org">Next.js!</a>
17-
</h1>
18-
19-
<p className={styles.description}>
20-
Get started by editing{' '}
21-
<code className={styles.code}>pages/index.js</code>
22-
</p>
23-
24-
<div className={styles.grid}>
25-
<a href="https://nextjs.org/docs" className={styles.card}>
26-
<h2>Documentation &rarr;</h2>
27-
<p>Find in-depth information about Next.js features and API.</p>
28-
</a>
4+
const Stack = Contentstack.Stack(
5+
{
6+
api_key: process.env.api_key,
7+
delivery_token: process.env.delivery_token,
8+
environment: process.env.environment
9+
}
10+
);
2911

30-
<a href="https://nextjs.org/learn" className={styles.card}>
31-
<h2>Learn &rarr;</h2>
32-
<p>Learn about Next.js in an interactive course with quizzes!</p>
33-
</a>
12+
function fetchData (contentType, entryId){
13+
try{
14+
if(entryId){
15+
let Query = Stack.ContentType(contentType).Entry(entryId);
16+
let entry = Query.fetch()
17+
.then(function success(entry) {
18+
return entry.toJSON();
19+
}, function error(err) {
20+
console.log("Error", err);
21+
});
22+
return entry;
23+
}
24+
else{
25+
let Query = Stack.ContentType(contentType).Query();
26+
let entry = Query.toJSON().find();
27+
return entry;
28+
}
3429

35-
<a
36-
href="https://github.com/vercel/next.js/tree/master/examples"
37-
className={styles.card}
38-
>
39-
<h2>Examples &rarr;</h2>
40-
<p>Discover and deploy boilerplate example Next.js projects.</p>
41-
</a>
42-
43-
<a
44-
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
45-
className={styles.card}
46-
>
47-
<h2>Deploy &rarr;</h2>
48-
<p>
49-
Instantly deploy your Next.js site to a public URL with Vercel.
50-
</p>
51-
</a>
52-
</div>
53-
</main>
30+
}
31+
catch(err){
32+
console.log(err);
33+
}
34+
35+
}
5436

55-
<footer className={styles.footer}>
56-
<a
57-
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
58-
target="_blank"
59-
rel="noopener noreferrer"
60-
>
61-
Powered by{' '}
62-
<span className={styles.logo}>
63-
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
64-
</span>
65-
</a>
66-
</footer>
37+
export default function Home(props) {
38+
console.log("PROPS", props)
39+
useEffect(() => {
40+
//initializing live preview when the page renders for the first time.
41+
window.parent.postMessage(
42+
{
43+
from: "live-preview",
44+
type: "init",
45+
data: {
46+
config: {
47+
shouldReload: false,
48+
href: window.location.href,
49+
},
50+
},
51+
},
52+
"*"
53+
);
54+
//listening to post messages for the hash value
55+
window.addEventListener("message", async (e) => {
56+
console.log("Event:",e)
57+
});
58+
}, [])
59+
return (
60+
<div>
61+
<h1>SSR Rendering</h1>
6762
</div>
6863
)
6964
}
65+
66+
export const getServerSideProps = async (context) => {
67+
let entry = await fetchData("ashwini_rte", "blt69a28688604b9027");
68+
return { props:
69+
{...entry}
70+
}
71+
72+
}

0 commit comments

Comments
 (0)