I’m trying to figure out the most appropriate fetching pattern for a multi-tenant prop tech app I’m building.
Goal
- Something similar to the filters on Zillow’s property search page
Conditions
- Next 13 with app router
- Filters are set in state in a client component
- Query params are built based off of the filter selections
- Those query params are then appended to an external API endpoint that will then send me back results that match the filters.
Some gotchas
- I’d like to avoid fetching in the client component if I can. In order to be authenticated I have to pass a private API key for the fetch. Seems maybe a bit insecure to pass that into a client component. These api key must exist in my db and can’t be env vars. Based on this fact, I have not tried the SWR/React Query way to fetch inside the client component.
- I figured my next option would be to ditch the local state and create context for this. Got it working (took a couple hours) only to find out that I can’t seem to access context in a sever component:
Cannot access ._context on the server. You cannot dot into a client module from a server component. You can only pass the imported name through.
**Thoughts **
- Some have suggested to just try to do a big fetch of all of the data and just do the actual filtering client side. I get it. I can see the pros. There are drawbacks too. I’ve had a few discussions with the creator of the external API I’m using and he highly recommended using their filter. A lot of his clients assume they will get a performance increase by doing a big fetch then filtering client side but he’s adamant that there is zero performance increase. I would like to at least try using their filter before recreating parts of it myself.
I’m a bit stumped here!
Happy to provide code but might just be worth talking high level first.