Skip to content

Commit

Permalink
Avoid collisions between schema hot reloader and __refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar committed Apr 11, 2020
1 parent cfa3779 commit ff36bbc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 11 additions & 2 deletions packages/gatsby/src/bootstrap/schema-hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,28 @@ const maybeRebuildSchema = debounce(async (): Promise<void> => {
activity.end()
}, 1000)

const snapshotInferenceMetadata = (): void => {
function snapshotInferenceMetadata(): void {
const { inferenceMetadata } = store.getState()
lastMetadata = cloneDeep(inferenceMetadata)
}

export const bootstrapSchemaHotReloader = (): void => {
export function bootstrapSchemaHotReloader(): void {
// Snapshot inference metadata at the time of the last schema rebuild
// (even if schema was rebuilt elsewhere)
// Using the snapshot later to check if inferred types actually changed since the last rebuild
snapshotInferenceMetadata()
emitter.on(`SET_SCHEMA`, snapshotInferenceMetadata)

startSchemaHotReloader()
}

export function startSchemaHotReloader(): void {
// Listen for node changes outside of a regular sourceNodes API call,
// e.g. markdown file update via watcher
emitter.on(`API_RUNNING_QUEUE_EMPTY`, maybeRebuildSchema)
}

export function stopSchemaHotReloader(): void {
emitter.off(`API_RUNNING_QUEUE_EMPTY`, maybeRebuildSchema)
maybeRebuildSchema.cancel()
}
12 changes: 9 additions & 3 deletions packages/gatsby/src/commands/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ import * as WorkerPool from "../utils/worker/pool"
import http from "http"
import https from "https"

import { bootstrapSchemaHotReloader } from "../bootstrap/schema-hot-reloader"
import {
bootstrapSchemaHotReloader,
startSchemaHotReloader,
stopSchemaHotReloader,
} from "../bootstrap/schema-hot-reloader"
import bootstrapPageHotReloader from "../bootstrap/page-hot-reloader"
import { developStatic } from "./develop-static"
import withResolverContext from "../schema/context"
import sourceNodes from "../utils/source-nodes"
import { createSchemaCustomization } from "../utils/create-schema-customization"
import { rebuild } from "../schema"
import { rebuild as rebuildSchema } from "../schema"
import { websocketManager } from "../utils/websocket-manager"
import getSslCert from "../utils/get-ssl-cert"
import { slash } from "gatsby-core-utils"
Expand Down Expand Up @@ -204,6 +208,7 @@ async function startServer(program: IProgram): Promise<IServer> {
**/
const REFRESH_ENDPOINT = `/__refresh`
const refresh = async (req: express.Request): Promise<void> => {
stopSchemaHotReloader()
let activity = report.activityTimer(`createSchemaCustomization`, {})
activity.start()
await createSchemaCustomization({
Expand All @@ -218,8 +223,9 @@ async function startServer(program: IProgram): Promise<IServer> {
activity.end()
activity = report.activityTimer(`rebuild schema`)
activity.start()
await rebuild({ parentSpan: activity })
await rebuildSchema({ parentSpan: activity })
activity.end()
startSchemaHotReloader()
}
app.use(REFRESH_ENDPOINT, express.json())
app.post(REFRESH_ENDPOINT, (req, res) => {
Expand Down

0 comments on commit ff36bbc

Please sign in to comment.