<commit_before>import React, { createElement } from "react"
import PropTypes from "prop-types"
import loader, { publicLoader } from "./loader"
import emitter from "./emitter"
import { apiRunner } from "./api-runner-browser"

const DefaultLayout = ({ children }) => <div>{children()}</div>

// Pass pathname in as prop.
// component will try fetching resources. If they exist,
// will just render, else will render null.
class ComponentRenderer extends React.Component {
  constructor(props) {
    super()
    let location = props.location

    // This covers layout for when page not found, especially during production
    if (!loader.getPage(location.pathname)) {
      location = Object.assign({}, location, {
        ...location,
        pathname: `/404.html`,
      })
    }

    this.state = {
      location,
      pageResources: loader.getResourcesForPathname(props.location.pathname),
    }
  }

  componentWillReceiveProps(nextProps) {
    // During development, always pass a component's JSON through so graphql
    // updates go through.
    if (process.env.NODE_ENV !== `production`) {
      if (
        nextProps &&
        nextProps.pageResources &&
        nextProps.pageResources.json
      ) {
        this.setState({ pageResources: nextProps.pageResources })
      }
    }
    if (this.state.location.pathname !== nextProps.location.pathname) {
      const pageResources = loader.getResourcesForPathname(
        nextProps.location.pathname
      )
      if (!pageResources) {
        // Page resources won't be set in cases where the browser back button
        // or forward button is pushed as we can't wait as normal for resources
        // to load before changing the page.
        loader.getResourcesForPathname(
          nextProps.location.pathname,
          pageResources => {
            this.setState({
              location: nextProps.location,
              pageResources,
            })
          }
        )
      } else {
        this.setState({
          location: nextProps.location,
          pageResources,
        })
      }
    }
  }

  componentDidMount() {
    // Listen to events so when our page gets updated, we can transition.
    // This is only useful on delayed transitions as the page will get rendered
    // without the necessary page resources and then re-render once those come in.
    emitter.on(`onPostLoadPageResources`, e => {
      if (
        loader.getPage(this.state.location.pathname) &&
        e.page.path === loader.getPage(this.state.location.pathname).path
      ) {
        this.setState({ pageResources: e.pageResources })
      }
    })
  }

  shouldComponentUpdate(nextProps, nextState) {<commit_msg>Restore layout for page not found when navigating from other page (#2980)<commit_after>import React, { createElement } from "react"
import PropTypes from "prop-types"
import loader, { publicLoader } from "./loader"
import emitter from "./emitter"
import { apiRunner } from "./api-runner-browser"

const DefaultLayout = ({ children }) => <div>{children()}</div>

// Pass pathname in as prop.
// component will try fetching resources. If they exist,
// will just render, else will render null.
class ComponentRenderer extends React.Component {
  constructor(props) {
    super()
    let location = props.location

    // Set the pathname for 404 pages.
    if (!loader.getPage(location.pathname)) {
      location = Object.assign({}, location, {
        pathname: `/404.html`,
      })
    }

    this.state = {
      location,
      pageResources: loader.getResourcesForPathname(props.location.pathname),
    }
  }

  componentWillReceiveProps(nextProps) {
    // During development, always pass a component's JSON through so graphql
    // updates go through.
    if (process.env.NODE_ENV !== `production`) {
      if (
        nextProps &&
        nextProps.pageResources &&
        nextProps.pageResources.json
      ) {
        this.setState({ pageResources: nextProps.pageResources })
      }
    }
    if (this.state.location.pathname !== nextProps.location.pathname) {
      const pageResources = loader.getResourcesForPathname(
        nextProps.location.pathname
      )
      if (!pageResources) {
        let location = nextProps.location

        // Set the pathname for 404 pages.
        if (!loader.getPage(location.pathname)) {
          location = Object.assign({}, location, {
            pathname: `/404.html`,
          })
        }

        // Page resources won't be set in cases where the browser back button
        // or forward button is pushed as we can't wait as normal for resources
        // to load before changing the page.
        loader.getResourcesForPathname(
          location.pathname,
          pageResources => {
            this.setState({
              location,
              pageResources,
            })
          }
        )
      } else {
        this.setState({
          location: nextProps.location,
          pageResources,
        })
      }
    }
  }

  componentDidMount() {
    // Listen to events so when our page gets updated, we can transition.
    // This is only useful on delayed transitions as the page will get rendered
    // without the necessary page resources and then re-render once those come in.
    emitter.on(`onPostLoadPageResources`, e => {
      if (
        loader.getPage(this.state.location.pathname) &&
        e.page.path === loader.getPage(this.state.location.pathname).path
      ) {
        this.setState({ pageResources: e.pageResources })
      }
    })
  }

  shouldComponentUpdate(nextProps, nextState) {
{'fraction_non_alphanumeric': 0.10233393177737882, 'fraction_numerical': 0.0034111310592459605, 'mean_word_length': 2.3379269023367284, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4035080', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1453, 'n_words': 426}