Let an Edge Function do it.

Four years ago Phil acted on a silly idea to deploy a Netlify site every single minute such that an entirely static site could do something as notably dynamic as displaying the current time. There is literally no JavaScript on that site.

It mostly worked. Aside from a failed deploy for random reasons here and there, it did its thing:

From 2018 to 2022 the site has been automatically rebuilt and redeployed millions of times.

Phil, What I learned from automating millions of web site deploys

It makes me a little queasy to think there are literally millions of deploys of that site sitting on cloud computers that will just sit there until the end of time. But there are much worse offenders in the world of useless data storage so I should probably get over it.

I really dig the upgrade to use Edge Functions instead. Now the site never needs to be deployed again. When you hit it, the edge function runs, gets the time (bonus: correct time zone), and alters the HTML to have that correct time before that HTML gets to the browser. Perfect. MAKE THE EDGE DO IT. I suppose it could cache itself for a minute too, but hey whatever this isn’t exactly a mega production project.

I love the idea of making the edge do it.

Say instead of returning time-injected HTML, that edge function performed an API request and injected useful production data into the HTML. Your client-side JavaScript could do that too, but let’s consider that for a moment. Which do you think is faster, a request from a cloud runtime hooked up to fat internet pipes designed for speed executing one single task, or your broke-ass personal computer on questionable WiFi in a browser with 90 tabs open requesting through single-threaded JavaScript that is also handling 80 other things?

Then, not only is the former obviously faster at just doing the job of fetching data, but less/none client-side JavaScript is required at all, and the HTML has useful, renderable content as soon as it gets there.

One response to “Let an Edge Function do it.”

  1. Rob L. says:

    Sorry, I know the post’s a month old, but I’m catching up on some long-neglected feeds…

    When you hit it, the edge function runs, gets the time (bonus: correct time zone)

    The time zone’s liable to be wrong if there’s a VPN in play. Do edge functions have a way to get the client’s time zone a little more reliably?

Leave a Reply

Your email address will not be published.