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

next-drupal-preview

v0.2.3

Published

Preview and front-end editing for decoupled Drupal + NextJS applications

Downloads

47

Readme

next-drupal-preview

This package is used for editing content in the frontend.

Requirements

  • NextJS
  • A Drupal back-end with the module next_preview. Please refer to the documentation for instructions on how to install and configure.

Installation

First you need to install the package, you do this by running the following command:

npm install next-drupal-preview

Endpoints

You need to set up 2 endpoints in your NextJS project, one for enabling and one for disabling Next preview mode. Example:

pages/api/preview/index.ts

import { NextApiRequest, NextApiResponse } from 'next'

export default function Preview (req: NextApiRequest, res: NextApiResponse): any {
  if (typeof req.query.path !== 'string') {
      return res.redirect('/')
  }
    
  // The token is the preview key that is generated by Drupal,
  // and is used to authenticate.
  res.setPreviewData({ token: req.query.token ?? '' }, { maxAge: 60 * 60 * 24 * 30 })
  return res.redirect(req.query.path)
}

pages/api/preview/clear.ts

import { NextApiRequest, NextApiResponse } from 'next'

export default function ClearPreview (req: NextApiRequest, res: NextApiResponse): any {
  res.clearPreviewData()
  return res.redirect('/')
}

Usage

Preview banner

To display a preview banner, you need to import the PreviewBanner component and place it in your page. Example:

import { DrupalPreviewBanner } from 'next-drupal-preview'

export default function Page () {
  return (
    <>
      <h1>Page</h1>
      {/* ... page content */}
      <DrupalPreviewBanner cmsUrl="https://cms.example.com" /* The Drupal CMS url */
                           id="123" /* The ID of the node entity in Drupal */
                           clearPreviewUrl="/api/preview/clear" /* The URL to the clear preview endpoint */
      />
    </>
  )
}

Customizing the banner

You can customize certain css properties of the banner using these props:

  • textColor
  • backgroundColor
  • buttonColor
  • buttonTextColor
  • buttonSecondaryColor
  • buttonSecondaryTextColor
  • borderColor
  • outline

Frontend editing

To enable editing content through the frontend using an isolated form, you need to wrap the content component with a DrupalEditable component. Example:

import { DrupalEditable } from 'next-drupal-preview'

export default function Paragraph ({ paragraph }) {
  return (
    <DrupalEditable id={paragraph.id.toString()} /* The ID of the entity in Drupal */
                    type='paragraph' /* The type of entity */
                    cmsUrl={getCmsUrl()} /* The Drupal CMS url */
                    token={token} /* The preview token from Next preview data */
    >
      <ExampleParagraph paragraph={paragraph} />
    </DrupalEditable>
  )
}