diff --git a/e2e-tests/development-runtime/content/2018-12-14-hello-world.md b/e2e-tests/development-runtime/content/2018-12-14-hello-world.md
index 7598fd3256790..bdedb261138b7 100644
--- a/e2e-tests/development-runtime/content/2018-12-14-hello-world.md
+++ b/e2e-tests/development-runtime/content/2018-12-14-hello-world.md
@@ -6,3 +6,5 @@ date: 2018-12-14
This is a truly meaningful blog post
%SUB_TITLE%
+
+%SUBCACHE_VALUE%
diff --git a/e2e-tests/development-runtime/cypress/integration/functionality/sub-plugin-caching.js b/e2e-tests/development-runtime/cypress/integration/functionality/sub-plugin-caching.js
new file mode 100644
index 0000000000000..f45df00a8a5f3
--- /dev/null
+++ b/e2e-tests/development-runtime/cypress/integration/functionality/sub-plugin-caching.js
@@ -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`)
+ })
+})
diff --git a/e2e-tests/development-runtime/gatsby-config.js b/e2e-tests/development-runtime/gatsby-config.js
index e229721485cea..07d6209963039 100644
--- a/e2e-tests/development-runtime/gatsby-config.js
+++ b/e2e-tests/development-runtime/gatsby-config.js
@@ -24,7 +24,7 @@ module.exports = {
{
resolve: `gatsby-transformer-remark`,
options: {
- plugins: [],
+ plugins: [`gatsby-remark-subcache`],
},
},
`gatsby-plugin-sharp`,
diff --git a/e2e-tests/development-runtime/package.json b/e2e-tests/development-runtime/package.json
index f3dc3285df795..c2a65599ab1f8 100644
--- a/e2e-tests/development-runtime/package.json
+++ b/e2e-tests/development-runtime/package.json
@@ -4,7 +4,7 @@
"version": "1.0.0",
"author": "Dustin Schau ",
"dependencies": {
- "gatsby": "^2.0.71",
+ "gatsby": "^2.0.88",
"gatsby-image": "^2.0.20",
"gatsby-plugin-manifest": "^2.0.9",
"gatsby-plugin-offline": "^2.0.20",
@@ -29,7 +29,7 @@
"serve": "gatsby serve",
"start": "npm run develop",
"format": "prettier --write \"src/**/*.js\"",
- "test": "npm run start-server-and-test || npm run reset",
+ "test": "npm run start-server-and-test || (npm run reset && exit 1)",
"posttest": "npm run reset",
"reset": "node scripts/reset.js",
"update": "node scripts/update.js",
diff --git a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/constants.js b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/constants.js
new file mode 100644
index 0000000000000..426ce562fdafa
--- /dev/null
+++ b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/constants.js
@@ -0,0 +1 @@
+exports.id = `gatsby-remark-subcache-value`
diff --git a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/gatsby-node.js b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/gatsby-node.js
new file mode 100644
index 0000000000000..0c7d3f4524705
--- /dev/null
+++ b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/gatsby-node.js
@@ -0,0 +1,5 @@
+const { id } = require(`./constants`)
+
+exports.onPreBootstrap = async ({ cache }) => {
+ await cache.set(id, `Hello World`)
+}
diff --git a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js
new file mode 100644
index 0000000000000..57a1f51e598e9
--- /dev/null
+++ b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js
@@ -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(/%SUBCACHE_VALUE%/, value)
+ }
+ })
+}
diff --git a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/package.json b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/package.json
new file mode 100644
index 0000000000000..58b61fbeea21a
--- /dev/null
+++ b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/package.json
@@ -0,0 +1,3 @@
+{
+ "name": "gatsby-remark-subcache"
+}
diff --git a/packages/gatsby-transformer-remark/package.json b/packages/gatsby-transformer-remark/package.json
index 82118e15fcbac..bff71cea1590b 100644
--- a/packages/gatsby-transformer-remark/package.json
+++ b/packages/gatsby-transformer-remark/package.json
@@ -42,13 +42,12 @@
],
"license": "MIT",
"peerDependencies": {
- "gatsby": "^2.0.33"
+ "gatsby": "^2.0.88"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-remark",
"scripts": {
"build": "babel src --out-dir . --ignore **/__tests__",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore **/__tests__"
- },
- "gitHead": "5bd5aebe066b9875354a81a4b9ed98722731c465"
+ }
}
diff --git a/packages/gatsby-transformer-remark/src/extend-node-type.js b/packages/gatsby-transformer-remark/src/extend-node-type.js
index c6cf553f7b61f..c4054ba1722aa 100644
--- a/packages/gatsby-transformer-remark/src/extend-node-type.js
+++ b/packages/gatsby-transformer-remark/src/extend-node-type.js
@@ -58,6 +58,14 @@ const tableOfContentsCacheKey = node =>
const withPathPrefix = (url, pathPrefix) =>
(pathPrefix + url).replace(/\/\//, `/`)
+// TODO: remove this check with next major release
+const safeGetCache = ({ getCache, cache }) => id => {
+ if (!getCache) {
+ return cache
+ }
+ return getCache(id)
+}
+
/**
* Map that keeps track of generation of AST to not generate it multiple
* times in parallel.
@@ -67,7 +75,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`) {
@@ -76,6 +93,8 @@ module.exports = (
pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``)
pathPrefixCacheStr = pathPrefix || ``
+ const getCache = safeGetCache({ cache, getCache: possibleGetCache })
+
return new Promise((resolve, reject) => {
// Setup Remark.
const {
@@ -150,7 +169,9 @@ module.exports = (
files: fileNodes,
getNode,
reporter,
- cache,
+ cache: getCache(plugin.name),
+ getCache,
+ ...rest,
},
plugin.pluginOptions
)
@@ -218,7 +239,9 @@ module.exports = (
files: fileNodes,
pathPrefix,
reporter,
- cache,
+ cache: getCache(plugin.name),
+ getCache,
+ ...rest,
},
plugin.pluginOptions
)
diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js
index 102557ca1f8e8..5b492e29f3c85 100644
--- a/packages/gatsby-transformer-remark/src/on-node-create.js
+++ b/packages/gatsby-transformer-remark/src/on-node-create.js
@@ -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