Skip to content

Instantly share code, notes, and snippets.

@pex
Created September 14, 2018 09:47
Show Gist options
  • Save pex/f525316e128e31dd255a6ef58a4cbb9c to your computer and use it in GitHub Desktop.
Save pex/f525316e128e31dd255a6ef58a4cbb9c to your computer and use it in GitHub Desktop.
Next.js + Prismic Intro
import React from 'react'
import Link from 'next/link'
import { RichText } from 'prismic-reactjs'
import { fetchBlogPosts, linkResolver } from '../lib/prismic.js'
import withPrismic from '../lib/with-prismic.js'
class BlogIndex extends React.Component {
static async getInitialProps (ctx.prismic) {
const posts = await fetchBlogPosts(ctx.prismic)
return { posts }
}
render () {
return (
<ul>
{posts.map(post => (
<li key={post.id}>
<h2>{RichText.asText(post.data.title)}</h2>
<Link href={linkResolver(post.uid)}>
<a>View Article</a>
</Link>
</li>
))}
</ul>
)
}
}
export default withPrismic(BlogIndex)
export const linkResolver = (doc) => {
if (doc.type === 'blog_article') {
return `/article?uid=${doc.uid}`
}
return '/'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment