gatsby: [1.0] Cannot read property 'component' of undefined in production-app.js

In a production build, when I reload the page and then navigate away to another I get this error.

production-app.js:147 Uncaught TypeError: Cannot read property 'component' of undefined
    at renderPage (production-app.js:147)
    at render (production-app.js:182)

Line 147 then points to return $(scriptsCache[page.path].component, (0, _extends3.default)({}, props, scriptsCache[page.path].pageData)); in this piece of code:

var renderPage = function renderPage(props) {
  var page = _pages2.default.find(function (page) {
    if (page.matchPath) {
      // Try both the path and matchPath
      return (0, _reactRouterDom.matchPath)(props.location.pathname, { path: page.path }) || (0, _reactRouterDom.matchPath)(props.location.pathname, {
        path: page.matchPath
      });
    } else {
      return (0, _reactRouterDom.matchPath)(props.location.pathname, {
        path: page.path,
        exact: true
      });
    }
  });
  if (page) {
    return $(scriptsCache[page.path].component, (0, _extends3.default)({}, props, scriptsCache[page.path].pageData));
  } else if (notFoundScripts) {
    return $(notFoundScripts.component, (0, _extends3.default)({}, props, notFoundScripts.pageData));
  } else {
    return null;
  }
};

This issue seems to come from the fact that I’m trying to use urls without a trailing slash. I have this in my gatsby-browser.js file to make that possible

exports.modifyRoutes = routes => {
  routes.childRoutes.forEach(
    route => (route.path = route.path.replace(/\/$/, ''))
  )
  return routes
}

It appears that the page.path needs to be sanitized before using it to look up the cash. I will see if I can fix this in a PR.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 25 (22 by maintainers)

Most upvoted comments

@0x80 yeah this definitely is needed.

I’m going to do a deep dive on this next week after the first beta/tutorial MVP is out but would love any help you could give in the meantime.

Oh yeah sorry, that was an api in v0.

Lemme write up a quick docs page on modifying routes. Am actually working on that right now anyways 😃