npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ueno/prismic-kit

v0.1.4

Published

Helpful tools for building Prismic apps in React

Downloads

7

Readme

prismic-kit

Helpful tools for building Prismic apps in React

Travis npm

Table of Contents

Installation

npm install @ueno/prismic-kit

This library has the following peer dependencies:

{
  "peerDependencies": {
    "express": "5.x",
    "prismic-javascript": "1.x",
    "prop-types": "^15.6.0",
    "react": "^16.4.0"
  }
}

You also need babel-polyfill, because the code has transpiled async functions.

Usage

Configuration

To use the querying function query or Express middleware middleware, you'll need to configure prismic-kit with your Prismic repo name, link resolver, and access token. The access token is optional, but you need it in order to fetch Prismic previews.

import PrismicKit from '@ueno/prismic-kit';
import linkResolver from './utils/prismic-link-resolver';

PrismicKit.config({
  repoName: 'ueno-llc',
  accessToken: process.env.PRISMIC_ACCESS_TOKEN,
  linkResolver,
});

You can also get the config you set by calling PrismicKit.config() with no arguments.

Fetching on the client

Make sure to configure prismic-kit before using this.

The query function is designed to make calls to the Prismic API faster. It generates the API wrapper from Prismic.getAPI() for you, and on the client, the wrapper is cached, which cuts down on network requests.

This is designed for use with react-jobs, but you can also use it standalone.

import React, { Component } from 'react';
import { withJob } from 'react-jobs';
import queryPrismic from '@ueno/prismic-kit/query';

@withJob({
  work: queryPrismic(api => api.getSingle('home_page')),
})
export default class App extends Component {
  render() {
    const page = this.props.jobResult;

    return (
      <h1>{page.data.title}</h1>
    );
  }
}

In the callback, you get access to a Prismic API wrapper, as well as the props of the component.

@withJob({
  work: queryPrismic((api, props) => api.getByUID('article', props.uid)),
})

You can also use the function standalone if need be. Basically, you just call it twice.

import { observable } from 'mobx';
import Prismic from 'prismic';
import queryPrismic from '@ueno/prismic-kit/query';

export default class PrismicStore {
  @observable articles = []

  fetchArticles = queryPrismic(api => api.query([
    Prismic.Predicates.at('document.type', 'article'),
  ]))

  constructor() {
    this.fetchArticles().then(articles => {
      this.articles.replace(articles);
    });
  }
}

Rendering slice zones

Slice zones are a great feature of Prismic for building modular content, and React's component model is a perfect fit for slices. The <SliceRenderer /> component converts a slice zone into a series of components.

To start, create one component for each slice type. This component will receive all the properties of a slice as props:

  • primary (Object): the non-repeatable fields of a slice. Each property will be a field.
  • items (Array of Objects): the repeatable fields of a slice. Each item will be an object with the repeatable fields.
  • slice_type (String): the kind of slice. You usually won't need to reference this.
  • index (Number): the index of the slice within the list.
// slices/text.js
import React from 'react';
import { RichText } from 'prismic-reactjs';

const Text = ({ primary }) => (
  <div>
    {RichText.render(primary.text)}
  </div>
);

Now, use <SliceRenderer /> where you want to render the slice zone, and pass in the components and slices. You can also add the passProps prop, which will add extra props to every slice. Use this when you need every slice to have some extra metadata.

import React, { Component } from 'react';
import SliceRenderer from '@ueno/prismic-kit/slice-renderer';
import TitleSlice from './slices/title';
import Text from './slices/text';
import ImageSlice from './slices/image';

export default ({ page }) => (
  <div>
    <h1>{page.data.title}</h1>
    <div>
      <SliceRenderer
        slices={page.data.body}
        components={{
          title: TitleSlice,
          body: BodySlice,
          image: ImageSlice,
        }}
      />
    </div>
  </div>
);

Enabling previews and webhooks

Make sure to configure prismic-kit before using this.

To enable preview and webhook support, use this Express middleware.

// server.js
import express from 'express';
import prismicMiddleware from '@ueno/prismic-kit/middleware';
import cache from './redis-cache';
import linkResolver from './prismic-link-resolver';

const app = express();

app.use('/api/prismic', prismicMiddleware({
  webhookCallback: () => {
    return cache.clear();
  },
  webhookSecret: process.env.PRISMIC_WEBHOOK_SECRET,
}));

Webhooks

The route POST /webhook is used for triggering webhooks.

If you specify a webhookCallback in the middleware, triggering the webhook will run the given function. The function can be synchronous, or asynchronous/Promise-returning.

If you specify a webhookSecret, the webhook will only trigger if the given secret is sent in the body of the request. You can configure this secret in the "Webhooks" section of your Prismic repo's settings.

This route can return any of these status codes:

  • 200 OK if the webhook callback finished.
  • 500 Internal Server Error if the webhook threw an error or a Promise rejected.
  • 400 Bad Request if a secret was sent in the request, but you didn't configure a webhookSecret.
  • 401 Unauthorized if the secret sent in the request doesn't match webhookSecret, or if no secret was sent.

Previews

The route GET /preview is used for Prismic previews. A Prismic preview cookie is sent with the response, and the 302 Found status is used to redirect the user to the correct page.

Fetching previews on the client

If you use server-side rendering in your React app, ordinarily you can't fetch Prismic previews on the server, because the server can't see the preview cookie.

With this in mind, we render Prismic previews like so:

  • Do the initial render on the server.
  • On the client, check if the Prismic preview cookie was set.
  • If it is, unmount the app and then immediately remount it, forcing all the network requests to happen again, thus loading the preview version.

Like the querying function, this process is build around react-jobs, because re-mounting a component will make all of its network requests happen again.

To help with this, you can wrap your entire app in the <PrismicPreviewLoader /> component. It will re-mount all of its children if a Prismic preview cookie is found.

import React, { Component } from 'react';
import PrismicPreviewLoader from '@ueno/prismic-kit/preview-loader';

export default class App extends Component {
  render() {
    return (
      <PrismicPreviewLoader>
        {/* ... */}
      </PrismicPreviewLoader>
    );
  }
}

Local Development

git clone https://github.com/ueno-llc/prismic-kit
cd prismic-kit
npm install
npm test

License

MIT © ueno.