From cc8aec035ad797de52b3b971aed35322754f0504 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Thu, 24 Jan 2019 21:55:42 +0100 Subject: [PATCH] update cache on query change --- .../gatsby-plugin-sharp/src/gatsby-node.js | 26 ++++++++++++------- packages/gatsby/src/commands/develop.js | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/gatsby-plugin-sharp/src/gatsby-node.js b/packages/gatsby-plugin-sharp/src/gatsby-node.js index 6dfd89373e01a..e9e3df1759782 100644 --- a/packages/gatsby-plugin-sharp/src/gatsby-node.js +++ b/packages/gatsby-plugin-sharp/src/gatsby-node.js @@ -17,15 +17,7 @@ const getQueueFromCache = async cache => { return new Map(rawQueue) } -exports.onPreInit = async ({ actions }, pluginOptions) => { - setBoundActionCreators(actions) - setPluginOptions(pluginOptions) -} - -/** - * save queue to the cache - */ -exports.onPostBootstrap = async ({ cache }) => { +const saveQueueToCache = async cache => { const cachedQueue = await getQueueFromCache(cache) // merge both queues @@ -37,6 +29,16 @@ exports.onPostBootstrap = async ({ cache }) => { return cache.set(`queue`, Array.from(queue)) } +exports.onPreInit = async ({ actions }, pluginOptions) => { + setBoundActionCreators(actions) + setPluginOptions(pluginOptions) +} + +/** + * save queue to the cache + */ +exports.onPostBootstrap = async ({ cache, store }) => saveQueueToCache(cache) + /** * Execute all unprocessed images on gatsby build */ @@ -62,7 +64,11 @@ exports.onPostBuild = ({ cache, reporter }) => /** * Build images on the fly when they are requested by the browser */ -exports.onCreateDevServer = ({ app, cache }, pluginOptions) => { +exports.onCreateDevServer = async ({ app, cache, compiler }, pluginOptions) => { + compiler.hooks.done.tap(`Gatsby`, () => { + saveQueueToCache(cache) + }) + app.use(async (req, res, next) => { const queue = await getQueueFromCache(cache) if (!queue.has(req.originalUrl)) { diff --git a/packages/gatsby/src/commands/develop.js b/packages/gatsby/src/commands/develop.js index 338c21e1d37e5..d079ed86721f4 100644 --- a/packages/gatsby/src/commands/develop.js +++ b/packages/gatsby/src/commands/develop.js @@ -171,7 +171,7 @@ async function startServer(program) { }) } - await apiRunnerNode(`onCreateDevServer`, { app }) + await apiRunnerNode(`onCreateDevServer`, { app, compiler }) // Render an HTML page and serve it. app.use((req, res, next) => {