Skip to content

Commit

Permalink
404 page: more bugfixes, sensible fallbacks for noscript
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-heyer committed Sep 29, 2023
1 parent ce2f22b commit bd809d4
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/pages/404.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import React from "react";
import React, { useEffect, useMemo, useState } from "react";
import { Container } from "react-bootstrap";
import algoliasearch from "algoliasearch/lite";
import {
InstantSearch,
Configure,
Hits,
useSearchBox,
useInstantSearch,
useRefinementList,
} from "react-instantsearch";
import { Footer, Layout, Link, MainContent } from "../components";
import Icon, { iconNames } from "../components/icon";
Expand Down Expand Up @@ -54,7 +52,8 @@ const buildQuery = (pathname, pathPrefix) => {

const cleanRegex = /-|\//g;

if (pathname.startsWith(pathPrefix)) pathname.replace(pathPrefix, "");
if (pathname.startsWith(pathPrefix))
pathname = pathname.replace(pathPrefix, "");
const segments = pathname.split("/");
if (products[segments[1]]) {
return {
Expand Down Expand Up @@ -92,13 +91,21 @@ const SuggestedLinksSearch = ({ queryParams }) => {
initialUiState={{ [algoliaIndex]: queryParams }}
>
<SuggestedLinks />
<Configure
hitsPerPage={5}
query={queryParams.query}
filters={
queryParams.refinementList?.product?.length &&
`product:"${queryParams.refinementList?.product[0]}"`
}
/>
<div>
Not finding what you need?
<Link
to={`/search?query=${encodeURIComponent(
queryParams.query,
)}&product=${encodeURIComponent(
queryParams.refinementList?.product?.join(","),
queryParams.refinementList?.product?.join(",") || "",
)}`}
className="ms-2"
>
Expand All @@ -111,16 +118,13 @@ const SuggestedLinksSearch = ({ queryParams }) => {

const SuggestedLinks = () => {
const { results: searchResults } = useInstantSearch();
useSearchBox(); // ensures query is actually sent
useRefinementList({ attribute: "product" }); // ensures product refinement is actually sent

return (
<>
{searchResults && searchResults.nbHits > 0 && (
<>
<div>Suggested links based on the requested URL:</div>
<div className="search-content mb-5 mt-3">
<Configure hitsPerPage={5} />
<Hits hitComponent={SuggestedHit} />
</div>
</>
Expand All @@ -136,9 +140,17 @@ const SuggestedHit = ({ hit }) => (
</Link>
);

const NotFound = (data) => {
const NotFound = ({ location: { pathname, href } }) => {
const pathPrefix = usePathPrefix();
const queryParams = buildQuery(data.location.pathname, pathPrefix);
// these are NEVER going to be useful on prerender, so trigger an update every time
const [location, setLocation] = useState({ href: "", pathname: "" });
useEffect(() => {
setLocation({ href, pathname });
}, [href, pathname]);
const queryParams = useMemo(
() => buildQuery(location.pathname, pathPrefix),
[location, pathPrefix],
);

return (
<Layout pageMeta={{ title: "Page Not Found" }}>
Expand All @@ -147,7 +159,7 @@ const NotFound = (data) => {
<div className="d-flex align-items-center flex-column">
<Ascii404 />
<div>
<PageNotFound path={data.location.href} />
<PageNotFound path={location.href} />
<SuggestedLinksSearch queryParams={queryParams} />
</div>
</div>
Expand Down

1 comment on commit bd809d4

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.