0.2.6 • Published 2 years ago

next-slicezone v0.2.6

Weekly downloads
544
License
ISC
Repository
github
Last release
2 years ago

Next SliceZone (wip)

A component that matches front-end components with Prismic slices. Pretty much a work in progress ✌️

RFC: https://github.com/prismicio/slice-machine/issues/7

Status

  • fetch content from getStaticProps
  • fetch dynamic paths from Prismic endpoint Handle dynamic imports
  • handle previews
  • Create registry
  • pass custom resolver

Usage

To display the right content, the SliceZone takes as parameters props passed at build time by useGetStaticProps. Notably:

  • slices, the array data components fetched from Prismic
  • theme, an arbitrary object passed as prop to all slices
  • resolver, a function that resolves calls to components from the SliceZone

The resolver function can be generated for you and should be everytime you make a change to your slices structure (rename, add, delete a slice, add a library...). To do this, create a pages/_document file and add the createResolver method to its getInitialProps method:

Example _document file

import Document, { Html, Head, Main, NextScript } from 'next/document'
import { createResolver } from 'next-slicezone/resolver'

export default class extends Document {
  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx)
    /* In development, generate an sm-resolver.js file
    that will map slices to components */
    if (process.env.NODE_ENV === 'development') {
      await createResolver()
    }
    return { ...initialProps }
  }

  render() {
    return (
      <Html>
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

⚠️ If you don't already have a resolver file and import it in your page components, you might encounter an error that should disappear on complete reload of the page.

Hooks

The SliceZone exports 2 hooks to help Next.js statically export SliceMachine pages.

useGetStaticProps

useGetStaticProps can be used in every page using the SliceZone. It's responsible for:

  • fetching content from Prismic
  • returning a pre-written Next getStaticProps

example

⚠️ Signature is subject to change

import { useGetStaticProps, useGetStaticPaths } from 'next-slicezone/hooks'
import resolver from '../sm-resolver'

const Page = ({ uid, slices }) => (
    <SliceZone resolver={resolver} slices={slices} />
)

export const getStaticProps = useGetStaticProps({
  client, // pass Prismic client here
  type: 'page', // query document of type "page"
  uid: ({ params }) => params.uid // pass a function to `uid` to resolve dynamic content
})

export default Page

Properties

useGetStaticProps takes a params object as argument.

ParamTypeRequiredDefault valueDescriptionExample value
uidstring | functionfalsenullIf queryType has value repeatable, pass document uid or function({params}) => /pages/${params.uid}
langstringfalsenull (master lang)Lang attribute, disabled if params is passed'fr-fr'
paramsobjectfalsenullObject passed to client apiOptions. Disables lang{ lang: 'fr-fr' }
clientfunctiontruenullATM, you have to pass a Prismic client herePrismic.client(apiEndpoint)
bodystringfalsebodyKey of slices array in API response (doc.data[body])'nobody'
typestringfalsepageCustom type to be queried'another_cts'
queryTypestringfalserepeatOne of 'repeat' or 'single', to switch between getByUID and getSingle calls'single'

useGetStaticPaths

useGetStaticPaths should be used in each dynamic page that relies on the SliceZone. It's responsible for:

  • fetching documents by type on Prismic

example

⚠️ Signature is subject to change

import { useGetStaticProps, useGetStaticPaths } from 'next-slicezone/hooks'

const Page = ({ uid, registry, slices }) => (
    <SliceZone resolver={resolver} registry={registry} slices={slices} />
)

// fetch all docs of type `MyPage` and pass params accordingly
export const getStaticPaths = useGetStaticPaths({
  client: Client(),
  fallback: false,
  type: 'MyPage',
  formatPath: ({ uid }) => ({ params: { uid }})
})

export default Page

Properties

useGetStaticPaths takes a params object as argument. Refer to Next docs to understand what's expected from formatPath.

ParamTypeRequiredDefault ValueDescriptionExample
type---Same as useGetStaticProps-
params---Same as useGetStaticProps-
lang---Same as useGetStaticProps-
client---Same as useGetStaticProps-
formatPathfunctiontrue(doc) => '/'Function to format Next path argument({uid}) =>({ params:{ uid }})

Installation guide

This guide assumes you have a running 9.3+ Next.js project, configured to use Prismic.

1/ Create an sm.json file at the root of your Next app

{ "libraries": ["~/slices"] }

👆 This will help the SliceZone locate your slices.

2/ Then install the SliceZone

yarn add next-slicezone

3/ create a [uid].js page

import SliceZone from 'next-slicezone'
import { useGetStaticProps, useGetStaticPaths } from 'next-slicezone/hooks'

// you may want to do this somewhere else
const client = Prismic.client(apiEndpoint)

import resolver from '../sm-resolver'

const Page = ({ uid, slices }) =>  (
    <SliceZone
        resolver={resolver}
        slices={slices}
    />
)

export const getStaticProps = useGetStaticProps({
  client,
  type: 'page',
  uid: ({ params }) => params.uid
})

export const getStaticPaths = useGetStaticPaths({
  client,
  type: 'page',
  fallback: false,
  formatPath: ({ uid }) => ({ params: { uid }})
})

export default Page

4/ Add a slice my_slice to your "page" custom type

In your custom types builder, add a slice with key my_slice and save it.

5/ Create a page with uid my-page on Prismic

Create some content using your my_slice slice.

6/ create a MySlice component in /slices

Your pages are now synced with the writing room ✌️

0.2.3-alpha.7

2 years ago

0.2.3-alpha.6

2 years ago

0.2.3-alpha.5

2 years ago

0.2.3-alpha.9

2 years ago

0.2.3-alpha.8

2 years ago

0.2.6-alpha.5

2 years ago

0.2.6-alpha.3

2 years ago

0.2.6-alpha.4

2 years ago

0.2.6-alpha.1

2 years ago

0.2.6-alpha.2

2 years ago

0.2.6-alpha.0

2 years ago

0.2.2-alpha.21

2 years ago

0.2.5-alpha.1

2 years ago

0.2.5-alpha.0

2 years ago

0.2.5-alpha.5

2 years ago

0.2.5-alpha.4

2 years ago

0.2.5-alpha.3

2 years ago

0.2.5-alpha.2

2 years ago

0.2.5-alpha.9

2 years ago

0.2.5-alpha.7

2 years ago

0.2.5-alpha.6

2 years ago

0.2.3-alpha.10

2 years ago

0.2.3-alpha.13

2 years ago

0.2.3-alpha.12

2 years ago

0.2.3-alpha.11

2 years ago

0.2.4-alpha.0

2 years ago

0.2.5-alpha.10

2 years ago

0.2.5-alpha.11

2 years ago

0.2.5-alpha.12

2 years ago

0.2.7-alpha.2

2 years ago

0.2.7-alpha.0

2 years ago

0.2.7-alpha.1

2 years ago

0.2.6

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3-alpha.3

2 years ago

0.2.3-alpha.2

2 years ago

0.2.3-alpha.1

2 years ago

0.2.3-alpha.0

2 years ago

0.2.2-alpha.18

2 years ago

0.2.2-alpha.19

2 years ago

0.2.2-alpha.15

2 years ago

0.2.2-alpha.16

2 years ago

0.2.2-alpha.17

2 years ago

0.2.3-alpha.4

2 years ago

0.2.2-alpha.20

2 years ago

0.1.4-alpha.39

2 years ago

0.1.4-alpha.38

2 years ago

0.1.4-alpha.37

2 years ago

0.2.2-alpha.14

2 years ago

0.2.2-alpha.10

2 years ago

0.1.4-alpha.32

2 years ago

0.2.2-alpha.11

2 years ago

0.1.4-alpha.31

2 years ago

0.2.2-alpha.12

2 years ago

0.1.4-alpha.30

2 years ago

0.2.2-alpha.13

2 years ago

0.1.4-alpha.36

2 years ago

0.1.4-alpha.35

2 years ago

0.1.4-alpha.34

2 years ago

0.1.4-alpha.33

2 years ago

0.1.4-alpha.50

2 years ago

0.1.4-alpha.49

2 years ago

0.1.4-alpha.48

2 years ago

0.1.4-alpha.43

2 years ago

0.1.4-alpha.42

2 years ago

0.1.4-alpha.40

2 years ago

0.1.4-alpha.47

2 years ago

0.1.4-alpha.46

2 years ago

0.1.4-alpha.45

2 years ago

0.1.4-alpha.44

2 years ago

0.2.2-alpha.5

2 years ago

0.2.2-alpha.6

2 years ago

0.2.2-alpha.7

2 years ago

0.2.2-alpha.8

2 years ago

0.2.2-alpha.9

2 years ago

0.1.4-alpha.18

2 years ago

0.1.4-alpha.17

2 years ago

0.1.4-alpha.16

2 years ago

0.1.4-alpha.15

2 years ago

0.1.4-alpha.19

2 years ago

0.1.4-alpha.10

2 years ago

0.2.2-alpha.0

2 years ago

0.2.2-alpha.1

2 years ago

0.1.4-alpha.14

2 years ago

0.2.2-alpha.2

2 years ago

0.1.4-alpha.13

2 years ago

0.2.2-alpha.3

2 years ago

0.1.4-alpha.12

2 years ago

0.2.2-alpha.4

2 years ago

0.1.4-alpha.11

2 years ago

0.1.4-alpha.29

2 years ago

0.1.4-alpha.28

2 years ago

0.1.4-alpha.26

2 years ago

0.1.4-alpha.21

2 years ago

0.1.4-alpha.20

2 years ago

0.1.4-alpha.25

2 years ago

0.1.4-alpha.24

2 years ago

0.1.4-alpha.23

2 years ago

0.1.4-alpha.22

2 years ago

0.2.1-alpha.11

2 years ago

0.2.1-alpha.12

2 years ago

0.2.1-alpha.10

2 years ago

0.2.1-alpha.6

2 years ago

0.2.1-alpha.7

2 years ago

0.2.1-alpha.8

2 years ago

0.2.1-alpha.9

2 years ago

0.2.1-alpha.19

2 years ago

0.2.1-alpha.17

2 years ago

0.2.1-alpha.18

2 years ago

0.2.1-alpha.15

2 years ago

0.2.1-alpha.16

2 years ago

0.2.1-alpha.13

2 years ago

0.2.1-alpha.14

2 years ago

0.2.1-alpha.2

2 years ago

0.2.1-alpha.3

2 years ago

0.2.1-alpha.4

2 years ago

0.2.1-alpha.5

2 years ago

0.2.1-alpha.0

2 years ago

0.2.1-alpha.1

2 years ago

0.2.1-alpha.22

2 years ago

0.2.1-alpha.23

2 years ago

0.2.1-alpha.20

2 years ago

0.2.1-alpha.21

2 years ago

0.2.1-alpha.24

2 years ago

0.1.4-alpha.51

2 years ago

0.1.4-alpha.3

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.4-alpha.8

2 years ago

0.1.4-alpha.9

2 years ago

0.1.4-alpha.4

2 years ago

0.1.4-alpha.5

2 years ago

0.1.3-beta.2

2 years ago

0.1.3-beta.1

2 years ago

0.1.3-beta.3

2 years ago

0.1.4-alpha.1

2 years ago

0.1.4-alpha.2

2 years ago

0.1.3

2 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.1.0-alpha.0

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.15-alpha.0

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.12-alpha.0

3 years ago

0.0.11

3 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.3

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago