From d207f68e437f89451471a62e145d274fbd5fed10 Mon Sep 17 00:00:00 2001 From: ybbarng Date: Mon, 30 Oct 2017 22:50:27 +0900 Subject: [PATCH] Fixes a crash when a post has no tag This commit avoids undefined.map() or null.map() is calling. When there is no tag in a post, there are two cases in frontmatter. 1) title: title categories: Technical ... 2) title: title tags: categories: Technical ... Let's look at `post.node.frontmatter.tags` in both situations. `FILE1 = /gatsby-node.js` `FILE2 = /src/components/PostTemplateDetails/index.jsx` In the first case, it is `undefined` in `FILE1`. Cheking `undefined` is already implemented at FILE1#126, so undefined.map() is not called. But it is `null` in `FILE2#22` and a crash will be occurred because it tries to run `null.map()`. In the second case, it is `null` in `FILE1` and `FILE2`. In this case, there will be two crashes in both files because both codes try to run `null.map()`. This commit avoids these crashes. --- gatsby-node.js | 2 +- src/components/PostTemplateDetails/index.jsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index b4c6f39..cb5c1a9 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -123,7 +123,7 @@ exports.onCreateNode = ({ node, boundActionCreators, getNode }) => { value: slug }); - if (typeof node.frontmatter.tags !== 'undefined') { + if (node.frontmatter.tags) { const tagSlugs = node.frontmatter.tags.map( tag => `/tags/${_.kebabCase(tag)}/` ); diff --git a/src/components/PostTemplateDetails/index.jsx b/src/components/PostTemplateDetails/index.jsx index 6620951..44192eb 100644 --- a/src/components/PostTemplateDetails/index.jsx +++ b/src/components/PostTemplateDetails/index.jsx @@ -19,7 +19,7 @@ class PostTemplateDetails extends React.Component { const tagsBlock = (