Skip to content

Commit 7d12082

Browse files
DSchaualexkrolick
authored andcommitted
fix: prevent gatsby file detection from breaking path field (reactjs#1226)
Fixes reactjs#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!
1 parent f2d39be commit 7d12082

File tree

7 files changed

+11
-18
lines changed

7 files changed

+11
-18
lines changed

gatsby/onCreateNode.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
'use strict';
88

9+
const path = require('path');
10+
911
// Parse date information out of blog post filename.
1012
const BLOG_POST_FILENAME_REGEX = /([0-9]+)\-([0-9]+)\-([0-9]+)\-(.+)\.md$/;
1113

@@ -30,7 +32,7 @@ module.exports = exports.onCreateNode = ({node, actions, getNode}) => {
3032
switch (node.internal.type) {
3133
case 'MarkdownRemark':
3234
const {permalink, redirect_from} = node.frontmatter;
33-
const {relativePath} = getNode(node.parent);
35+
const {relativePath, sourceInstanceName} = getNode(node.parent);
3436

3537
let slug = permalink;
3638

@@ -71,10 +73,11 @@ module.exports = exports.onCreateNode = ({node, actions, getNode}) => {
7173
});
7274

7375
// Used to generate a GitHub edit link.
76+
// this presumes that the name in gastby-config.js refers to parent folder
7477
createNodeField({
7578
node,
7679
name: 'path',
77-
value: relativePath,
80+
value: path.join(sourceInstanceName, relativePath),
7881
});
7982

8083
// Used by createPages() above to register redirects.

src/components/MarkdownPage/MarkdownPage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const MarkdownPage = ({
112112
<div css={{marginTop: 80}}>
113113
<a
114114
css={sharedStyles.articleLayout.editLink}
115-
href={`https://github.com/reactjs/reactjs.org/tree/master/content/${
115+
href={`https://github.com/reactjs/reactjs.org/tree/master/${
116116
markdownRemark.fields.path
117117
}`}>
118118
Edit this page

src/pages/docs/error-decoder.html.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ export const pageQuery = graphql`
107107
markdownRemark(fields: {slug: {eq: $slug}}) {
108108
html
109109
fields {
110-
path {
111-
id
112-
}
110+
path
113111
}
114112
frontmatter {
115113
title

src/templates/blog.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ export const pageQuery = graphql`
5858
}
5959
fields {
6060
date(formatString: "MMMM DD, YYYY")
61-
path {
62-
id
63-
}
61+
path
6462
slug
6563
}
6664
}

src/templates/community.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ export const pageQuery = graphql`
3333
prev
3434
}
3535
fields {
36-
path {
37-
id
38-
}
36+
path
3937
slug
4038
}
4139
}

src/templates/docs.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ export const pageQuery = graphql`
3838
prev
3939
}
4040
fields {
41-
path {
42-
id
43-
}
41+
path
4442
slug
4543
}
4644
}

src/templates/tutorial.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ export const pageQuery = graphql`
3636
prev
3737
}
3838
fields {
39-
path {
40-
id
41-
}
39+
path
4240
slug
4341
}
4442
}

0 commit comments

Comments
 (0)