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
Madaxen86
Madaxen865mo ago
Cache - Nitro
Nitro provides a caching system built on top of the storage layer.
Katja (katywings)
Katja (katywings)OP5mo ago
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 🤔
Brendonovich
Brendonovich5mo ago
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
Katja (katywings)
Katja (katywings)OP5mo ago
Thats not an option since I wanna have a built-in solution 😅
Brendonovich
Brendonovich5mo ago
Built into start or nitropage?
Katja (katywings)
Katja (katywings)OP5mo ago
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 🙂
Brendonovich
Brendonovich5mo ago
What if you checked/populated a cache via middleware?
Katja (katywings)
Katja (katywings)OP5mo ago
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...
Brendonovich
Brendonovich5mo ago
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
Katja (katywings)
Katja (katywings)OP5mo ago
whuaaat 😱 I need to try this out, NOW! The nitro route cache didnt work 😌
Brendonovich
Brendonovich5mo ago
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?
Katja (katywings)
Katja (katywings)OP5mo ago
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 😅
Brendonovich
Brendonovich5mo ago
Makes sense, maybe you could still use cdn-cache-control but use the middleware to handle it yourself in self-hosted scenarios?
Katja (katywings)
Katja (katywings)OP5mo ago
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)
Brendonovich
Brendonovich5mo ago
ayyy nice
Katja (katywings)
Katja (katywings)OP5mo ago
Thank you both for the help @Brendonovich @Madaxen86 ❤️!
lxsmnsyc
lxsmnsyc5mo ago
fun fact: renderToStringAsync is actually renderToStream under the hood.
Want results from more Discord servers?
Add your server