From 7d12082d8b57b5317d28986c382b251950914587 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Wed, 3 Oct 2018 17:02:44 -0500 Subject: [PATCH] fix: prevent gatsby file detection from breaking path field (#1226) Fixes #1088 This should fix the path issues that people intermittently run into. Gatsby sees the relative path (and the parent node with an absolute path), joins on the parent path and the relative path, and presumes it's a file... which is normally good, but which here is not what we want beacuse then we're getting a resolver of type file instead of type string referring to the relative path of the document. This seems to fix a (long standing?) bug where the "Edit this Page" button is broken, as well. e.g. see [this page](https://reactjs.org/community/support.html) which _should_ have an Edit this Page button I'd need to think more as to whether there's a cleaner fix here, but this seems to work pretty well! --- gatsby/onCreateNode.js | 7 +++++-- src/components/MarkdownPage/MarkdownPage.js | 2 +- src/pages/docs/error-decoder.html.js | 4 +--- src/templates/blog.js | 4 +--- src/templates/community.js | 4 +--- src/templates/docs.js | 4 +--- src/templates/tutorial.js | 4 +--- 7 files changed, 11 insertions(+), 18 deletions(-) diff --git a/gatsby/onCreateNode.js b/gatsby/onCreateNode.js index 1c08e5b6f..0e6e9934d 100644 --- a/gatsby/onCreateNode.js +++ b/gatsby/onCreateNode.js @@ -6,6 +6,8 @@ 'use strict'; +const path = require('path'); + // Parse date information out of blog post filename. const BLOG_POST_FILENAME_REGEX = /([0-9]+)\-([0-9]+)\-([0-9]+)\-(.+)\.md$/; @@ -30,7 +32,7 @@ module.exports = exports.onCreateNode = ({node, actions, getNode}) => { switch (node.internal.type) { case 'MarkdownRemark': const {permalink, redirect_from} = node.frontmatter; - const {relativePath} = getNode(node.parent); + const {relativePath, sourceInstanceName} = getNode(node.parent); let slug = permalink; @@ -71,10 +73,11 @@ module.exports = exports.onCreateNode = ({node, actions, getNode}) => { }); // Used to generate a GitHub edit link. + // this presumes that the name in gastby-config.js refers to parent folder createNodeField({ node, name: 'path', - value: relativePath, + value: path.join(sourceInstanceName, relativePath), }); // Used by createPages() above to register redirects. diff --git a/src/components/MarkdownPage/MarkdownPage.js b/src/components/MarkdownPage/MarkdownPage.js index 9f7d3a4bb..9214552bb 100644 --- a/src/components/MarkdownPage/MarkdownPage.js +++ b/src/components/MarkdownPage/MarkdownPage.js @@ -112,7 +112,7 @@ const MarkdownPage = ({
Edit this page diff --git a/src/pages/docs/error-decoder.html.js b/src/pages/docs/error-decoder.html.js index 3b24c815a..e51abe51d 100644 --- a/src/pages/docs/error-decoder.html.js +++ b/src/pages/docs/error-decoder.html.js @@ -107,9 +107,7 @@ export const pageQuery = graphql` markdownRemark(fields: {slug: {eq: $slug}}) { html fields { - path { - id - } + path } frontmatter { title diff --git a/src/templates/blog.js b/src/templates/blog.js index 5f9a0cd91..57967afcc 100644 --- a/src/templates/blog.js +++ b/src/templates/blog.js @@ -58,9 +58,7 @@ export const pageQuery = graphql` } fields { date(formatString: "MMMM DD, YYYY") - path { - id - } + path slug } } diff --git a/src/templates/community.js b/src/templates/community.js index b388bc99b..05f7ca910 100644 --- a/src/templates/community.js +++ b/src/templates/community.js @@ -33,9 +33,7 @@ export const pageQuery = graphql` prev } fields { - path { - id - } + path slug } } diff --git a/src/templates/docs.js b/src/templates/docs.js index 8f686af6c..b874201d7 100644 --- a/src/templates/docs.js +++ b/src/templates/docs.js @@ -38,9 +38,7 @@ export const pageQuery = graphql` prev } fields { - path { - id - } + path slug } } diff --git a/src/templates/tutorial.js b/src/templates/tutorial.js index 26ea2dd30..79a4b2c8c 100644 --- a/src/templates/tutorial.js +++ b/src/templates/tutorial.js @@ -36,9 +36,7 @@ export const pageQuery = graphql` prev } fields { - path { - id - } + path slug } }