Skip to content

Commit

Permalink
Add ability to refresh data on the development server
Browse files Browse the repository at this point in the history
The external source data can be refreshed by making a POST request to
the `__refresh` endpoint. If the env var GATSBY_REFRESH_TOKEN is present
there needs to be a matching `Authorization` header. This post request
is wrapped for convenience with the command `gatsby refresh` which uses
localhost:8000 by default, but allows for a host and port to be
specified in the same way as `gatsby develop`.
  • Loading branch information
Justin Schulz committed Dec 4, 2017
1 parent e6924c4 commit 64c6d2a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/gatsby-cli/src/create-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ function buildLocalCommands(cli, isLocalSite) {
handler: getCommandHandler(`develop`),
})

cli.command({
command: `refresh`,
desc: `Reload external data sources`,
builder: _ =>
_.option(`H`, {
alias: `host`,
type: `string`,
default: defaultHost,
describe: `Set host. Defaults to ${defaultHost}`,
})
.option(`p`, {
alias: `port`,
type: `string`,
default: `8000`,
describe: `Set port. Defaults to 8000`,
}),
handler: getCommandHandler(`refresh`),
})

cli.command({
command: `build`,
desc: `Build a Gatsby project.`,
Expand Down
14 changes: 14 additions & 0 deletions packages/gatsby/src/commands/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const launchEditor = require(`react-dev-utils/launchEditor`)
const formatWebpackMessages = require(`react-dev-utils/formatWebpackMessages`)
const chalk = require(`chalk`)
const address = require(`address`)
const sourceNodes = require(`../utils/source-nodes`)

// const isInteractive = process.stdout.isTTY

Expand Down Expand Up @@ -91,6 +92,19 @@ async function startServer(program) {
graphiql: true,
})
)

/**
* Refresh external data sources.
* If no GATSBY_REFRESH_TOKEN env var is available, then no Authorization header is required
**/
app.post(`/__refresh`, (req, res) => {
if (req.headers.authorization === process.env.GATSBY_REFRESH_TOKEN) {
console.log(`Refreshing source data`)
sourceNodes()
}
res.end()
})

app.get(`/__open-stack-frame-in-editor`, (req, res) => {
launchEditor(req.query.fileName, req.query.lineNumber)
res.end()
Expand Down
26 changes: 26 additions & 0 deletions packages/gatsby/src/commands/refresh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* @flow */

const http = require(`http`)

module.exports = async (program: { host: string, port: string }) => {
const options = {
host: program.host,
port: program.port,
path: `/__refresh`,
method: `POST`,
}

const req = http.request(options, (res) => {
res.on(`end`, () => {
console.log()
console.info(`Successfully refreshed external data`)
})
})

req.on(`error`, (e) => {
console.log()
console.error(`Unable to refresh external data: ${e.message}`)
})

req.end()
}

0 comments on commit 64c6d2a

Please sign in to comment.