Skip to content

Commit

Permalink
perf(website): add ttl cache for algolia ssr to reduce requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito committed Dec 11, 2023
1 parent 4a8fcfc commit bb918dc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 26 additions & 9 deletions website/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { Filters } from '@/components/search/Filters';
import { InfiniteHits } from '@/components/search/Hits';
import classes from '@/styles/global.module.css';
import { getSSRCache, setSSRCache } from '@/utils/algolia.server';

import { theme } from '../styles/theme';

Expand Down Expand Up @@ -102,22 +103,38 @@ const routing = (serverUrl: string): any => {

export const loader = async ({ request }: LoaderFunctionArgs) => {
const serverUrl = request.url;
const serverState = await getServerState(

// Check local cache for server state first to avoid unnecessary API calls
let serverState = getSSRCache(serverUrl);
if (serverState) {
return json<SearchProps>({
serverState,
serverUrl,
});
}

serverState = await getServerState(
<MantineProvider theme={theme}>
<InstantSearch
searchClient={searchClient}
indexName="prod_POPULAR"
routing={routing(serverUrl)}
future={{ preserveSharedStateOnUnmount: true }}
>
<Filters />
</InstantSearch>
<InstantSearchSSRProvider>
<InstantSearch
searchClient={searchClient}
indexName="prod_POPULAR"
routing={routing(serverUrl)}
future={{ preserveSharedStateOnUnmount: true }}
>
<Filters />
<InfiniteHits />
</InstantSearch>
</InstantSearchSSRProvider>
</MantineProvider>,
{
renderToString,
},
);

// Add server state to local cache before responding
setSSRCache(serverUrl, serverState);

return json<SearchProps>({
serverState,
serverUrl,
Expand Down
24 changes: 20 additions & 4 deletions website/app/utils/algolia.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import TTLCache from '@isaacs/ttlcache';
import algoliasearch from 'algoliasearch';
import { type InstantSearchServerState } from 'react-instantsearch';

import {
getFontlist,
Expand All @@ -7,6 +9,23 @@ import {
} from '@/utils/metadata.server';
import type { AlgoliaMetadata } from '@/utils/types';

// Cache for Algolia SSR state to avoid re-fetching on every request
const ALGOLIA_TTL = 6 * 60 * 60 * 1000; // 6 hours
const ssrCache = new TTLCache({ ttl: ALGOLIA_TTL });

const getSSRCache = (
serverUrl: string,
): InstantSearchServerState | undefined => {
return ssrCache.get(serverUrl);
};

const setSSRCache = (serverUrl: string, state: InstantSearchServerState) => {
ssrCache.set(serverUrl, state);
};

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const client = algoliasearch('WNATE69PVR', process.env.ALGOLIA_ADMIN_KEY!);

const shuffleArray = (size: number) => {
// Generate array of numbers from 0 to size
const arr: number[] = [...Array.from({ length: size }).keys()];
Expand All @@ -19,9 +38,6 @@ const shuffleArray = (size: number) => {
return arr;
};

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const client = algoliasearch('WNATE69PVR', process.env.ALGOLIA_ADMIN_KEY!);

const updateAlgoliaIndex = async (force?: boolean) => {
try {
// Get font list
Expand Down Expand Up @@ -77,4 +93,4 @@ const updateAlgoliaIndex = async (force?: boolean) => {
}
};

export { updateAlgoliaIndex };
export { getSSRCache, setSSRCache, updateAlgoliaIndex };
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@fontsource-utils/generate": "^0.3.1",
"@fontsource-variable/inter": "^5.0.16",
"@fontsource-variable/source-code-pro": "^5.0.17",
"@isaacs/ttlcache": "^1.4.1",
"@legendapp/state": "^1.11.3",
"@mantine/core": "^7.3.1",
"@mantine/hooks": "^7.3.1",
Expand Down

0 comments on commit bb918dc

Please sign in to comment.