Theo's Typesafe CultTTC
Theo's Typesafe Cult2y ago
23 replies
lelabo

Handling Third-party lib client component that relies on ENV vars.

Hi,

I am currently trying to add search on my project, and I stubbled upon a common scenario that I don't know how to handle yet...

I am trying to use Meilisearch with react-instantsearch library.
I have this snippet of code, from the doc:

"use client";

import { instantMeiliSearch } from "@meilisearch/instant-meilisearch";
import { InfiniteHits, InstantSearch, SearchBox } from "react-instantsearch";
import { env } from "~/env";

export const { searchClient } = instantMeiliSearch(
  env.NEXT_PUBLIC_SEARCH_API_URL,
  env.NEXT_PUBLIC_SEARCH_API_KEY,
);

export default function InstantSearchComponent() {

  return (
    // @ts-ignore
    <InstantSearch indexName="steam-videogames" searchClient={searchClient}>
      {/* @ts-ignore */}
      <SearchBox />
      {/* @ts-ignore */}
      <InfiniteHits hitComponent={Hit} />
    </InstantSearch>
  );
}

function Hit({
  hit,
}: {
  hit: {
    id: string;
    image: string;
    name: string;
    description: string;
  };
}) {
  return (
    <article key={hit.id}>
      <img src={hit.image} alt={hit.name} />
      <h1>{hit.name}</h1>
      <p>${hit.description}</p>
    </article>
  );
}


- I could do this... but I prefer avoiding sensitive keys on client if possible.
- I can create the searchClient props on the server, but I can't pass it to a client component.
- I can't use the InstantSearch component server-side.

How am I supposed to handle this type of Third-party problems where I have to create things client-side that depends upon sensitive keys?
Was this page helpful?