import React from "react" import PropTypes from "prop-types" import { graphql, Link } from "gatsby" class Dev404Page extends React.Component { static propTypes = { data: PropTypes.object, custom404: PropTypes.element, location: PropTypes.object, } constructor(props) { super(props) const { data } = this.props const pagePaths = data.allSitePage.nodes.map(node => node.path) this.state = { showCustom404: false, initPagePaths: pagePaths, pagePaths: pagePaths, pagePathSearchTerms: ``, } this.showCustom404 = this.showCustom404.bind(this) this.handlePagePathSearch = this.handlePagePathSearch.bind(this) this.handleSearchTermChange = this.handleSearchTermChange.bind(this) } showCustom404() { this.setState({ showCustom404: true }) } handleSearchTermChange(event) { this.setState({ pagePathSearchTerms: event.target.value, }) } handlePagePathSearch(event) { event.preventDefault() const tempPagePaths = [...this.state.initPagePaths] const searchTerm = new RegExp(`${this.state.pagePathSearchTerms}`) this.setState({ pagePaths: tempPagePaths.filter(pagePath => searchTerm.test(pagePath)), }) } render() { const { pathname } = this.props.location let newFilePath if (pathname === `/`) { newFilePath = `src/pages/index.js` } else if (pathname.slice(-1) === `/`) { newFilePath = `src/pages${pathname.slice(0, -1)}.js` } else { newFilePath = `src/pages${pathname}.js` } return this.state.showCustom404 ? ( this.props.custom404 ) : (

Gatsby.js development 404 page

{`There's not a page yet at `} {pathname}

{this.props.custom404 ? (

) : (

{`A custom 404 page wasn't detected - if you would like to add one, create a component in your site directory at `} src/pages/404.js.

)}

Create a React.js component in your site directory at {` `} {newFilePath} {` `} and this page will automatically refresh to show the new page component you created.

{this.state.initPagePaths.length > 0 && (

If you were trying to reach another page, perhaps you can find it below.

Pages ( {this.state.pagePaths.length != this.state.initPagePaths.length ? `${this.state.pagePaths.length}/${ this.state.initPagePaths.length }` : this.state.initPagePaths.length} )

)}
) } } export default Dev404Page export const pagesQuery = graphql` query PagesQuery { allSitePage(filter: { path: { ne: "/dev-404-page/" } }) { nodes { path } } } `