Hacker News new | ask | show | jobs
by dbbk 1520 days ago
I would love to jump over to something like Vercel or Netlify Edge, but maddeningly none of these platforms give you control over the cache key. I have pages that are server-side rendered with Cache-Control headers, but because our visitors come with unique tracking params on the end of their URL (e.g. from Mailchimp or Branch), we would essentially have no cache hits.

It seems the only way to have control over this is to write your own Cloudflare Workers. There must be a better way? I can't imagine this is an infrequent problem for people at scale.

5 comments

So far Netlify Edge Functions runs before the cache layer, so you can actually use a minimal function to rewrite the URL to remove all unique params, etc, and then let it pass through our system to a Netlify Function which runs behind our caching layer.

For anything you can do at build time as a static HTML pages we already strip query parameters from cache keys.

Interesting, thanks... do you have any docs on how we might achieve this with Next.js? Am I right in thinking we would have essentially a custom Edge Function first that handles query params, and then a second Edge Function that renders the Next app?
I work at Netlify on framework integrations. Next has beta support for running the whole app at the edge, and Netlify supports that. If you create you own custom edge functions they will run first, so you can do just that. You can also run Next "normally" (i.e. in the node-based Netlify Functions) and run your own edge functions in front of that. In those you can modify the Request in any way you'd like, before passing it on to the origin.
Yeah I'm very intrigued by running the whole app on the edge (in their Edge Runtime).

This sounds pretty promising. I'll take a dive and see if I can get it working, thanks for the tip!

Cloudflare Transform Rules let you rewrite URLs on the fly https://developers.cloudflare.com/rules/transform/
I'm biased but there is a better way. Give developers a high performance method programmatically manipulating the cache key from JavaScript. That's what we created with EdgeJS: https://docs.layer0.co/guides/caching It's less work to write and higher performance than dealing with edge functions or worker for routine tasks like this.
Why don't you just link to an API route, consume the tracking params, set a cookie, and redirect to a statically rendered page?
Time to first paint
> There must be a better way?

You're experiencing friction trying to use something in a way that it's supposed to not be used. (I.e., click-tracking by junking up URLs.) You could look for an answer, or you could take a step back, evaluate your expectations, and then decide not to do what you're trying to do.

Unfortunately this is an enormous business and asking them to stop all tracking is well outside of my remit.
At the end of the day, though, no matter how big the business is, it is the result of someone agreeing to fulfill their wishes.