Refetching Route Data with Solid Router
Is there any way to refetch (revalidate) the data functions with
@solidjs/router
? (not Solid Start; I'm using Vite)
- I'm mutating something on the backend and would like to make sure the data is up to date
- Returning the whole createResource
return value kind of works -- calling refetch
correctly refetches the data, but mutate
doesn't work.
- Additionally, I'd like to refetch/revalidate all resources somehow. Is that possible?68 Replies
Happy to take a look, what do you mean by mutate doesn't work?
Also posting some code would be helpful for us to lend a hand
Example with comments explaining the problems ☝️
I'm probably just doing something dumb (or this isn't supported at all)
No, no the solid docs are a work in progress I afaik so a lot of things aren't well documented but are fully supported. So when you call mutate you expect it to mutate the resource object, but are you also mutating it on the server so that if it refetches the new data would be the same as the "feebarbaz" object?
Oh I see, interesting, I just read the comments and you're saying the data in project object is updated as per the call to the setter but it doesn't cause a rerender despite using them in a reactive context i.e calling the getter?
So the way I normally go about this is:
- someCallback realizes that the data needs to be updated and sends a tRPC call to the backend with the new object, then I trigger a refetch of the async data so I don't have to mutate my values at all, you can do optimistic updates by using the mutate function and then reconciling with the servers response, does this sound like what your implementation is?
https://start.solidjs.com/api/useRouteData#retrieving-data-from-the-routedata-function
you can't blame yourself for not knowing how this works, the docs are pretty sparse on the setter and getters
SolidStart Beta Documentation
SolidStart Beta Documentation
Early release documentation and resources for SolidStart Beta
Yes! Exactly
That is certainly very strange
you can do optimistic updates by using the mutate functionExactly what I'm trying to do refetching works, but if the user has a slow connection, it comes out all janky
Right okay cool, so we're on the same page.
What do you mean by janky?
like I know it means not great lol
Oh I just meant the user experience lmao
gotcha
Like flickering and layout shift?
Yeah
This isn't the codebase I'm working on right now, but take a look at this:
After unfocusing the input, there's a 1-2s delay until it's done refetching
I want to optimistically update, but
mutate
doesn't seem to work
But I may be doing something stupid, I'll definitely try again to make sure
It's weird because refetch
works, though
Is there any possibility this isn't related to mutate
, but to list rendering? The stuff I'm trying to mutate is an array rendered by <For>
So my mutate
call ends up looking like this:
Oh totally get the problem, but I haven't yet attempted this in solid, I am rewriting my Next.js and React app in Solid and I managed to do it in two nights, plus some finagling - which coincidentally, leaves me with one more job to do, update a users profile optimistically using Solid's createResource, so I am in the same boat as you
oh lol
So you're mutating to filter an array basically?
I'm mutating one element in the array
gotcha, and the array is the config array of that card
Each card represents one element in the array
Each card is a project
right okay, understood
Either way, I think this is either a bug with @solidjs/router or just unsupported
and so you anticipate mutate to work instantly, which I would agree is true and refetch should then reconcile
I might be doing something stupid but I can't see it
have you tried with dummy fetches?
Right
just to see?
dummy fetches?
Refetching works, just optimistic updates (
mutate
) don't
That's why I'm so baffled by it lollike
to eliminate the server as a variable in the issue and ensure that the delay of optimistic update is not the server delay
if that makes sense
i.e is mutate basically refetching
and not setting
No, mutate always optimistically updates
okay huh
weird
createResource is really just createSignal + a wrapper around fetch
yeah ik
and I managed to do it in two nightsThat's absolutely crazy And huge for solidjs adoption
It was, I mean I am crazy and worked about 12hrs a day on it
I'm doing basically the same, it's taking slightly longer but I'm rewriting more
and the app is only about 20-30 pages
"only"
that's absolutely crazy
it's an e-commerce app with a backend dashboard, auth, etc.
most of the react JSX was portable
I think if anything is gonna see mainstream adoption of the new frameworks it's SolidJS
only the business logic needed some minor tweaking and the solidjs DX is sooooo much better than Next
Are you using solid start?
I agree, though some people seem to be really into Svelte
Or astro/vite/...?
yep
How's that working out for you? Any bugs/issues?
No it's been great, I might run into the same thing you're running into in a minute, I am coding the page where users can update their profiles in the same optimistic with backend refetch
Not sure if you will
I'm using SolidJS + Vite, so no Solid Start
Some random issues where my client side javascript stopped hydrating for no apparent reason
@solidjs/router only
in dev or prod?Oh interesting, what made you choose that?
in both actually
Huh
I just rolled back and slowly introduced the stashed changes back and I still have yet to recreate the issue
I don't need SSR that badly, and marketing pages I can move to astro or prerender "manually" if it affects SEO (I think solid has good SSR support without Solid Start)
Since I'm building more of an app
Fair enough, what is your project? if I may ask
I am also working on an open-source blog/cms starter app for the solid community
Doing a transcription/content generation thingy
Happy to share more soon if you're interested :P
ooo neat, yeah I am all ears
are you going to monetize or open-source
Oh, it's for a company
I'm doing freelance
Ohhhh interesting okay
The current version of the app is with Remix.run
But I'm having a couple issues, not necessarily because of Remix, but I'm building more of an app
yeah I have never worked with Remix
yeah I get it, hopefully everything is on time and you're making good money on the project then
Thanks, appreciate it a lot 😄
Of course
If you don't mind me asking... what are you working on? o:
I am working on the aforementioned blog cms platform open-source solidjs ecosystem
and I am working on a personal project where I resell hobby chemistry/experimental lab equipment and supplies and merch on an e-commerce platform, but I am doing a really thorough job of the e-commerce platform so I can reuse the code for future endeavors or an open-source thing again
I also started to look into developing an auth library
but Lucia Auth did almost exactly what I envisioned, basically AuthJs but less opinionated
though they don't have an officially supported solid package
Wow, that sounds awesome -- good luck with all of those :)
I'd love to take a look at the lab equipment one
I might be able to show you, it might require some staging first
it's the only not open-source of the list XD
One aspect of it that will be though is a cheminformatics API
but that is only in the very beginning stages
Oh, no worries, I thought you had it live somewhere
Would love to see it once it's live :)
Very odd. What happens if you move the resource call into the component body? Not that I expect that to make a difference