solid-start: How to import browser-only depencency
I'm trying to include a browser-only dependency into a solid-start project that is partly server-side rendered.
I.e. my vite.config:
Now after doing the usual
npm install rudder-sdk-js
, when I do npm run dev
, I get:
35 Replies
This happens when running
dev
, I haven't even gotten to prod yetYou should just be able to use a dynamic import, so the dependency doesn't get included in the server bundle
i.e. not just plopping the import into a Tracking.ts file, like:
yeah
so vite https://vitejs.dev/guide/features.html#dynamic-import says
I guess that is again not it, so what you are saying what I should do is:
is that it?
I didn't even know It's allows to
import
inside a jsx component, never imported anything beyond the beginning of a js file beforeYou're not able to await within a component as they're not async
But you can import it in onMount, and set a variable to it
Something like this could work
Or you can make
handleTrackingCall
async, and dynamically import the module if it doesn't exist
The latter has the added benefit of making it lazy: the analytics are only imported when they are actually usedi see
do you also know a typeascript trick to set the type correct for TS?:
?
I think it'd need to be
Awaited<(typeof import("./file.js"))>
As the import itself returns a promise
seems to work
Oh perfect, I guess
Awaited<T>
isn't neededmhmm now the site loads but it shows strange errors
Can you see where exactly it fails?
Like the line numbers and what's actually on that line
tryuing to but since it's all one line its hard
thats the line
I think i'll try loading it via cdn, and only use
in the code
I mean, if the dependency uses a lot of this stuff (
window.rudderanalytics.errorReporting=this
), maybe it'll still fail even if I do the dynamic importIt shouldn't, unless something depends on rudderanalytics which is imported normally
mhmm do you have an idea how to fix the error in the line https://discord.com/channels/722131463138705510/1127918107508342894/1127932019922436259 then?
my code so far is:
right after that, it'll encounter the error, trying to do
window.rudderanalytics.errorReporting=this
when I log console.log('window: ' + window.rudderanalytics);
after the last line in the above code, it's undefined
Maybe try binding it to the window manually?
After importing, just do
window.rudderanalytics = rudderanalytics
yes, already on it, unfortunately it seems to crash my chrome now, or at least make it unusefully slow
their documentation says they recommend importing the code via cdn, and only offer the npm module as a fallback. Maybe it's just not very well done
Yeah, possibly
I'm honestly not really sure what the root cause is
Have you tried it through a CDN yet?
doing that atm, example:
thats just the script they say you're supposed to copy+paste to your own page
but honestly, i'm not even seeing the
console.log('loading rudderstack script')
.
Is adding <script>
to components supported by solid-start?Maybe not within the components themselves
It's definitely fine within root.tsx
I use it in one of my projects for google gsi
mhmm but
root.tsx
is not necessarily meant to have that tracking everywhere
i also have a different project where I have scripts only in the subfolder [id].tsx
and there stuff worksright
So it'll be something about having it here
There's this svelte tutorial which could be useful https://www.rudderstack.com/guides/how-to-event-stream-data-from-svelte-app-to-google-analytics-using-rudderstack/
Rudderstack
How to event stream data from your Svelte app to Google Analytics u...
This article will walk you through the process of integrating your Svelte app with RudderStack using our JavaScript SDK
A lot of the same practices will probably apply to solid
It looks like they're placing it in the root
So ig that doesn't work for you
mhmm, yeah, so I could at least isolate now that doing:
brings the whole browser down (I'm assuming some kind of infinite loop).
And without the
window.rudderanalytics = rudderanalytics
it'll fail with the above discussed line
sanity check, shouldn't:
at least print a hello world?
it does when I stick it in root.tsx
when I stick the same script into
(app.tsx)
it also prints hello (app)
, but the one script I put into (app)/(csr)/index.tsx
does not print in the browserwhich is probably due to the
<ClientOnly>
component sitting below itProbably
Maybe just try adding manually with the normal DOM api
I think I can just move
<TrackingContextProvider>
outside of <ClientOnly>
That's probably better
yup, that seems to fix the CDN
Awesome :)
so, when I first tried it, I did a quick test with CDN, which didn't work. Then I tried for hours with the dynamic imports, which doesn't work with the rudderstack code the way it's built.
turns out the cdn didn't work because of our SSR/CSR split
thank you @tommypop !
You're welcome :)