Struggling to run server code before workers cache
So in my case inside hooks.server.ts (similar to middleware in other frameworks) im trying to write analytics, and then check cache.
But in my case analytics are never written if i get a cache hit. im not using cache headers, only return the cache response from the server. does anyone have a clue why only non cache hits trigger my code here?
7 Replies
code looks fine, i'd make sure something like this block is not hit:
const cache = event.platform?.caches?.default; if (!cache) return resolve(event);i don't know what framework this is - sveltekit? but i'd check none of the earlier resolves are firing
yep its sveltekit. its a bit weird. if i do a normal navigation or refresh i just hit the GET endpoint. hook is not even ran. if i hit it with a cmd shift r or what ever the hard refresh is, i get to the hook, and the analytic is written

but yea seems related to sveltekit not cloudflare
weird yeah, sadly can't help with sveltekit sorry
damn very weird issue. so it seems to be partially both.
this part of code (if cache hit ofc) stops my server part to run. which means my hook wont run. which means i just get the cached html back. genuinely no clue how this even works at this point. i thought returning the cache will just return the cached response but somehow also stops code from executing in svelte
I don't know what svelte is doing but this does indeed return a cached response. I don't know why that'd prevent further code running though
i asked the svelte discord and they said not running resolve(event) prevents server load functions to run which also means the hook wont run.
how the cache response hits the user? no clue but it does somehow work lol
oh well time to put the analytics in an api endpoint and call it on the client ig