Retain in-memory cache for builds only.

This commit is contained in:
Dillon Kearns 2021-07-09 13:28:22 -07:00
parent 036271288a
commit 5df5c654dd
2 changed files with 12 additions and 5 deletions

View File

@ -12,6 +12,7 @@ async function run({ mode, pathname }) {
const req = null; const req = null;
const renderResult = await renderer( const renderResult = await renderer(
requireElm(mode), requireElm(mode),
mode,
pathname, pathname,
req, req,
function (patterns) { function (patterns) {

View File

@ -24,12 +24,13 @@ module.exports =
* @param {(pattern: string) => void} addDataSourceWatcher * @param {(pattern: string) => void} addDataSourceWatcher
* @returns * @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 // 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) // we can provide a fake HTTP instead of xhr2 (which is otherwise needed for Elm HTTP requests from Node)
XMLHttpRequest = {}; XMLHttpRequest = {};
const result = await runElmApp( const result = await runElmApp(
elmModule, elmModule,
mode,
path, path,
request, request,
addDataSourceWatcher addDataSourceWatcher
@ -40,11 +41,12 @@ module.exports =
/** /**
* @param {Object} elmModule * @param {Object} elmModule
* @param {string} pagePath * @param {string} pagePath
* @param {string} mode
* @param {import('aws-lambda').APIGatewayProxyEvent} request * @param {import('aws-lambda').APIGatewayProxyEvent} request
* @param {(pattern: string) => void} addDataSourceWatcher * @param {(pattern: string) => void} addDataSourceWatcher
* @returns {Promise<({is404: boolean} & ( { kind: 'json'; contentJson: string} | { kind: 'html'; htmlString: string } | { kind: 'api-response'; body: string; }) )>} * @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 patternsToWatch = new Set();
let app = null; let app = null;
let killApp; let killApp;
@ -57,7 +59,7 @@ function runElmApp(elmModule, pagePath, request, addDataSourceWatcher) {
app = elmModule.Elm.TemplateModulesBeta.init({ app = elmModule.Elm.TemplateModulesBeta.init({
flags: { flags: {
secrets: process.env, secrets: process.env,
staticHttpCache: global.staticHttpCache, staticHttpCache: global.staticHttpCache || {},
request: { request: {
payload: modifiedRequest, payload: modifiedRequest,
kind: "single-page", kind: "single-page",
@ -78,7 +80,9 @@ function runElmApp(elmModule, pagePath, request, addDataSourceWatcher) {
console.log(fromElm.value); console.log(fromElm.value);
} else if (fromElm.tag === "ApiResponse") { } else if (fromElm.tag === "ApiResponse") {
const args = fromElm.args[0]; const args = fromElm.args[0];
global.staticHttpCache = args.staticHttpCache; if (mode === "build") {
global.staticHttpCache = args.staticHttpCache;
}
resolve({ resolve({
kind: "api-response", kind: "api-response",
@ -88,7 +92,9 @@ function runElmApp(elmModule, pagePath, request, addDataSourceWatcher) {
}); });
} else if (fromElm.tag === "PageProgress") { } else if (fromElm.tag === "PageProgress") {
const args = fromElm.args[0]; const args = fromElm.args[0];
global.staticHttpCache = args.staticHttpCache; if (mode === "build") {
global.staticHttpCache = args.staticHttpCache;
}
if (isJson) { if (isJson) {
resolve({ resolve({