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

fix(gatsby-transformer-remark): correctly pass cache to sub plugins #10892

Merged
merged 8 commits into from
Jan 8, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ date: 2018-12-14
This is a truly meaningful blog post

<h2 data-testid="sub-title">%SUB_TITLE%</h2>

<h2 data-testid="gatsby-remark-subcache-value"></h2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* This e2e test validates that the cache structure
* is unique per plugin (even sub-plugins)
* and can interact between Gatsby lifecycles and a plugin
*/
describe(`sub-plugin caching`, () => {
beforeEach(() => {
cy.visit(`/2018-12-14-hello-world/`).waitForAPI(`onRouteUpdate`)
})

it(`has access to custom sub-plugin cache`, () => {
cy.getTestElement(`gatsby-remark-subcache-value`)
.invoke(`text`)
.should(`eq`, `Hello World`)
})
})
2 changes: 1 addition & 1 deletion e2e-tests/development-runtime/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [],
plugins: [`gatsby-remark-subcache`],
},
},
`gatsby-plugin-sharp`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.id = `gatsby-remark-subcache-value`
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { id } = require(`./constants`)

exports.onPreBootstrap = async ({ cache }) => {
await cache.set(id, `Hello World`)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const visit = require(`unist-util-visit`)
const { id } = require(`./constants`)

module.exports = function remarkPlugin({ cache, markdownAST }) {
visit(markdownAST, `html`, async node => {
if (node.value.match(id)) {
const value = await cache.get(id)
node.value = node.value.replace(/></, () => `>${value}<`)
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "gatsby-remark-subcache"
}
41 changes: 38 additions & 3 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ const tableOfContentsCacheKey = node =>
const withPathPrefix = (url, pathPrefix) =>
(pathPrefix + url).replace(/\/\//, `/`)

// TODO: remove this check with next major release
// the getCache feature was implemented in gatsby@2.0.88
const safeGetCache = ({ getCache, cache, reporter }) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pieh this warning looks like:

success onPreBootstrap — 0.004 s
success source and transform nodes — 0.072 s
success building schema — 0.175 s
success createPages — 0.030 s
success createPagesStatefully — 0.021 s
success onPreExtractQueries — 0.003 s
success update schema — 0.102 s
success extract queries from components — 0.119 s
warning It looks like you're using a slightly outdated version of gatsby with gatsby-transformer-remark.
To update, run 'npm install gatsby@^2.0.86 --save' or 'yarn add gatsby@^2.0.86'

I thought about using onPreBootstrap but I like how we can remove this code all in one spot (easier refactoring). Up for whatever though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it'd be cool to think more broadly about how we introduce non-backwards compatible API changes, and if we could improve this.

For now this will work fine.

const GATSBY_VERSION_WITH_GET_CACHE = `2.0.88`
let warned = false
return id => {
if (!getCache) {
if (!warned) {
warned = true
reporter.warn(reporter.stripIndent`
It looks like you're using a slightly outdated version of gatsby with gatsby-transformer-remark.
To update, run 'npm install gatsby@^${GATSBY_VERSION_WITH_GET_CACHE} --save' or 'yarn add gatsby@^${GATSBY_VERSION_WITH_GET_CACHE}'
`)
}
return cache
}
return getCache(id)
}
}

/**
* Map that keeps track of generation of AST to not generate it multiple
* times in parallel.
Expand All @@ -67,7 +87,16 @@ const withPathPrefix = (url, pathPrefix) =>
const ASTPromiseMap = new Map()

module.exports = (
{ type, store, pathPrefix, getNode, getNodesByType, cache, reporter },
{
type,
pathPrefix,
getNode,
getNodesByType,
cache,
getCache: possibleGetCache,
reporter,
...rest
},
pluginOptions
) => {
if (type.name !== `MarkdownRemark`) {
Expand All @@ -76,6 +105,8 @@ module.exports = (
pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``)
pathPrefixCacheStr = pathPrefix || ``

const getCache = safeGetCache({ cache, getCache: possibleGetCache, reporter })

return new Promise((resolve, reject) => {
// Setup Remark.
const {
Expand Down Expand Up @@ -150,7 +181,9 @@ module.exports = (
files: fileNodes,
getNode,
reporter,
cache,
cache: getCache(plugin.name),
getCache,
...rest,
},
plugin.pluginOptions
)
Expand Down Expand Up @@ -218,7 +251,9 @@ module.exports = (
files: fileNodes,
pathPrefix,
reporter,
cache,
cache: getCache(plugin.name),
getCache,
...rest,
},
plugin.pluginOptions
)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const crypto = require(`crypto`)
const _ = require(`lodash`)

module.exports = async function onCreateNode(
{ node, getNode, loadNodeContent, actions, createNodeId, reporter },
{ node, loadNodeContent, actions, createNodeId, reporter },
pluginOptions
) {
const { createNode, createParentChildLink } = actions
Expand Down