#community-help

Deploying Typesense-instantsearch-adapter in Serverless Context

TLDR Emin ran into problems deploying a webpage using Typesense with CloudFlare due to axios' incompatibility with serverless contexts. Jason suggested a workaround and mentioned that axios might add a fetch-based adapter in the future. Emin agreed to implement the workaround and contribute a PR.

Powered by Struct AI

1

Apr 28, 2022 (18 months ago)
Emin
Photo of md5-8be274e1e38f49505009f42e45fd3f71
Emin
03:30 AM
Hi, I'm trying to deploy a webpage that uses Typesense-instantsearch-adapter to CloudFlare pages (using CF workers to server side render), and while it can fetch results from within the browser, I can't server side render the page since Typesense-js relies on axios which uses node APIs which aren't available in a serverless context. This is essentially the same problem that this thread discusses: https://typesense-community.slack.com/archives/C01UZEXF5FY/p1643168574904559

My problem is that I can't use the typesense-deno fork mentioned in that thread since Typesense-instantsearch-adapter doesn't allow me to pass in a custom searchClient implementation. Now I could use typesense-deno and fork the adapter to use that library instead, but before I do that I was wondering if moving away from axios towards something like cross-fetch is a possibility?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:48 AM
My understanding is that axios works both in node.js and browser envs...
04:49
Jason
04:49 AM
Sounds like there's something specific about the Cloudflare pages env that's preventing axios from working?
04:50
Jason
04:50 AM
In any case, I know some users rely on the ability to customize axios with additional interceptors, etc. So it would be a major backward incompatible change for us to switch http clients
04:54
Jason
04:54 AM
> My problem is that I can't use the typesense-deno fork mentioned in that thread since Typesense-instantsearch-adapter doesn't allow me to pass in a custom searchClient implementation.
Interesting. You mean the typesense-js library right? This should be doable by changing this line to accept a typesenseClient from options if it is specified.

I'd be open to a PR to do that
Emin
Photo of md5-8be274e1e38f49505009f42e45fd3f71
Emin
05:36 AM
Yup, axios works in node.js and browser envs, just not in the serverless env of CloudFlare workers and other serverless platforms. I think they have access to the same serviceWorker environment that PWAs use.

I'll open a PR to be able to override the client, the only problem is that I'd have to maintain an axios-less fork of typesense-js. But according to this it seems like axios is planning to add a fetch-based adapter anyway, so I'll just do it this way for now until they release that. I think I'll be able to get the PR out on Friday 🙂

1