|
| 1 | +# gatsby-plugin-static-cms |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Automatically generates an `admin/index.html` with a default Static CMS implementation. |
| 6 | + |
| 7 | +Static CMS is a React single page app for editing git based content via API. |
| 8 | +Its built for non-technical and technical editors alike, and its super easy to |
| 9 | +install and configure. For more details, check out the [docs |
| 10 | +site](https://staticjscms.netlify.app). |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +```shell |
| 15 | +npm install @staticcms/core gatsby-plugin-static-cms |
| 16 | +``` |
| 17 | + |
| 18 | +## How to use |
| 19 | + |
| 20 | +Add the Static CMS plugin in your `gatsby-config.js`: |
| 21 | + |
| 22 | +```javascript |
| 23 | +plugins: [`gatsby-plugin-static-cms`] |
| 24 | +``` |
| 25 | + |
| 26 | +Then add your Static CMS [configuration |
| 27 | +file](https://staticjscms.netlify.app/docs/add-to-your-site-cdn#configuration) in |
| 28 | +`static/admin/config.yml`. |
| 29 | + |
| 30 | +## Options |
| 31 | + |
| 32 | +Static CMS can be configured via the plugin options below. You can learn |
| 33 | +about how to pass options to plugins in the [Gatsby |
| 34 | +docs](https://www.gatsbyjs.com/docs/plugins/#how-to-use-gatsby-plugins). |
| 35 | + |
| 36 | +### `modulePath` |
| 37 | + |
| 38 | +(_optional_, type: `string | Array<string>`, default: `undefined`) |
| 39 | + |
| 40 | +If you need to customize Static CMS, e.g. registering [custom |
| 41 | +widgets](https://staticjscms.netlify.app/docs/custom-widgets/#registerwidget) or |
| 42 | +styling the [preview |
| 43 | +pane](https://staticjscms.netlify.app/docs/customization/#registerpreviewstyle), |
| 44 | +you'll need to do so in a JavaScript module and provide Gatsby with the path to |
| 45 | +your module via the `modulePath` option. Any styles imported by this module (or |
| 46 | +by the modules that it imports, all the way down the chain) are automatically |
| 47 | +applied to the editor preview pane by the plugin. |
| 48 | + |
| 49 | +```javascript |
| 50 | +plugins: [ |
| 51 | + { |
| 52 | + resolve: `gatsby-plugin-static-cms`, |
| 53 | + options: { |
| 54 | + /** |
| 55 | + * One convention is to place your Static CMS customization code in a |
| 56 | + * `src/cms` directory. |
| 57 | + */ |
| 58 | + modulePath: `${__dirname}/src/cms/cms.js`, |
| 59 | + }, |
| 60 | + }, |
| 61 | +] |
| 62 | +``` |
| 63 | + |
| 64 | +The js module might look like this: |
| 65 | + |
| 66 | +```javascript |
| 67 | +/** |
| 68 | + * The default export of `@staticcms/core` is an object with all of the Static CMS |
| 69 | + * extension registration methods, such as `registerWidget` and |
| 70 | + * `registerPreviewTemplate`. |
| 71 | + */ |
| 72 | +import CMS from "@staticcms/core" |
| 73 | + |
| 74 | +/** |
| 75 | + * Any imported styles should be automatically be applied to the editor preview |
| 76 | + * pane thus eliminating the need to use `registerPreviewStyle` for imported |
| 77 | + * styles. However if you are experiencing build errors regarding importing css, |
| 78 | + * sass or scss into a cms module when deploying to the netlify platform, you |
| 79 | + * may need to follow the implementation found in netlify documentation here: |
| 80 | + * https://staticjscms.netlify.app/docs/beta-features/#raw-css-in-registerpreviewstyle |
| 81 | + * All of the example imports below would result in styles being applied to the |
| 82 | + * preview pane. |
| 83 | + */ |
| 84 | +import "module-that-imports-styles.js" |
| 85 | +import "styles.scss" |
| 86 | +import "../other-styles.css" |
| 87 | + |
| 88 | +/** |
| 89 | + * Let's say you've created widget and preview components for a custom image |
| 90 | + * gallery widget in separate files: |
| 91 | + */ |
| 92 | +import ImageGalleryWidget from "./image-gallery-widget.js" |
| 93 | +import ImageGalleryPreview from "./image-gallery-preview.js" |
| 94 | + |
| 95 | +/** |
| 96 | + * Register the imported widget: |
| 97 | + */ |
| 98 | +CMS.registerWidget(`image-gallery`, ImageGalleryWidget, ImageGalleryPreview) |
| 99 | +``` |
| 100 | + |
| 101 | +### `manualInit` |
| 102 | + |
| 103 | +(_optional_, type: `boolean`, default: `false`) |
| 104 | + |
| 105 | +Set this to `true` If you need to [manually initialize](https://staticjscms.netlify.app/docs/beta-features/#manual-initialization) Static CMS. The plugin will take care of setting `window.CMS_MANUAL_INIT` to `true`: |
| 106 | + |
| 107 | +```javascript |
| 108 | +plugins: [ |
| 109 | + { |
| 110 | + resolve: `gatsby-plugin-static-cms`, |
| 111 | + options: { |
| 112 | + manualInit: true, |
| 113 | + }, |
| 114 | + }, |
| 115 | +] |
| 116 | +``` |
| 117 | + |
| 118 | +The js module might look like this: |
| 119 | + |
| 120 | +```javascript |
| 121 | +import CMS from "@staticcms/core" |
| 122 | + |
| 123 | +/** |
| 124 | + * Optionally pass in a config object. This object will be merged into `config.yml` if it exists |
| 125 | + */ |
| 126 | + |
| 127 | +CMS.init({ |
| 128 | + config: { |
| 129 | + backend: { |
| 130 | + name: "git-gateway", |
| 131 | + }, |
| 132 | + }, |
| 133 | +}) |
| 134 | +``` |
| 135 | + |
| 136 | +### `enableIdentityWidget` |
| 137 | + |
| 138 | +(_optional_, type: `boolean`, default: `true`) |
| 139 | + |
| 140 | +`enableIdentityWidget` is `true` by default, allowing [Netlify |
| 141 | +Identity](https://www.netlify.com/docs/identity/) to be used without |
| 142 | +configuration. Disable it when not using Netlify Identity to reduce bundle size. |
| 143 | + |
| 144 | +```javascript |
| 145 | +plugins: [ |
| 146 | + { |
| 147 | + resolve: `gatsby-plugin-static-cms`, |
| 148 | + options: { |
| 149 | + enableIdentityWidget: true, |
| 150 | + }, |
| 151 | + }, |
| 152 | +] |
| 153 | +``` |
| 154 | + |
| 155 | +### `publicPath` |
| 156 | + |
| 157 | +(_optional_, type: `string`, default: `"admin"`) |
| 158 | + |
| 159 | +Customize the path to Static CMS on your Gatsby site. |
| 160 | + |
| 161 | +### `htmlTitle` |
| 162 | + |
| 163 | +(_optional_, type: `string`, default: `Content Manager`) |
| 164 | + |
| 165 | +Customize the value of the `title` tag in your CMS HTML (shows in the browser |
| 166 | +bar). |
| 167 | + |
| 168 | +### `htmlFavicon` |
| 169 | + |
| 170 | +(_optional_, type: `string`, default: `""`) |
| 171 | + |
| 172 | +Customize the value of the `favicon` tag in your CMS HTML (shows in the browser |
| 173 | +bar). |
| 174 | + |
| 175 | +### `includeRobots` |
| 176 | + |
| 177 | +(_optional_, type: `boolean`, default: `false`) |
| 178 | + |
| 179 | +By default, the CMS page is not indexed by crawlers. Use this to add a `meta` tag to invite robots to index the CMS page. |
| 180 | + |
| 181 | +### `customizeWebpackConfig` |
| 182 | + |
| 183 | +(_optional_, type: `function`) |
| 184 | + |
| 185 | +Function to customize webpack configuration. |
| 186 | + |
| 187 | +Function parameters: |
| 188 | + |
| 189 | +- config: webpack configuration for StaticCMS |
| 190 | +- destructured object from onCreateWebpackConfig { store, stage, pathPrefix, getConfig, rules, loaders, plugins} as seen in https://www.gatsbyjs.com/docs/node-apis/#onCreateWebpackConfig |
| 191 | + |
| 192 | +```javascript |
| 193 | +plugins: [ |
| 194 | + { |
| 195 | + resolve: `gatsby-plugin-static-cms`, |
| 196 | + options: { |
| 197 | + customizeWebpackConfig: (config, { plugins }) => { |
| 198 | + const Plugin = require("...") |
| 199 | + |
| 200 | + config.plugins.push( |
| 201 | + plugins.define({ |
| 202 | + "process.env.MY_VAR": JSON.stringify("my var value"), |
| 203 | + }) |
| 204 | + ) |
| 205 | + |
| 206 | + config.plugins.push(new Plugin()) |
| 207 | + }, |
| 208 | + }, |
| 209 | + }, |
| 210 | +] |
| 211 | +``` |
| 212 | + |
| 213 | +## Example |
| 214 | + |
| 215 | +Here is the plugin with example values for all options (note that no option is |
| 216 | +required): |
| 217 | + |
| 218 | +```javascript |
| 219 | +plugins: [ |
| 220 | + { |
| 221 | + resolve: `gatsby-plugin-static-cms`, |
| 222 | + options: { |
| 223 | + modulePath: `path/to/custom/script.js`, // default: undefined |
| 224 | + enableIdentityWidget: true, |
| 225 | + publicPath: `admin`, |
| 226 | + htmlTitle: `Content Manager`, |
| 227 | + htmlFavicon: `path/to/favicon`, |
| 228 | + includeRobots: false, |
| 229 | + }, |
| 230 | + }, |
| 231 | +] |
| 232 | +``` |
| 233 | + |
| 234 | +## Disable widget on site |
| 235 | + |
| 236 | +If you're not using Netlify Identity within your site you have the option to completely disable the widget (and not the CMS). To do so, add the following to `gatsby-node.js`: |
| 237 | + |
| 238 | +```javascript |
| 239 | +const webpack = require(`webpack`) |
| 240 | + |
| 241 | +exports.onCreateWebpackConfig = ({ actions }) => { |
| 242 | + actions.setWebpackConfig({ |
| 243 | + plugins: [ |
| 244 | + new webpack.IgnorePlugin({ |
| 245 | + resourceRegExp: /^netlify-identity-widget$/, |
| 246 | + }), |
| 247 | + ], |
| 248 | + }) |
| 249 | +} |
| 250 | +``` |
| 251 | + |
| 252 | +## Support |
| 253 | + |
| 254 | +For help with integrating Static CMS with Gatsby, check out the community |
| 255 | +[Slack](https://staticjscms.netlify.app/chat). |
0 commit comments