From 5df5c654dd28de2d8846fdf8d4fb61f9c97b1359 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Fri, 9 Jul 2021 13:28:22 -0700 Subject: [PATCH] Retain in-memory cache for builds only. --- generator/src/render-worker.js | 1 + generator/src/render.js | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/generator/src/render-worker.js b/generator/src/render-worker.js index 91aa292f..469a24da 100644 --- a/generator/src/render-worker.js +++ b/generator/src/render-worker.js @@ -12,6 +12,7 @@ async function run({ mode, pathname }) { const req = null; const renderResult = await renderer( requireElm(mode), + mode, pathname, req, function (patterns) { diff --git a/generator/src/render.js b/generator/src/render.js index e8728343..7b84528b 100755 --- a/generator/src/render.js +++ b/generator/src/render.js @@ -24,12 +24,13 @@ module.exports = * @param {(pattern: string) => void} addDataSourceWatcher * @returns */ - async function run(elmModule, path, request, addDataSourceWatcher) { + async function run(elmModule, mode, path, request, addDataSourceWatcher) { // since init/update are never called in pre-renders, and DataSource.Http is called using undici // we can provide a fake HTTP instead of xhr2 (which is otherwise needed for Elm HTTP requests from Node) XMLHttpRequest = {}; const result = await runElmApp( elmModule, + mode, path, request, addDataSourceWatcher @@ -40,11 +41,12 @@ module.exports = /** * @param {Object} elmModule * @param {string} pagePath + * @param {string} mode * @param {import('aws-lambda').APIGatewayProxyEvent} request * @param {(pattern: string) => void} addDataSourceWatcher * @returns {Promise<({is404: boolean} & ( { kind: 'json'; contentJson: string} | { kind: 'html'; htmlString: string } | { kind: 'api-response'; body: string; }) )>} */ -function runElmApp(elmModule, pagePath, request, addDataSourceWatcher) { +function runElmApp(elmModule, mode, pagePath, request, addDataSourceWatcher) { let patternsToWatch = new Set(); let app = null; let killApp; @@ -57,7 +59,7 @@ function runElmApp(elmModule, pagePath, request, addDataSourceWatcher) { app = elmModule.Elm.TemplateModulesBeta.init({ flags: { secrets: process.env, - staticHttpCache: global.staticHttpCache, + staticHttpCache: global.staticHttpCache || {}, request: { payload: modifiedRequest, kind: "single-page", @@ -78,7 +80,9 @@ function runElmApp(elmModule, pagePath, request, addDataSourceWatcher) { console.log(fromElm.value); } else if (fromElm.tag === "ApiResponse") { const args = fromElm.args[0]; - global.staticHttpCache = args.staticHttpCache; + if (mode === "build") { + global.staticHttpCache = args.staticHttpCache; + } resolve({ kind: "api-response", @@ -88,7 +92,9 @@ function runElmApp(elmModule, pagePath, request, addDataSourceWatcher) { }); } else if (fromElm.tag === "PageProgress") { const args = fromElm.args[0]; - global.staticHttpCache = args.staticHttpCache; + if (mode === "build") { + global.staticHttpCache = args.staticHttpCache; + } if (isJson) { resolve({