Caching server-side render results
How can results of server-side renders be cached for consequential requests?
I assume this would need
renderToStringAsync
on the server, but SolidStart always renders with renderToStream, even in async mode 🤔. (https://github.com/solidjs/solid-start/blob/65c8ac0844ffac42947773e6d529e307ac687513/packages/start/src/server/handler.ts#L99)GitHub
solid-start/packages/start/src/server/handler.ts at 65c8ac0844ffac4...
SolidStart, the Solid app framework. Contribute to solidjs/solid-start development by creating an account on GitHub.
17 Replies
Maybe like this?
https://nitro.unjs.io/guide/cache#caching-route-rules
Cache - Nitro
Nitro provides a caching system built on top of the storage layer.
Great suggestion 🙂👍, but it wouldnt work in my case 🙈. I need to be able control via runtime if and when a route should be cached 😅
Hmmm unless, maybe shouldBypassCache could be used for that logic 🤔
Could u use cache-control headers?
That’d work for client caching and if you’re on netlify/vercel/cloudflare you can use cdn-cache-control
Thats not an option since I wanna have a built-in solution 😅
Built into start or nitropage?
Nitropage 🤓, I mean I use what I can get from Start / Vinxi / Nitro, but NP has to glue it together in a way that makes it useable by non-devs 🙂
What if you checked/populated a cache via middleware?
Thats exactly what I wanna do, but I think renderToStream that Start uses for async is not the proper render type for caching.
I can try it with renderToString for now...
I think it’ll work, renderToStringAsync is just a wrapper of renderToStream and start replicates the same logic
Oh yeah renderToStream’s return value is both a promise-like and stream-like
whuaaat 😱
I need to try this out, NOW! The nitro route cache didnt work 😌
Id be surprised if the fully formed html isn’t part of the middleware response, since the request won’t resolve on the client until ssr is done
To clarify, cdn-cache-control wouldn’t work bc it relies on the deployment method yeah?
If users wanna go the cloudflare route to optimize their NP deployments all power to them, but the included solution in NP should work without external services by default, thats the key 😅
Makes sense, maybe you could still use cdn-cache-control but use the middleware to handle it yourself in self-hosted scenarios?
Yeah that seems like the best way to do this, great idea!
Holy moly! The onBeforeResponse middleware even receives the body as a string because nitro awaits the promise in async mode. I can lower the server html response times with this from 220ms to 4ms 😱. (Tested locally with an sqlite database)
ayyy nice
Thank you both for the help @Brendonovich @Madaxen86 ❤️!
fun fact: renderToStringAsync is actually renderToStream under the hood.