Skip to content

Instantly share code, notes, and snippets.

@hypervillain
Last active June 25, 2020 09:19
Show Gist options
  • Save hypervillain/4249f98e1cbd45b62fd7a23d0b8f26ee to your computer and use it in GitHub Desktop.
Save hypervillain/4249f98e1cbd45b62fd7a23d0b8f26ee to your computer and use it in GitHub Desktop.
Setup Next.js project with SliceMachine + essential slices
import { Client } from '../prismic'
import SliceZone from 'next-slicezone'
import { useGetStaticProps, useGetStaticPaths } from 'next-slicezone/hooks'
import resolver from '../sm-resolver.js'
const Page = (props) => <SliceZone {...props} resolver={resolver} />
export const getStaticProps = useGetStaticProps({
client: Client(),
uid: ({ params }) => params.uid
})
export const getStaticPaths = useGetStaticPaths({
client: Client(),
type: 'page',
fallback: process.env.NODE_ENV === 'development',
formatPath: ({ uid }) => ({ params: { uid }})
})
export default Page
// pages/_app.js
import React from 'react'
import NextApp from 'next/app'
import { ThemeProvider, BaseStyles } from 'theme-ui'
export default class App extends NextApp {
render() {
const { Component, pageProps } = this.props
return (
<ThemeProvider theme={theme}>
<BaseStyles>
<Component {...pageProps} />
</BaseStyles>
</ThemeProvider>
)
}
}
// pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'
import { InitializeColorMode } from 'theme-ui'
import { createResolver } from 'next-slicezone/resolver'
export default class extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
await createResolver()
return { ...initialProps }
}
render() {
return (
<Html>
<Head />
<body>
<InitializeColorMode />
<Main />
<NextScript />
</body>
</Html>
)
}
}
const withTM = require('next-transpile-modules')(['next-slicezone', 'essential-slices']);
module.exports = withTM();
/** Example file */
import Prismic from 'prismic-javascript'
import Link from 'next/link'
import smConfig from './sm.json'
export const apiEndpoint = smConfig.apiEndpoint
// -- Access Token if the repository is not public
// Generate a token in your dashboard and configure it here if your repository is private
export const accessToken = ''
// -- Link resolution rules
// Manages the url links to internal Prismic documents
export const linkResolver = (doc) => {
if (doc.type === 'post') {
return `/blog/${doc.uid}`
}
return '/'
}
// Additional helper function for Next/Link components
export const hrefResolver = (doc) => {
if (doc.type === 'post') {
return '/blog/[uid]'
}
return '/'
}
export const customLink = (type, element, content, children, index) => (
<Link
key={index}
href={hrefResolver(element.data)}
as={linkResolver(element.data)}
>
<a>{content}</a>
</Link>
)
export const routes = [{
type: 'page',
path: '/:uid'
}]
export const Client = (req = null, options = {}) => (
Prismic.client(apiEndpoint, Object.assign({ routes: Router.routes }, options))
)
import React from 'react'
export default ({ i }) => {
return () => i ? <div /> : <p>Your SliceZone is loading... You might need to reload this page.</p>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment