-
-
Save CosAnca/f776edd4d4af16f66e259d504b785b74 to your computer and use it in GitHub Desktop.
Sample gatsby-config.js enabling live preview in Craft CMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { createHttpLink } = require('apollo-link-http'); | |
| const fetch = require('node-fetch'); | |
| const store = require('store'); | |
| const sourceNodes = require('gatsby/dist/utils/source-nodes'); | |
| require('dotenv').config(); | |
| const craftGqlUrl = process.env.CRAFT_GQL_URL; | |
| const craftGqlToken = process.env.CRAFT_GQL_TOKEN; | |
| module.exports = { | |
| plugins: [ | |
| { | |
| resolve: 'gatsby-source-graphql', | |
| options: { | |
| typeName: 'Craft', | |
| fieldName: 'craft', | |
| createLink: () => | |
| createHttpLink({ | |
| uri: `${craftGqlUrl}`, | |
| headers: { | |
| Authorization: `Bearer ${craftGqlToken}`, | |
| }, | |
| fetch: (uri, options) => { | |
| const token = store.get('X-Craft-Token'); | |
| return fetch(`${uri}?token=${token}`, options); | |
| }, | |
| }), | |
| }, | |
| }, | |
| ], | |
| developMiddleware: app => { | |
| app.use('*', (req, res, next) => { | |
| if (req.query.token) { | |
| store.set('X-Craft-Token', req.query.token); | |
| sourceNodes(); | |
| } | |
| next(); | |
| }); | |
| }, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment