A Gatsby + Contentful + Netlify bolierplate multi-locale project.
This project is a modified clone of contentful-userland/gatsby-contentful-starter
- A good starting point for a new production JAMstack site that has ...
en-CA
andfr-CA
pages- CI/CD pipeline and architecture using Netlify (a different CI platform can be bolted on)
- A Content Management System (Contentful) with a
Production
andDevelopment
publishing environment
- Here's the deployed Production site: πhttps://multilocale-prod.netlify.com/
- Here's the deployed Development site: πhttps://multilocale-dev.netlify.com/
- I built this with these two languages in mind:
en-CA
andfr-CA
because I am a Canadian developer.- Contentful sets your default Locale to
en-US
- I haven't tested this project in scenarios for any other languages other than
en-CA
andfr-CA
but in theory you should be able to swap these out with whatever language Contentful supports
- Contentful sets your default Locale to
- Reach out to me on Twitter @jasonrundell with your questions/issues or, of course, here on GitHoob
Register or log in to your Contentful account. Contentful allows for free accounts with beefy enough rate limits for developing experiments or very low traffic sites.
-
Create a new space.
-
Select the preferred space type.
-
Name your space e.g. mysite.com and make sure the option for 'Create an empty space' is selected. Click the proceed to confirmation button.
-
Confirm and create space.
-
Navigate to Settings > General Settings and copy your space ID somewhere for use later.
-
Go to Settings > API keys and select the Content management tokens tab. Generate a personal token and save it somewhere safe. !!!You can not retrieve this token at any point afterwards!!! This token is used for Contentful API operations. Name it something appropriate but the name is not used for anything other than a visual reference from the Contentful UI.
-
Go to Settings > API keys and select the Content delivery/preview tokens tab. Click the Add API key button. Name your access token something appropriate (e.g.
mysite.com Production
). Description is optional. Make sure onlymaster
is checked in the Environments section. Save the changes. Copy down the Content Delivery API - access token. This key can be viewed at any time. This key will be used only for production builds. -
Go to Settings > Locales and click on English (United States). Change the Local to
English (Canada) (en-CA)
and Save. A modal will ask you to confirm by enteringen-US
into it and clicking the Git it, change locale button. -
Go to Settings > Locales and Add Locale. Select
French (Canada) (fr-CA)
from the list, None (no fallback) for Fallback locale, and make sure all three options are checked (Enable this locale in response, Enable editing for this locale, Allow empty fields for this locale). Save your locale.
-
Installation with command line:
git clone https://github.com/jasonrundell/gatsby-contentful-netlify-multilocale cd gatsby-contentful-netlify-multilocale yarn
Automate the setup of Contentful to get a project working right away!
Open a command window in your project's root folder and use the following command:
yarn setup
The yarn setup
command will ask you for a space ID, and access tokens for Contentful Management (step 6 in Contentful setup), Delivery API (step 7 in Contentful setup), your Environment ID (use master
) and then it will start to import the example content and content models (uses the json dump file ./contentful/export.json
) into your space. Finally, the script will write a .env.development
and .env.production
file for later use.
A successful import will output something similar to the following:
Writing config file...
Config file /Users/{user}/Documents/Github/jasonrundell/gatsby-contentful-multilocale/.env.development written
Config file /Users/{user}/Documents/Github/jasonrundell/gatsby-contentful-multilocale/.env.production written
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
β The following entities are going to be imported: β
βββββββββββββββββββββββββββββββββββ¬βββββββββββββββββ€
β Content Types β 1 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββ€
β Editor Interfaces β 1 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββ€
β Entries β 3 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββ€
β Assets β 7 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββ€
β Locales β 2 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββ€
β Webhooks β 0 β
βββββββββββββββββββββββββββββββββββ΄βββββββββββββββββ
β Validating content-file
β Initialize client (1s)
β Checking if destination space already has any content and retrieving it (1s)
β Apply transformations to source data (1s)
β Push content to destination space
β Connecting to space (1s)
β Importing Locales (1s)
β Importing Content Types (4s)
β Publishing Content Types (1s)
β Importing Editor Interfaces (1s)
β Importing Assets (8s)
β Publishing Assets (3s)
β Archiving Assets (0s)
β Importing Content Entries (4s)
β Publishing Content Entries (2s)
β Archiving Entries (1s)
β Creating Web Hooks (0s)
Finished importing all data
βββββββββββββββββββββββββ
β Imported entities β
βββββββββββββββββββββ¬ββββ€
β Locales β 2 β
βββββββββββββββββββββΌββββ€
β Content Types β 1 β
βββββββββββββββββββββΌββββ€
β Editor Interfaces β 1 β
βββββββββββββββββββββΌββββ€
β Assets β 7 β
βββββββββββββββββββββΌββββ€
β Published Assets β 7 β
βββββββββββββββββββββΌββββ€
β Archived Assets β 0 β
βββββββββββββββββββββΌββββ€
β Entries β 3 β
βββββββββββββββββββββΌββββ€
β Published Entries β 3 β
βββββββββββββββββββββΌββββ€
β Archived Entries β 0 β
βββββββββββββββββββββΌββββ€
β Webhooks β 0 β
βββββββββββββββββββββ΄ββββ
The import was successful.
All set! You can now run yarn run dev to see it in action.
You may see some rate limit messages - ignore these unless they actually stop the import process. The import script will do a good job of pausing to overcome these limits for you.
For an unknown reason (please let me know if you know why), importing content into Contentful that has multiple locales does not automatically enable the fields to show up when editing content in Contentful. Do the following to just one content item to enable fr-CA
fields on all content:
-
Navigate to https://app.contentful.com/spaces and go to your space.
-
Go to Content in the top navigation and edit one of your new pages.
-
In the right side bar, click Change, check off French in the modal window and click Save.
-
Navigate to https://app.contentful.com/spaces and go to your space.
-
Settings > Environments and add a new environment named
develop
. This will make a copy of the content, models, and assets frommaster
and become your development environment. -
Go to Settings > API keys and select the Content delivery/preview tokens tab. Click the Add API key button. Name your access token something appropriate (e.g.
mysite.com Development
). Description is optional. Make sure onlydevelop
is checked in the Environments section. Save the changes. The keys in this access will be used only for development builds.
Register or log in to your Contentful account. Contentful allows for free accounts with beefy enough rate limits for developing experiments. It's a CDN so it can handle and scale for a lot of web traffic!
-
Create a new site from Git.
-
Select GitHub/GitLab/Bitbucket.
-
This step will vary depending on how you configure Netlify's permissions with your Git platform of choice, but you need to target your git repo.
-
Set your owner.
-
Set Branch to deploy to
master
. -
Set Build command to
gatsby build
. -
Set Publish directory to
public/
. -
Click Show advanced and set the following variables:
- Key: CONTENTFUL_SPACE_ID,
Value:
your_space_id
- Key: CONTENTFUL_ACCESS_TOKEN,
Value:
your_production_delivery_api_key
- Key: CONTENTFUL_ENVIRONMENT_ID,
Value:
master
- Key: ENV,
Value:
production
- Key: CONTENTFUL_SPACE_ID,
Value:
-
Click Deploy site
Netlify will then begin running a build of your site and deploy it (if it succeeds). If the build fails, take a look at your deploy log. Your git repo's
master
branch may not be ina ready state, or perhaps you copied the wrong key into one of the Netlify variables from step 8 of Netlify setup. -
Go to Site settings.
-
Under Site information, change the site name from the random name Netlify assign you, to something that makes sense and can be identified quickly, e.g.
mysite-prod.netlify.com
(note that the subdomain you choose might not be available). This subdomain can easily be changed later and is more of a stub for DNS changes that would be normal for a real production site, but those instructions are not covered in this document.
-
Go back to Contentful https://app.contentful.com/spaces/.
-
Select your
mysite.com
space. -
Go to Apps in the top navigation.
-
Click on Netlify from the list.
-
Click Install
This app will bring the following functionality into Contentful for you when publishing content across your environments:
- triggers Netlify builds with a simple click in the sidebar
- displays build status so editors donβt preview outdated sites
- brings Netlify functionality into the Contentful web app
- makes it possible to configure on a per-content-type level
-
On the Connect Netlify screen, click Connect Account.
-
Authorize.
-
Set Netlify site to the Netlify site you made (e.g.
mysite-prod
). -
Set Display name to something appropriate for publishers to recognise as the Production site, e.g.
Production
. This name can be changed at any point later. -
Under Content types check off Select all.
-
Click Install (blue button in the top right of the UI).
You will return later to add the Development site.
We've set up our Production pipeline. In order to create our Development pipeline, we need to create a branch in our Git repo.
βοΈNOTE: This branch/pipeline can be named many different things by many different teams. It's been known to be called "UAT", "Pre-prod", "Perf", "Preview", and "Staging". Call it what you want, it's purpose is to serve as a mirror of Production because you want to see changes before they go to Production.
Use your preferred method of creating a branch off of your master
, but here is an example command line:
git checkout master
git branch develop
git checkout develop
git push origin develop
π‘TIP: It's good to add branch protection to develop
so that it can't be deleted accidentally.
Follow the same instructions from Netlify PRODUCTION setup with the following differences:
-
Set Branch to deploy to
develop
. -
Click Show advanced and set the following variables:
- Key: CONTENTFUL_SPACE_ID,
Value:
your_space_id
- Key: CONTENTFUL_ACCESS_TOKEN,
Value:
your_develop_delivery_api_key
- Key: CONTENTFUL_ENVIRONMENT_ID,
Value:
develop
- Key: ENV,
Value:
development
- Key: CONTENTFUL_SPACE_ID,
Value:
-
Under Site settings > Site information, change the site name from the random name Netlify assign you, to something that makes sense and can be identified quickly, e.g.
mysite-dev.netlify.com
.
Follow the same instructions from Netlify app setup in Contentful for PRODUCTION with the following differences:
-
Set Netlify site to the Netlify site you made (e.g.
mysite-dev
). -
Set Display name to something appropriate for publishers to recognise as the Development site, e.g.
Development
. This name can be changed at any point later.
Your Production and Development pipelines are now finished and you can begin customizing to your heart's delight! π
You can run the following to view you site and begin publishing:
yarn start
- Credit to rxaviers for the gist of emojis.
- Stefan Judis and all the contributors for the gatsby-contentful-starter repo.
- Contentful and Netlify for empowering developers with their free tiers!
- Gatsby for a fantastic framework that 'helps developers build blazing fast websites and apps'.