Skip to content

Instantly share code, notes, and snippets.

@cpv123
Last active January 28, 2020 02:43
Show Gist options
  • Save cpv123/454600f5f7a2c139eb9dd6e3c467d51a to your computer and use it in GitHub Desktop.
Save cpv123/454600f5f7a2c139eb9dd6e3c467d51a to your computer and use it in GitHub Desktop.
// gatsby-node.js file
const axios = require('axios')
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`, // this dotenv config gives access to process.env object
})
const getCoffeeShops = async () => {
const shops = await axios.get(process.env.GATSBY_COFFEE_SHOPS_URL)
return shops.data.Items
}
exports.onCreatePage = async ({ page, actions }) => {
if (page.path === '/coffee' || page.path === '/coffee/') {
const { createPage, deletePage } = actions
deletePage(page)
const coffeeShops = await getCoffeeShops()
createPage({
...page,
context: {
...page.context,
coffeeShops,
},
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment