Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to preserve cache entries #3

Closed
novemberborn opened this issue Nov 17, 2016 · 2 comments
Closed

Option to preserve cache entries #3

novemberborn opened this issue Nov 17, 2016 · 2 comments

Comments

@novemberborn
Copy link

I'm using stealthy-require to reload React components for server-side rendering. One of their dependencies, namely react-helmet should not be reloaded.

My current workaround is:

const stealthyRequire = require('stealthy-require')
const helmetModule = require.cache[require.resolve('react-helmet')]

const loadView =  name => () => stealthyRequire(require.cache, () => {
  require.cache[helmetModule.id] = helmetModule
  return getUniversalView(name)
})

It'd be neat if stealthyRequire would take IDs that should not be cleared. Perhaps:

stealthyRequire({
  cache: require.cache,
  keep: [require.resolve('react-helmet')]
}, () => {})
@analog-nico
Copy link
Owner

Good idea! I'll be including it in the next release.

@analog-nico
Copy link
Owner

I just released version 1.1.0 which adds this option. See at the bottom of the usage section of the README.

It turned out to be much more complicated than your suggestion below because (a) Browserify and Webpack need to be supported, (b) the module which should not be reloaded may or may not be cached already when the stealthyRequire(...) call is made, and (c) the module – i.e. react-helmet – may itself depend on modules that also should not be reloaded. Therefore my universal solution has the drawback of having to require react-helmet an additional time and discards that module instance immediately. That impacts the load performance.

Since you are using node.js and apparently react-helmet is already cached before the stealthyRequire(...) call is executed, you may choose to continue using your solution which is not universal but sufficient for you and more performant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants