Setting Up Decap CMS
Table of Contents
Unfinished article
How it works
Netlify CMS is runs as a single page web app on your site that can access and edit the content sections of the site’s Git repo. Because this part of the site does not need to be processed by Hugo these files are kept in the static
folder. A folder at /static/admin
can then be accessed at www.example.com/admin
on your site. The simple CMS page, index.html
is actually a React app that uses React code from a CDN or kept locally.
What you need
You need an unbuilt static site on GitHub, GitLab or BitBucket that is built and hosted by Netlify. These can all be used for free.
Demo version
If you want to get an idea of whta the CMS is like there is online demo version that doesn’t even require a login.
Setup in brief
Here are the main steps to setup the CMS when hosted on Netlify:
On your site
- Add a folder called
admin
to your project’sstatic
folder and add two files: anindex.html
file andconfig.yml
(NOTconfig.yaml
). - Add two
<script>
tags to thisindex.html
file. The first, for a netlify identity widget, is added to the<head>
section. The second, the react script for the CMS, is added to the<body>
of your page. The netlify one is:
1 <script src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
For the CMS you can use either of the following:
1<script src="https://unpkg.com/netlify-cms@^2.0.0/dist/netlify-cms.js">
2<script src="https://cdn.jsdelivr.net/npm/netlify-cms@^2.0.0/dist/netlify-cms.js">
- Add the script tag for the Netlify Identity widget (above) to the
<head>
of your site’s mainindex.html
page. (This can also be done using Netlify’s script injection feature) - Add another short script just before the closing
<body>
tag of your site’s mainindex.html
page:
1<script>
2 if (window.netlifyIdentity) {
3 window.netlifyIdentity.on("init", user => {
4 if (!user) {
5 window.netlifyIdentity.on("login", () => {
6 document.location.href = "/admin/";
7 });
8 }
9 });
10 }
11</script>
On Netlify
- Enable Git Gateway. From the Netlify interface for your website go to
site configuration
(the second menu item). From here click onidentity
in the vertical menu then press theEnable Identity
button. - Under registration press
configure
and choose whether you wantOpen
orInvite only
. - Scroll down to services and click on
enable git gateway
. - Next click on
integrations
in the main menu then onidentity
. invite at least one user to be able to login into the CMS. The email provided will get a login link which will take you to the CMS where you’ll be prompted for a password of choosing. - Setup the
admin/config.yml
file. See the Netlify CMS config article for how to do this.
More detailed info
On the hosting side you need to set up authentication so that the CMS can access and make commits to your repo.
Although Netlify CMS is an open source project it is, at this time, easier to set up with a Netlify hosting account and is slightly better for some features if kept on GitHub.
The Netlify account makes it simple to set up authentication from GitHub. You can find this in the Netlify interface under the Site Settings > Identity > Services
section of the specific site (see below).
One click install
You can use a template specific to your static site generator and install with just a few clicks from here.
However setting up with a Hugo site that is already built and hosted by Netlify is fairly easy.
In your site’s project folders
You need to:
- Add an
admin
folder to the/static/
folder of your site. - Add two files to this, and
index.html
file and aconfig.yml
file.
If the index.html
file has a link the Netlify CMS script at a CDN you don’t need to do anything else.
If you don’t want to use a CDN for Netlify CMS it’s also possible to install it using NPM.
From Netlify
You can generate the access token from within the Netlify interface. From the site’s settings (far right end of the menu) click on Idenity
then Services
to find the Git Gateway
section. Click on the enable Git Gatewy
button. Very easy and that’s it done!
From GitHub
But you can also generate a new token from GitHub. Go to your GitHub account settings in the top right dropdown. From there choose Developer settings near the bottom of the list. Here you can choose Personal access tokens and press the Generate new token button. Copy this immediately as you cannot reaccess it again. You then need to paste it into your Netlify project settings.
To generate a PAT (personal access token) from GitHub click on your profile pic (top right) and choose settings. These are different from the settings for each repo. From there scroll down a click on the the Developer settings button near the bottom.
From here you can choose Personal Access Tokens and click generate a new token.
Add a note and set the expiration date and click the sections on the form.
Expiration of access tokens
From GitHub:
As a security precaution, GitHub automatically removes personal access tokens that haven’t been used in a year. To provide additional security, we highly recommend adding an expiration to your personal access tokens.
More on Netlify Identity in Netlify docs
Creating a personal ccess token on GitHub
Adding Hugo shortcode menu to the CMS UI
There’s another script that adds a menu dropdown to the rich text options of the CMS UI.
1<script src="https://sharadcodes.github.io/hugo-shortcodes-netlify-cms/dist/hugo_shortcodes_netlify_cms.js"></script>
If you add this to the static/admin/index.html
file BELOW the CMS script in the <body>
section of the page.
Then in the UI for editing a page you get this selection:
The link above is a Javascript file made up of different blocks for each shortcode and uses the CMS.registerEditorComponent
for each block. Here is the code for Hugo’s built in <figure>
element shortcode:
1CMS.registerEditorComponent({
2 id: "figure",
3 label: "Figure",
4 fields: [{
5 name: "title",
6 label: "Figure Title",
7 widget: "string"
8 },
9 {
10 name: "src",
11 label: "Figure SRC",
12 widget: "string"
13 },
14 ],
15 pattern: /
/,
16 fromBlock: function(match) {
17 return {
18 title: match[1],
19 src: match[2],
20 };
21 },
22 toBlock: function(obj) {
23 return `
`;
24 },
25 toPreview: function(obj) {
26 return `<figure><img src=${obj.src} alt=${obj.title}><figcaption>${obj.title}</figcaption></figure>`;
27 },
28});
See the GitHub page for this code.
GitHub GraphQL API
One of the new beta features that can be used is the new GitHub GraphQL API.
See also: Netlify CMS Configuration