How do i conditionally add something to <head> based on what createAsync returns?
I need to wait for a resource coming from
createAsync
so then i can decide whether i should add google recaptcha to <head> using useHead
from @solidjs/meta
38 Replies
chat()
is returning undefined
because its not waiting for the promise to resolve.It seems you're intending to create an effect to when chat is done loading:
would that work with ssr? i'm using solid start
i need the <script> to be injected in ssr
i think
createEffect
is for client only?oh, got it. Sorry. And I was looking, it seems solid meta doesnt support <Script> right?
I think you could create your own and put it into a Suspense boundary:
yeah solid meta doesn't support <Script> but useHead adds the ability to inject any tag.
nice! i will see
oops. that didn't work.
its injecting the <script> in all cases.
@edygar do you have any other fixes?
I was trying to reproduce on the playground
ah. ok. take your time!
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
The script tag is only added when the suspense is lift
Oh, forgot to make it "random"
xD wait a bit
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Ok, on this example, only when Date.now() returns a even nubmer it loads the catpcha
this changes nothing in the example u already gave me
here
which didn't work sadly
maybe i should give you the whole code i have?
if shouldShowChat is true, captcha script shouldn't be in <head>
aahá!
shouldShowChat should indicate that the captcha was done correctly, but this logic is not implemented yet.
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
I put an alert to help us there xD
🤣
Recaptcha was being called while we were loading, even with the suspense. Now it returns a function so it will only be called when the "element" was supposed to be included
i already tried <Show>, it didn't work
It does work the way I did there.
fallback={Recaptcha}
That's the trick, it only calls Recaptcha after the suspense is lift
yeah i did that exactly
maybe it works for u in solid CSR
but it didn't for me in solid start
with ssr
and also it gives a ts error because the fallback needs to be <Recaptcha/> and Recaptcha should return <></>
😭 f*ck, I don't get why createAsync signal doesnt return the same flags as createResource does, it would be way simpler
yeah idk
Still catching up on the thread but to delay the SSR response until an async op has settled use
{ deferStream: true}
on createAsync
.GitHub
solid-router/src/data/createAsync.ts at a2652b4eab6576db78e6371e5c0...
A universal router for Solid inspired by Ember and React Router - solidjs/solid-router
didn't do what i wanted
tbf you don't have to use createAsync, createResource is still perfectly useful
will it fix my issue?
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
nope
i'm starting to think this isn't possible
haha
🤣 don't think so, It's just my inexperience
does this work?
this will render the captcha while
getChat
is loading, since during that time chat()
will return undefined
which is falsey, and even though there's a suspense, suspense is non-blocking and renders its children while it is suspendingfinally. it did!
thank you lol
ahá! so the trick was
=== null
Could you try this one? Just for learning
Recaptcha doesn't even return jsx
and yeah it works
this also fixed a weird bug where my captcha doesn't render some times 😆
crazy
createAsync
solution.this is already resolved by https://discord.com/channels/722131463138705510/1242072263767887992/1242136782238121985
This was in response to "didn't do what i wanted".
With
With
if (!chat())
your code wasn't differentiating between the unresolved and resolved state of chat()
.is there a way to show a suspense fallback with this solution?
actually the suspense fallback is working, but not when i
revalidate
...
i want the suspense fallback to rerender on revalidate
suspense fallback only retriggers when using createResource
and refetch
and removing cache