Contentful
Contentful is a headless content management system that allows rapidly build the dynamic experiences your customers crave. Together with Alokai, you can create a powerful and flexible solution for your business. This guide presents how to add the Contentful module to your Storefront.
Read more
Check the Content Management System Overview to learn more about the features of the Contentful module.
Bootstrapping Contentful with Alokai Storefront
To use the Contentful integration in your Storefront, perform the following steps:
1
Install the module
From the root of your Storefront project, run the following command:
npx @vsf-enterprise/storefront-cli add-module cms-contentful
Follow the instructions in the command line to complete the installation. The command will:
- copy Alokai Contentful schemas to the Middleware directory,
- install the Alokai Contentful API Client
@vsf-enterprise/contentful-api
in the Middleware, - install the Alokai Contentful SDK module
@vsf-enterprise/contentful-sdk
in the Storefront, - setup example environment variables.
You can find the changelogs for the installed packages here:
2
Update variables (optional)
By default, the module installation will connect your Storefront to our demo Contentful space. To connect it to a different space, update both .env.example
and .env
files in the Middleware directory.
- CNTF_TOKEN=<alokai-contentful-token>
+ CNTF_TOKEN=<your-contentful-token>
- CNTF_SPACE=<alokai-contentful-space>
+ CNTF_SPACE=<your-contentful-space>
- CNTF_ENVIRONMENT=<alokai-contentful-environment>
+ CNTF_ENVIRONMENT=<your-contentful-content-environment>
Make sure you have bootstrapped your Contentful space before connecting it to your Storefront.
Bootstrapping Contentful
Before you start using our Contentful integration with your Contentful space, you need to import Alokai schemas into your space. They are aligned with our out-of-the-box frontend components.
Importing integration schemas
If you have installed the Contentful module in your Storefront, you will find the default schemas provided by Alokai in the apps/storefront-middleware/sf-modules/cms-contentful/schemas directory. To import them into your own Contentful space, use the CLI tool provided by Contentful:
Locale compatibility requirement
Contentful requires matching locales between source and destination spaces. Our integration schemas use "en" as the default locale. Before importing, ensure your destination space also uses "en" as its default locale to avoid compatibility issues. Or, if you wish to use another default local, you may search and replace "en"
in the schemas.json with your default locale.
1
Navigate to the schemas directory
cd apps/storefront-middleware/sf-modules/cms-contentful/schemas
2
Execute the import
command
contentful space import --space-id <your-space-id> --content-file ./schemas.json
Once done, the default Content Types should appear under the Content model tab in your Contentful space.
Enabling Live Preview
Contentful has a reliable Live Preview feature. It allows editors to display a preview of their website within Contentful and verify their content changes as they make them. Alokai gives you a straightforward way to enable the feature in your frontend application.
Enabling Live Preview starts with setting up content preview URLs in Contentful. The default Alokai setup only supports out-of-the-box content previews for the Page content type which acts as a container for other components.
By setting content preview URLs, we are basically telling Contentful that - whenever we start editing some Page or Layout entry and open up the Live Preview window - it should display our app running on http://localhost:3000
.
Notice how we have assigned the {entry.fields.url}
token to the ?preview
query param. We've chosen the url
field because that's the one our Storefront uses to fetch pages.
No additional actions are required in your Alokai Storefront. The only thing you need to do is to make sure your frontend app is running on the same port as the one you've set up in Contentful. By default, it's 3000
for Next.js and 3333
for Nuxt.
Enabling Live Preview for the content enhanced pages
To enable preview on any enhanced page within your Contentful panel please follow these steps.
1
2
3
Add preview platform details
Within the Create preview platform modal please fil-in data for platform of your choice:
- add name of the preview platform,
- select the preview model (enhanced Category or Product Details Page),
- provide preview URL (localhost with dedicate port).
On the screen below you can see an example of the Next.js platform.
4
Use Live Preview
Now you can check if your Live Preview works as expected.
Navigate to the main Content tab. Select one of your enhanced pages, for example Category one. While viewing the content editing panel hit the Open Live Preview
button from the right-located sidebar.
Just after you'll be navigated to the Preview panel where you'll be able to manage and preview your content instantly. And that's it.
Using Preview Content
Preview content is supported since @vsf-enterprise/contentful-api@5.1.0
To enable preview content functionality, follow these steps:
1
2
Add x-preview header to the request
Include the x-preview
header in the request.
// Update the default request config...
unifiedCms:buildModule(middlewareModule<UnifiedCmsEndpoints>, {
apiUrl: `${config.apiUrl}/contentful/unified`,
cdnCacheBustingId: config.cdnCacheBustingId,
defaultRequestConfig: {
getConfigSwitcherHeader,
headers: {
...getRequestHeaders(),
+ 'x-preview': 'true',
},
},
ssrApiUrl: `${config.ssrApiUrl}/contentful/unified`,
}),
// ...or add it to the request config for a single request...
+ import { prepareConfig } from "@alokai/connect/sdk";
const { data } = await sdk.unifiedCms.getPage(
params,
+ prepareConfig({
+ headers: {
+ 'x-preview': 'true',
+ },
+ })
);
// ...or add it to the request based on the query params
+ import { prepareConfig } from "@alokai/connect/sdk";
+ import { useSearchParams } from 'next/navigation';
+ const searchParams = useSearchParams();
+ const preview = searchParams.get('preview');
const { data } = await sdk.unifiedCms.getPage(
params,
+ prepareConfig({
+ headers: {
+ 'x-preview': preview ? 'true' : undefined,
+ },
+ })
);