Skip to content

Commit

Permalink
fix(init/netlify): purge netlify require cache (remix-run#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcansh authored Sep 21, 2021
1 parent e5a8e85 commit 8a6fa27
Showing 1 changed file with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
const path = require("path");

const { createRequestHandler } = require("@remix-run/netlify");

exports.handler = createRequestHandler({
build: require("./build")
});
const BUILD_DIR = path.join(process.cwd(), "netlify");

function purgeRequireCache() {
// purge require cache on requests for "server side HMR" this won't let
// you have in-memory objects between requests in development,
// netlify typically does this for you, but we've found it to be hit or
// miss and some times requires you to refresh the page after it auto reloads
// or even have to restart your server
for (let key in require.cache) {
if (key.startsWith(BUILD_DIR)) {
delete require.cache[key];
}
}
}

exports.handler =
process.env.NODE_ENV === "production"
? createRequestHandler({ build: require("./build") })
: (event, context) => {
purgeRequireCache();
return createRequestHandler({ build: require("./build") })(
event,
context
);
};

0 comments on commit 8a6fa27

Please sign in to comment.