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

@jahia/nextjs-sdk

v0.0.7

Published

The aim of the Jahia Nextjs initiative is to explore and explain the Jahia capabilities to easily create and manage headless web project. Solutions we use are : - [Jahia][jahia-website] : a Cloud / On-premise *DXP* solution to create and contribute - [Ver

Downloads

6

Readme

The Jahia Nextjs initiative : jahia SDK for Nextjs

The aim of the Jahia Nextjs initiative is to explore and explain the Jahia capabilities to easily create and manage headless web project. Solutions we use are :

  • Jahia : a Cloud / On-premise DXP solution to create and contribute
  • Vercel a next-js Cloud platform provider to render the web project

To know more about the Jahia Nextjs initiative read this dedicated page.

Why this sdk ?

Use the sdk in Nextjs headless project:

  • to create and export easily wysiwyg webpage template
  • to get content properties stored in Jahia with one line of code in a React component
  • to manage edit, preview and live urls
  • and much more

Installation

The SDK is available as an npm package. To install the sdk in a project, run one of this command :

# with npm
npm install @jahia/nextjs-sdk

# with yarn
yarn add @jahia/nextjs-sdk

To know more about the full configuration of a project using Jahia and Nextjs read the setup.

Quick start to use the sdk in a Nextjs project

To understand in details the architecture of a Nextjs project using Jahia, have a look at the architecture.

In this section we present source code extracted from the nextjs-industrial project. This project is a fully functional website using Jahia and Nextjs.

Pages

Pages are created and managed in Jahia, and the page url is based on the jahia page path. To render the page in Nextjs you have to create a [[slug.js]] file in the pages folder and copy/past the following code :

/* eslint-disable prefer-destructuring */
import {JahiaPage} from '@jahia/nextjs-sdk';

export default JahiaPage.Component;
export const getStaticPaths = JahiaPage.getStaticPaths;
export const getStaticProps = JahiaPage.getStaticProps;

To render the pages properly you have to :

  • run the registration of components
  • get initial main content properties

This is done in the _app.jsx file:

import React from 'react';
import {JahiaNextApp} from '@jahia/nextjs-sdk';

/* Load css or other if needed */

import {registerComponents} from '../components/registerComponents';

registerComponents();

JahiaNextApp.useRender = () => {
    /* Do extra stuff if needed*/
};

export default JahiaNextApp;

The components folder contains for each content type templates and/or views. For example, for the page content type jnt:page we register three templates (default,open and fixed Structure) using two components PageOpenTemplate and PageFixedStructureTemplate and the registration element :

import {PageOpenTemplate, PageFixedStructureTemplate} from './Page';
export const registerComponents = () => {
    Object.assign(templates, {
        'jnt:page': {
            default: PageOpenTemplate,
            open: PageOpenTemplate,
            'fixed Structure': PageFixedStructureTemplate,
        },
        ...

    });
    ...
}

Note : a special api service is used to select from the Jahia UI the available templates. See this file.

You can now start to use Jahia components or create your own.

SDK Components

|Name|Props|Description| |---|---|---| |Area| name, tagProps, componentProps, path |This component enable the in-context contribution in the webpage. The Area is used by Jahia to create a slot in edit mode where a contributor can create or copy/past a content.| |DefaultImage| path, alt, className, width, height |This component is use to render an image. For the moment (alpha release) it doesn't use the Next Image component.| |EmbeddedPathInHtmlResolver| htmlAsString |This component is to resolve anchor href or image src path embedded in an HTML string written with a RichText editor. This component replace the dangerouslySetInnerHTML props| |JahiaLink| same as Link from Next |This component is a wrapper of the Link component provided by Next. The JahiaLink generate the appropriate link to browse the website in edit or preview mode from Jahia, or in live mode from Vercel. The live link is generated with the Link component provided by Next.| |ContentReference| id, referenceComponent, className |This component is used to render a content used through a reference in Jahia (like a symlink in linux). You have a unique content you can use in several pages with a specific rendering. referenceComponent is useful if you want to force the rendering of the content in reference with a specific component| |ImageReferenceLink| id, referenceComponent, className |This component is used to render the default Jahia content type: Image (from the document manager). Link rendering is not implemented yet (alpha release).| |RichText| id |This component is used to render the default Jahia content type: Rich text.|

GraphQL helper function

The sdk exports the useNode function, which resumes in one line the GraphQL call needed to get content properties.

typescript signature:

const useNode = (uuid: string, properties: string[]=[], children= false)

Example of usage

Let consider a content using this content definition:

[hicnt:featureContentBloc]> jnt:content
  - title (string) internationalized
  - teaser (string) internationalized
  - iconName (string)

To retrieve the value of the properties in your React component, use the useNode() function as follows:

import {useNode} from '@jahia/nextjs-sdk';

function FeatureContentBloc({id}) {
    const {data, error, loading} = useNode(id, ['title', 'teaser', 'iconName']);
    ...
}

The function returns a data object with a properties object which contains the props, and a children array which contains child nodes if children props is set to true.

const {title, teaser, iconName} = data.properties;

The function returns also core properties accessible directly from the data object:

  • uuid: unique id of the content
  • path: path of the content in the content tree
  • name: technical name of the content
  • primaryNodeType.name: type of content
  • mixinTypes[{name}]: name of the mixins associated to the content
  • view: name of the component to call to render the content

Note: you can always write your own GraphQL query and use it with the apollo useQuery function. Don't forget to use the CORE_NODE_FIELDS fragment for apollo cache purpose