Displaying content
import type { NextPage, GetStaticProps, GetStaticPaths } from "next";
import {RenderableContent, Metadata, RawContent} from "@shopstory/core";
import { createClient, Entry } from "contentful";
import { ShopstoryClient } from "@shopstory/core";
import { Shopstory, ShopstoryMetadataProvider } from "@shopstory/react";
import { shopstoryConfig } from "../../src/shopstory/config";
import { DemoShopstoryProvider } from "../../src/shopstory/provider";
type ShopstoryBlockPageProps = {
renderableContent: RenderableContent;
meta: Metadata;
};
const ShopstoryBlockPage: NextPage<ShopstoryBlockPageProps> = (props) => {
return (
<DemoShopstoryProvider>
<ShopstoryMetadataProvider meta={props.meta}>
<Shopstory content={props.renderableContent} />
</ShopstoryMetadataProvider>
</DemoShopstoryProvider>
);
};
export const getStaticPaths: GetStaticPaths = () => {
return { paths: [], fallback: "blocking" };
};
export const getStaticProps: GetStaticProps<
ShopstoryBlockPageProps,
{ entryId: string }
> = async (context) => {
let { params, preview, locale = "en-US" } = context;
if (!params) {
return { notFound: true };
}
const rawContent = await fetchShopstoryContentJSONFromCMS(params.entryId, locale, !!preview);
const shopstoryClient = new ShopstoryClient(shopstoryConfig, {
locale,
contentful: { preview }, // Contentful-specific piece of code
});
const renderableContent = shopstoryClient.add(rawContent);
const meta = await shopstoryClient.build();
return {
props: { renderableContent, meta },
revalidate: 10,
};
};
async function fetchShopstoryContentJSONFromCMS(entryId: string, locale: string, preview: boolean) : RawContent {
/**
CMS specific operations that fetches the Shopstory JSON field data.
**/
}
export default ShopstoryBlockPage;
Fetching data from the CMS
Building content - ShopstoryClient
ShopstoryClientCreate ShopstoryClient instance
ShopstoryClient instanceAdd the content
Build
Rendering the content
Server-side rendering
Summary
Last updated