sourcebit-target-next v0.8.3
sourcebit-target-next
Overview
This plugin leverages Next.js SSG capabilities to provide content from any Sourcebit data source, such as a headless CMS, into React page components as properties using getStaticProps and getStaticPaths methods
Installation
Install Sourcebit and the plugin:
npm install sourcebit sourcebit-target-nextImport
sourcebitandsourcebit.jsconfiguration file into yournext.config.jsfile (the next section will explain how to configuresourcebit.jsfile):const sourcebit = require('sourcebit'); const sourcebitConfig = require('./sourcebit.js'); sourcebit.fetch(sourcebitConfig);To provide data fetched by Sourcebit to pages, update
getStaticPathsandgetStaticPropsmethods of your page components:If a page does not use dynamic routes, then it should only have the
getStaticPropsmethod. To pass the data fetched by Sourcebit to a page, update itsgetStaticPropsby callingsourcebitDataClient.getStaticPropsForPageAtPath(path)and returning the props returned from it. Thepathparameter should be the URL path of the rendered page.For example, if the page component is
index.js, then the path would be/, and if the page component isabout.js, then the path would be/about.For instance, given a page component at
pages/index.js, the code would look like this:import { sourcebitDataClient } from 'sourcebit-target-next'; export async function getStaticProps() { const props = await sourcebitDataClient.getStaticPropsForPageAtPath('/'); return { props }; }If a page does use dynamic routes then it should have both
getStaticPropsandgetStaticPathsmethods.Similar to the previous example, use
getStaticPropsForPageAtPath(path)to get the static props. But in this case, thepathparameter cannot be constant. Instead it should be computed by applyingparamsprovided by thegetStaticPropsto the pattern of the dynamic route.For example, given a page component at
pages/[...slug].js, the code would look like this:import { sourcebitDataClient } from 'sourcebit-target-next'; export async function getStaticProps({ params }) { const pagePath = '/' + params.slug.join('/'); const props = await sourcebitDataClient.getStaticPropsForPageAtPath(pagePath); return { props }; }Use
sourcebitDataClient.getStaticPaths()to get the static paths of pages and return them fromgetStaticPaths. Note thatsourcebitDataClient.getStaticPaths()returns paths for all pages, therefore you will need to filter them to return only those that are supported by the dynamic route of the given page.For example, if you have two pages with dynamic routes, each will have to filter its own static paths:
pages/post/[pid].jsimport { sourcebitDataClient } from 'sourcebit-target-next'; export async function getStaticProps() { ... } export async function getStaticPaths() { const paths = await sourcebitDataClient.getStaticPaths(); return { paths: paths.filter(path => path.startsWith('/post/')), fallback: false }; }pages/[...slug].jsimport { sourcebitDataClient } from 'sourcebit-target-next'; export async function getStaticProps() { ... } export async function getStaticPaths() { const paths = await sourcebitDataClient.getStaticPaths(); return { // do not include paths for /post/[pid].js and for /index.js paths: paths.filter(path => path !== '/' && !path.startsWith('/post/')), fallback: false }; }
To update the browser with live content changes while running
next dev, wrap your pages with following higher order component (HOC):import { withRemoteDataUpdates } from 'sourcebit-target-next/with-remote-data-updates'; class Page extends React.Component { render() { // ... } } export default withRemoteDataUpdates(Page);
Sourcebit Configuration
The plugin is configured with two options - pages and commonProps:
sourcebit.js:
module.exports = {
plugins: [
...otherPlugins,
{
module: require('sourcebit-target-next'),
options: {
// Define which source objects represent pages
// and under which paths they should be available.
pages: [
{ path: '/{slug}', predicate: _.matchesProperty('__metadata.modelName', 'page') },
{ path: '/{slug}', predicate: _.matchesProperty('__metadata.modelName', 'special_page') },
{ path: '/blog/{slug}', predicate: _.matchesProperty('__metadata.modelName', 'post') }
],
// Define common props that will be provided to all pages
commonProps: {
config: { single: true, predicate: _.matchesProperty('__metadata.modelName', 'site_config') },
posts: { predicate: _.matchesProperty('__metadata.modelName', 'post') }
}
}
}
]
};pages(array) An array of objects mapping entries fetched by one of the source plugins to props that will be provided to a specific page identified by its path viagetStaticProps.Every object should define two fields
pathandpredicate. Thepredicateis used to filter entries fetched by source plugins. While thepathis used to generate the URL path of the page. Thepathparameter can use tokens in form of{token_name}where eachtoken_nameis a field of an entry from the source plugin.When calling
sourcebitDataClient.getStaticPropsForPageAtPath(pagePath)from withingetStaticProps, the returned value will be an object with two properties:pageholding the actual page entry; andpathmatching thepagePathpassed togetStaticPropsForPageAtPath.For example:
// lodash's matchesProperty(path, value) creates a function that compares // between the value at "path" of a given object to the provided "value" [ { path: '/{slug}', predicate: _.matchesProperty('__metadata.modelName', 'page') }, { path: '/{slug}', predicate: _.matchesProperty('__metadata.modelName', 'custom_page') }, { path: '/blog/{slug}', predicate: _.matchesProperty('__metadata.modelName', 'post') } ];Assuming a Headless CMS returned a page of type
custom_pagehavingslug: "about", callingsourcebitDataClient.getStaticPropsForPageAtPath('/about')from withingetStaticPropswill return a following object:{ path: '/about', page: { slug: "about", ...otherEntryFields } }commonProps(object) An object mapping entries fetched by one of the source plugins to props that will be provided to all page components viagetStaticProps.The keys of the object specify the propery names that will be provided to page components, and their values specify what data should go into these properties. Every value should be an object with a
predicatefield. Thepredicateis used to filter entries fetched by source plugins. Additionally, a boolean fieldsinglecan be used to specify a property that should reference a single entry rather list of entries. Ifsingle: trueis applied to multiple entries, only the first one will be selected.When calling
sourcebitDataClient.getStaticPropsForPageAtPath(pagePath)from withingetStaticProps, the returned value will be an object with two predefined propertiespageandpathas described above, plus all the properties defined by this map.For example:
{ config: { single: true, predicate: _.matchesProperty('_type', 'site_config') }, posts: { predicate: _.matchesProperty('_type', 'post') } }When calling
sourcebitDataClient.getStaticPropsForPageAtPath(pagePath), in addition topageandpathproperties, the returned object will haveconfigandposts:{ path: '/about', page: { ... }, config: { ... }, posts: [ ... ] }liveUpdate(boolean) A flag indicating if page should reload its data when remote data changed. Defaults to true whenNODE_ENVis set todevelopment.
You can check out an example project
that uses sourcebit-source-sanity and sourcebit-target-next plugins to fetch
the data from Sanity.io and feed it into
Next.js page components.
Tips
Add following to your .gitignore:
.sourcebit-cache.json
.sourcebit-nextjs-cache.jsonTo simplify the dynamic routing architecture and to allow greater flexibility when creating pages in Headless CMS, we advise using following pattern:
pages/[...slug].js
import React from 'react';
import { sourcebitDataClient } from 'sourcebit-target-next';
import withRemoteDataUpdates from 'sourcebit-target-next/withRemoteDataUpdates';
import pageLayouts from '../layouts';
class Page extends React.Component {
render() {
// every page can have different layout, pick the layout based
// on the modelName of the page
const PageLayout = pageLayouts[_.get(this.props, 'page.__metadata.modelName')];
return <PageLayout {...this.props} />;
}
}
export async function getStaticPaths() {
const paths = await sourcebitDataClient.getStaticPaths();
return { paths: paths.filter((path) => path !== '/'), fallback: false };
}
export async function getStaticProps({ params }) {
const pagePath = '/' + params.slug.join('/');
const props = await sourcebitDataClient.getStaticPropsForPageAtPath(pagePath);
return { props };
}
export default withRemoteDataUpdates(Page);pages/index.js
import Page from './[...slug]';
import { sourcebitDataClient } from 'sourcebit-target-next';
export async function getStaticProps({ params }) {
console.log('Page [index] getStaticProps, params: ', params);
const props = await sourcebitDataClient.getStaticPropsForPageAtPath('/');
return { props };
}
export default Page;Note: we are using additional index.js page because [...slug].js page does
not catch root page /;
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago