Skip to content

Commit

Permalink
Merge pull request #103 from yummy-recipes/defer-algoliaclient
Browse files Browse the repository at this point in the history
Defer algoliaclient
  • Loading branch information
ertrzyiks authored Jan 5, 2025
2 parents 3dc9491 + 5fe2afb commit 14b22f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ jobs:

- name: Build
run: yarn build
env:
ALGOLIA_SEARCH_KEY: dummy-key

deploy:
needs:
Expand Down
15 changes: 14 additions & 1 deletion src/lib/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ import { PrismaClient } from "@prisma/client";
import { algoliasearch } from "algoliasearch";

const prisma = new PrismaClient();
const client = algoliasearch("J8YFF4CZ4C", process.env.ALGOLIA_SEARCH_KEY);
const indexName = process.env.ALGOLIA_SEARCH_INDEX || "prod_recipes";

let client: ReturnType<typeof getNewClient> | null = null;

const getNewClient = () =>
algoliasearch("J8YFF4CZ4C", process.env.ALGOLIA_SEARCH_KEY);

const getClient = () => {
if (!client) {
client = getNewClient();
}
return client;
};

export async function search(query: string) {
const client = getClient();

const { hits } = await client.searchSingleIndex<{ slug: string }>({
indexName,
searchParams: { query },
Expand Down

0 comments on commit 14b22f7

Please sign in to comment.