Ejemplo n.º 1
0
  pages.some(page => {
    let pathToMatch = page.matchPath ? page.matchPath : page.path
    if (matchPath(pathToMatch, trimmedPathname)) {
      foundPage = page
      pageCache[trimmedPathname] = page
      return true
    }

    // Finally, try and match request with default document.
    if (matchPath(`${page.path}index.html`, trimmedPathname)) {
      foundPage = page
      pageCache[trimmedPathname] = page
      return true
    }

    return false
  })
Ejemplo n.º 2
0
apiRunnerAsync(`onClientEntry`).then(() => {
  // Let plugins register a service worker. The plugin just needs
  // to return true.
  if (apiRunner(`registerServiceWorker`).length > 0) {
    require(`./register-service-worker`)
  }

  class RouteHandler extends React.Component {
    render() {
      let { location } = this.props
      // TODO
      // check if hash + if element and if so scroll
      // remove hash handling from gatsby-link
      // check if scrollbehavior handles back button for
      // restoring old position
      // if not, add that.

      return (
        <ScrollContext
          location={location}
          shouldUpdateScroll={shouldUpdateScroll}
        >
          <EnsureResources location={location}>
            {({ pageResources, location }) => (
              <PageRenderer
                key={location.pathname}
                {...this.props}
                location={location}
                pageResources={pageResources}
                {...pageResources.json}
                isMain
              />
            )}
          </EnsureResources>
        </ScrollContext>
      )
    }
  }

  const { page, location: browserLoc } = window
  // TODO: comment what this check does
  if (
    page &&
    page.path !== `/404.html` &&
    __PATH_PREFIX__ + page.path !== browserLoc.pathname &&
    !page.path.match(/^\/offline-plugin-app-shell-fallback\/?$/) &&
    (!page.matchPath ||
      !match(__PATH_PREFIX__ + page.matchPath, browserLoc.pathname))
  ) {
    navigate(
      __PATH_PREFIX__ + page.path + browserLoc.search + browserLoc.hash,
      { replace: true }
    )
  }

  loader
    .getResourcesForPathname(browserLoc.pathname)
    .then(() => {
      if (!loader.getPage(browserLoc.pathname)) {
        return loader
          .getResourcesForPathname(`/404.html`)
          .then(resources =>
            loadDirectlyOr404(
              resources,
              browserLoc.pathname + browserLoc.search + browserLoc.hash,
              true
            )
          )
      }
      return null
    })
    .then(() => {
      const Root = () =>
        createElement(
          Router,
          {
            basepath: __PATH_PREFIX__,
          },
          createElement(RouteHandler, { path: `/*` })
        )

      const WrappedRoot = apiRunner(
        `wrapRootElement`,
        { element: <Root /> },
        <Root />,
        ({ result }) => {
          return { element: result }
        }
      ).pop()

      let NewRoot = () => WrappedRoot

      const renderer = apiRunner(
        `replaceHydrateFunction`,
        undefined,
        ReactDOM.hydrate
      )[0]

      domReady(() => {
        renderer(
          <NewRoot />,
          typeof window !== `undefined`
            ? document.getElementById(`___gatsby`)
            : void 0,
          () => {
            apiRunner(`onInitialClientRender`)
          }
        )
      })
    })
})