Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
fix(search): 🐛 fix issue with performance not avaialble during SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
filipowm committed Aug 12, 2020
1 parent e7dd75f commit cd4eaa8
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/components/Search/localsearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import { StaticQuery, graphql } from 'gatsby';

const Results = ({ q }) => <SearchStatus noHits={true} searching={false} query={q} />;

const getPerformance = () => {
if (typeof window !== `undefined` && window.performance) {
return window.performance;
}
return {
now: () => new Date().getMilliseconds()
}
}

const calculatePage = (results, page) => {
const hitsPerPage = config.features.search.hitsPerPage;
const startIdx = hitsPerPage * page - hitsPerPage;
Expand All @@ -19,8 +28,13 @@ const calculatePage = (results, page) => {
};

const search = (query, index, store, page) => {
const performance = getPerformance();
const t1 = performance.now();
const results = useFlexSearch(query, index, JSON.parse(store));
let results = [];
if (index && store) {
const parsedStore = typeof store === `string` ? JSON.parse(store) : store;
results = useFlexSearch(query, index, parsedStore);
}
const maxResults =
config.features.search.pagination.totalPages * config.features.search.hitsPerPage;
const nbHits = results.length > maxResults ? maxResults : results.length;
Expand Down

0 comments on commit cd4eaa8

Please sign in to comment.