Skip to content

Commit

Permalink
chore: add comment about purgeRequireCache
Browse files Browse the repository at this point in the history
Signed-off-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
mcansh committed Sep 11, 2021
1 parent a2e1f45 commit de366b4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export let links: LinksFunction = () => {
};

export let loader: LoaderFunction = async () => {
return { message: "this is so awesome 😎" };
return { message: "this is so awesome 😎🤘" };
};

export default function Index() {
Expand Down
20 changes: 14 additions & 6 deletions netlify/functions/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ const path = require("path");

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) => {
for (const key of Object.keys(require.cache)) {
if (key.startsWith(BUILD_DIR)) {
delete require.cache[key];
}
}

purgeRequireCache();
return createRequestHandler({ build: require("./build") })(
event,
context
Expand Down

0 comments on commit de366b4

Please sign in to comment.