-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby): provide a mechanism for plugins to use a named cache instance #10146
Conversation
Note: this injects a getCache helper (when available) that can be used to get a cache instance
@@ -3,7 +3,7 @@ const crypto = require(`crypto`) | |||
const _ = require(`lodash`) | |||
|
|||
module.exports = async function onCreateNode( | |||
{ node, getNode, loadNodeContent, actions, createNodeId, reporter }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't used
@DSchau I think CI failed due |
@@ -133,6 +133,7 @@ module.exports = ( | |||
getNode, | |||
reporter, | |||
cache, | |||
...rest, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if doing something like:
cache: getCache(plugin.id)
to pass individual cache for subplugin woudln't make sense, so subplugins don't need to use getCache
directly, and would automatically get same cache
instance as in their gatsby-node
API hooks?
Questions:
- Is there scenario where it is actually desirable that subplugin use parent plugin cache?
I can't think if reason for this of top of my head. - Will this break any subplugin?
Cache key generating functions (gatsby/packages/gatsby-transformer-remark/src/extend-node-type.js
Lines 31 to 50 in 6a8c593
const astCacheKey = node => `transformer-remark-markdown-ast-${ node.internal.contentDigest }-${pluginsCacheStr}-${pathPrefixCacheStr}` const htmlCacheKey = node => `transformer-remark-markdown-html-${ node.internal.contentDigest }-${pluginsCacheStr}-${pathPrefixCacheStr}` const htmlAstCacheKey = node => `transformer-remark-markdown-html-ast-${ node.internal.contentDigest }-${pluginsCacheStr}-${pathPrefixCacheStr}` const headingsCacheKey = node => `transformer-remark-markdown-headings-${ node.internal.contentDigest }-${pluginsCacheStr}-${pathPrefixCacheStr}` const tableOfContentsCacheKey = node => `transformer-remark-markdown-toc-${ node.internal.contentDigest }-${pluginsCacheStr}-${pathPrefixCacheStr}`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ultimately I would like to prep helper functions/objects in core, so parent plugin could pass prepped helpers, but this is out of scope for this PR I think
Is anyone looking into this @pieh? |
@pieh I provided a relatively janky e2e test to validate this functionality in 307b49e. Not in love with it, but it is a nice way to validate this--open to suggestions or just removing it. One thought I had here is that this is actually a breaking change for gatsby-transformer-remark as currently authored.
Alternatively, I can write a little utility that will guard against const getSubPluginCache = (plugin, { getCache, cache }) => {
if (getCache) {
return getCache(plugin.name);
}
return cache
}
// later
cache: getSubPluginCache(plugin, { getCache, cache }) Anything I'm missing? Sound reasonable? |
the As for
I think we should do both and even possibly warn user to update gatsby version if |
@pieh lol I almost proposed guessing what the peerDep would be 😆 I'll get this ship shape and split them apart and add in a little utility. |
@pieh want to take another look at this today? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Fixes #8788
This PR provides a mechanism for any plugin to call a
getCache
helper, which takes a single argument (name) and returns or creates a new cache instance for that named instance.This is particularly applicable to cases where a plugin has sub-plugins (e.g. gatsby-transformer-remark). In this scenario, we have a cache mismatch, essentially the cache provided by Gatsby is named to the plugins name, whereas the cache provided by gatsby-transformer-remark is named
gatsby-transformer-remark
This PR fixes this imbalance. Few questions/comments:
cache: getCache(plugin.name)
for sub plugins. We can still inject getCache so if the parent cache is needed (rarely?) this will still function, as expected