Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@stnew/prismic

Package Overview
Dependencies
Maintainers
4
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stnew/prismic

Prismic components and utilities for React

latest
npmnpm
Version
1.2.6
Version published
Maintainers
4
Created
Source

@stnew/prismic

This package exports:

  • urlResolver - A helper function to create link resolvers.

urlResolver

The urlResolver function will help Prismic and (optionally) Next.js resolve the correct prismic documents when you wire up links.

Inside each function is a hash map. The key is the API ID of your Prismic "Single" or "Repeatable" type.

Each route contains up to three keys:

  • href: The URL of the page that will resolve to when passed to our Link component
  • page: (optional): The Next.js page to resolve to. Only neccessary if you're using hrefResolver or PrismicLink
  • root: (optional): The uid of the index page from Prismic

"Single" types, will resolve directly to href. "Repeatable" types resolve to a the href + the uid field, and you can use the root field in conjuction with that to filter out the root uid, so that your URLs are clean.

resolvers

The urlResolver function will return two more functions.

  • linkResolver will resolve the url as you would see it in the address bar, such as /about-us or /blog/hello-world.

  • hrefResolver is for only for next/link components to resolve the correct route, such as /[page] or /blog/[...post]. If you're not using Next.js don't worry about it.

Example

resolvers.js

import { urlResolver } from '@sntew/prismic'

const routeMap = {
  home_page: {
    href: '/',
    page: '/',
  },
  about_page: {
    href: '/about',
    page: '/about',
  }
  privacy_page: {
    href: '/privacy',
    page: '/privacy/[[...uid]]',
    root: 'privacy-policy'
  }
}

export const { linkResolver, hrefResolver}  = urlResolver(routeMap)

NavLink.js

import NextLink from 'next/link'
import { linkResolver, hrefResolver } from './resolvers.js'

function NavLink({ href, label }) {
  return (
    <NextLink as={linkResolver(href)} href={hrefResolver(href)}>
      <a>{label}</a>
    </NextLink>
  )
}

Usage with React

The @stnew/prismic-react package has a Context/Provider pattern, as well as a hook for accessing the linkResolver in any component.

FAQs

Package last updated on 17 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts