0.2.6 • Published 3 years ago

next-slicezone v0.2.6

Weekly downloads
544
License
ISC
Repository
github
Last release
3 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

3 years ago

0.2.3-alpha.6

3 years ago

0.2.3-alpha.5

3 years ago

0.2.3-alpha.9

3 years ago

0.2.3-alpha.8

3 years ago

0.2.6-alpha.5

3 years ago

0.2.6-alpha.3

3 years ago

0.2.6-alpha.4

3 years ago

0.2.6-alpha.1

3 years ago

0.2.6-alpha.2

3 years ago

0.2.6-alpha.0

3 years ago

0.2.2-alpha.21

3 years ago

0.2.5-alpha.1

3 years ago

0.2.5-alpha.0

3 years ago

0.2.5-alpha.5

3 years ago

0.2.5-alpha.4

3 years ago

0.2.5-alpha.3

3 years ago

0.2.5-alpha.2

3 years ago

0.2.5-alpha.9

3 years ago

0.2.5-alpha.7

3 years ago

0.2.5-alpha.6

3 years ago

0.2.3-alpha.10

3 years ago

0.2.3-alpha.13

3 years ago

0.2.3-alpha.12

3 years ago

0.2.3-alpha.11

3 years ago

0.2.4-alpha.0

3 years ago

0.2.5-alpha.10

3 years ago

0.2.5-alpha.11

3 years ago

0.2.5-alpha.12

3 years ago

0.2.7-alpha.2

3 years ago

0.2.7-alpha.0

3 years ago

0.2.7-alpha.1

3 years ago

0.2.6

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3-alpha.3

3 years ago

0.2.3-alpha.2

3 years ago

0.2.3-alpha.1

3 years ago

0.2.3-alpha.0

3 years ago

0.2.2-alpha.18

3 years ago

0.2.2-alpha.19

3 years ago

0.2.2-alpha.15

3 years ago

0.2.2-alpha.16

3 years ago

0.2.2-alpha.17

3 years ago

0.2.3-alpha.4

3 years ago

0.2.2-alpha.20

3 years ago

0.1.4-alpha.39

3 years ago

0.1.4-alpha.38

3 years ago

0.1.4-alpha.37

3 years ago

0.2.2-alpha.14

3 years ago

0.2.2-alpha.10

3 years ago

0.1.4-alpha.32

3 years ago

0.2.2-alpha.11

3 years ago

0.1.4-alpha.31

3 years ago

0.2.2-alpha.12

3 years ago

0.1.4-alpha.30

3 years ago

0.2.2-alpha.13

3 years ago

0.1.4-alpha.36

3 years ago

0.1.4-alpha.35

3 years ago

0.1.4-alpha.34

3 years ago

0.1.4-alpha.33

3 years ago

0.1.4-alpha.50

3 years ago

0.1.4-alpha.49

3 years ago

0.1.4-alpha.48

3 years ago

0.1.4-alpha.43

3 years ago

0.1.4-alpha.42

3 years ago

0.1.4-alpha.40

3 years ago

0.1.4-alpha.47

3 years ago

0.1.4-alpha.46

3 years ago

0.1.4-alpha.45

3 years ago

0.1.4-alpha.44

3 years ago

0.2.2-alpha.5

3 years ago

0.2.2-alpha.6

3 years ago

0.2.2-alpha.7

3 years ago

0.2.2-alpha.8

3 years ago

0.2.2-alpha.9

3 years ago

0.1.4-alpha.18

3 years ago

0.1.4-alpha.17

3 years ago

0.1.4-alpha.16

3 years ago

0.1.4-alpha.15

3 years ago

0.1.4-alpha.19

3 years ago

0.1.4-alpha.10

3 years ago

0.2.2-alpha.0

3 years ago

0.2.2-alpha.1

3 years ago

0.1.4-alpha.14

3 years ago

0.2.2-alpha.2

3 years ago

0.1.4-alpha.13

3 years ago

0.2.2-alpha.3

3 years ago

0.1.4-alpha.12

3 years ago

0.2.2-alpha.4

3 years ago

0.1.4-alpha.11

3 years ago

0.1.4-alpha.29

3 years ago

0.1.4-alpha.28

3 years ago

0.1.4-alpha.26

3 years ago

0.1.4-alpha.21

3 years ago

0.1.4-alpha.20

3 years ago

0.1.4-alpha.25

3 years ago

0.1.4-alpha.24

3 years ago

0.1.4-alpha.23

3 years ago

0.1.4-alpha.22

3 years ago

0.2.1-alpha.11

3 years ago

0.2.1-alpha.12

3 years ago

0.2.1-alpha.10

3 years ago

0.2.1-alpha.6

3 years ago

0.2.1-alpha.7

3 years ago

0.2.1-alpha.8

3 years ago

0.2.1-alpha.9

3 years ago

0.2.1-alpha.19

3 years ago

0.2.1-alpha.17

3 years ago

0.2.1-alpha.18

3 years ago

0.2.1-alpha.15

3 years ago

0.2.1-alpha.16

3 years ago

0.2.1-alpha.13

3 years ago

0.2.1-alpha.14

3 years ago

0.2.1-alpha.2

3 years ago

0.2.1-alpha.3

3 years ago

0.2.1-alpha.4

3 years ago

0.2.1-alpha.5

3 years ago

0.2.1-alpha.0

3 years ago

0.2.1-alpha.1

3 years ago

0.2.1-alpha.22

3 years ago

0.2.1-alpha.23

3 years ago

0.2.1-alpha.20

3 years ago

0.2.1-alpha.21

3 years ago

0.2.1-alpha.24

3 years ago

0.1.4-alpha.51

3 years ago

0.1.4-alpha.3

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.4-alpha.8

3 years ago

0.1.4-alpha.9

3 years ago

0.1.4-alpha.4

3 years ago

0.1.4-alpha.5

3 years ago

0.1.3-beta.2

3 years ago

0.1.3-beta.1

3 years ago

0.1.3-beta.3

3 years ago

0.1.4-alpha.1

3 years ago

0.1.4-alpha.2

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

4 years ago

0.1.0-alpha.0

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.15-alpha.0

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.12-alpha.0

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.3

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago