#community-help

Solving An Issue with Typesense-js in Sveltekit Production

TLDR Francois reported an issue where a static value in typesense-js was undefined in Sveltekit's production. Jason helped debug the issue, suspecting a build problem or a circular dependency. Francois's solution eliminated this error and was tested, approved, and finally merged by Jason.

Powered by Struct AI
raised_hands1
timer_clock1
white_check_mark1
27
20mo
Solved
Join the chat
Jan 06, 2022 (20 months ago)
Francois
Photo of md5-cee98684ac28f06242bd964ea7e9eda7
Francois
08:16 PM
👋 Hello community 🙂 We stumbled on a production-only issue with typesense-js and I found out something that fixes it, but I'm not sure I did the right thing 😛 Can someone check this out a bit with me? (see thread for more info)
08:18
Francois
08:18 PM
We are using Sveltekit (which uses Vite) and a default import's static value was returning "undefined", which seemed very odd

The line in question is here, on Documents.RESOURCEPATH : https://github.com/typesense/typesense-js/blob/master/src/Typesense/SearchOnlyDocuments.ts#L38
08:20
Francois
08:20 PM
I have two theories:
1- Documents.RESOURCEPATH was null because Documents extends SearchOnlyDocument and static getter are not properly extended, but then it would seem TypeScript should detect that and fail
2- The circular dependency between ./Documents.ts and ./SearchOnlyDocuments.ts is causing some issues at build-time by Vite
08:20
Francois
08:20 PM
Either way, I tried this thing here and it fixed our issue: https://github.com/francoislg/typesense-js/commit/d0d6d2e03bfc026814fe7802ab6f93ddb1bcecf5
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:21 PM
Oh hmmm! I wonder how the current js lib works in other environments without Documents.RESOURCEPATH being defined
08:21
Jason
08:21 PM
Let me do some debugging to see what's going on
Francois
Photo of md5-cee98684ac28f06242bd964ea7e9eda7
Francois
08:21 PM
I'm just not sure whether the static getter Documents.RESOURCEPATH was super important here, and I don't know about the in-and-outs enough 😛
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:22 PM
It is important! That's how the URL endpoints are constructed to make API calls
Francois
Photo of md5-cee98684ac28f06242bd964ea7e9eda7
Francois
08:23 PM
I tried to debug the built version and it seemed to have 2 levels of default , so I'm wondering if this is a Vite issue instead (since it works properly in dev mode)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:24 PM
Oh wait, const RESOURCEPATH = '/documents' is defined in SearchOnlyDocuments which Documents extends.
08:25
Jason
08:25 PM
So Documents.RESOURCEPATH actually comes from SearchOnlyDocuments.RESOURCEPATH
08:25
Jason
08:25 PM
Yeah if it works in dev, and not in prod, that seems isolated to the build system?
08:27
Jason
08:27 PM
CC: Damian in case you've seen this issue before
Francois
Photo of md5-cee98684ac28f06242bd964ea7e9eda7
Francois
08:28 PM
Yeah, it might be a build quirk because ./Documents imports ./SearchOnlyDocuments and ./SearchOnlyDocuments imports ./Documents 🤔
08:29
Francois
08:29 PM
I have seen similar stuff like that in the past (but I might be wrong in this case)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:31 PM
Hmm that circular dependency does seem off... wonder how typescript compiler resolves it
08:31
Jason
08:31 PM
may be it somehow knows to only import types based on the usage of the import?
08:34
Jason
08:34 PM
I think removing that circular dependency is good in any case... So if your commit fixes the issue for you, and the test suite still passes, I'd be down to merging it in as a PR
Francois
Photo of md5-cee98684ac28f06242bd964ea7e9eda7
Francois
08:40 PM
Tests are passing!

The only thing I can see that wouldn't work with my fix is if people used to do something like
typesensemodule.Documents.RESOURCEPATH = '/somethingelse' , it would not be taken into account in SearchOnlyDocuments , but I hope people are not doing this 😂
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:41 PM
Haha, I hope not! That's meant to be an internal const anyway
Francois
Photo of md5-cee98684ac28f06242bd964ea7e9eda7
Francois
08:43 PM
"internal const" ☀️ this is what I wanted to read 🙏
08:44
Francois
08:44 PM
do I need to fill the Contributor License thing before the PR ? 😓
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:45 PM
Yeah... It's mainly to give assurances to all users that the code in the code base contributed by everyone is indeed properly licensed as described in the repo
white_check_mark1
Francois
Photo of md5-cee98684ac28f06242bd964ea7e9eda7
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:56 PM
Thank you! I've published this as 1.1.3-0. Could you give it a shot now?
raised_hands1
timer_clock1
Jan 07, 2022 (20 months ago)
Francois
Photo of md5-cee98684ac28f06242bd964ea7e9eda7
Francois
01:31 PM
Sorry, a bit late, I got sidetracked yesterday 😄 1.1.3-0 looks good 🙂
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
02:17 PM
Awesome!