Let me tell you the story of how I learned that you can build Progressive Web Apps on Cloudflare’s network around the globe with one JavaScript bundle that runs both in the browser and on Cloudflare Workers with no modification and no separate bundling for client and server. And when registered as a Service Worker, the same JavaScript bundle will turn your page into a Progressive Web App that doesn’t even make network requests. Here's how that works...
"Any resemblance to actual startups, living or IPO'd, is purely coincidental and unintended" - @sevki
A (possibly apocryphal) Story
I recently met up with some old friends in London who told me they were starting a new business. They did what every coder would do... they quickly hacked something together, bought a domain, and registered the GitHub org and thus Buzzwords was born.
The idea was simple: you could feed the name of your application into a machine learning model and it would generate the configuration files for your deployment for various container orchestrators. They achieved this by going through millions of deployment configurations and training a linear regression model by gamifying quantum computing because blockchain, or something (I told you this story was apocryphal).
I was intrigued, to say the least, but I was playing it cool. One of the co-founders broke the silence, "So, does any of these sound like something you'd like to work on?" I struggled with it for tens of seconds. You see I only recently started this new job at Cloudflare, and I actually like my job and the people I work with. So I said, "Hell yeah, man, let's change the world one container at a time". One of them said, "Well, since we really can’t pay you right now, and we don't seem to be able to set aside enough time to build our website, would you mind helping us out with that on a trial basis, like an interview?" I enthusiastically accepted.
So while the Buzzwords crew were busy producing HYPE, I set off to build their website. As any business starting up, discover-ability is paramount to them, so Buzzwords definitely needs to be optimized for search engines so they can generate organic traffic from keywords like "machine learning", "YAML", "containers" and "blockchain".
When parsing dynamic pages, crawlers need to do more work, there is an inherent penalty for using fancy frameworks compared to plain old HTML.
Don’t take my word for it, as Google cautions;
Currently, it's difficult to process JavaScript and not all search engine crawlers are able to process it successfully or immediately. In the future, we hope that this problem can be fixed, but in the meantime, we recommend dynamic rendering as a workaround solution to this problem.
Yet, the Buzzwords folks still wanted something fancy I told them about this new thing called React hooks. I slammed a La Croix and put together the 2018 equivalent of Hello, World! which is still Hello, World! but with an ? at the end.
import React from "react";
import ReactDOMServer from "react-dom/server";
class HelloMessage extends React.Component {
render() {
return Hello, {this.props.name} ?;
}
}
async function handleRequest(event) {
return new Response(ReactDOMServer.renderToString( ), {
headers: {
"Content-Type": "text/html"
}
});
}
self.addEventListener("fetch", event => {
event.respondWith(handleRequest(event));
});
Other than setting up Webpack to bundle your code, this is pretty much all you need to get started with React and Cloudflare Workers. But Hello, World! is hardly a startup website. Thankfully, since all startup websites look exactly the same it's really not that hard to build one. First, I mapped the paths to pages:
let routes = {
"/": ,
"/hype": ,
"/careers":
};
Instead of rendering any old component, I just used the URL's pathname
, available both in the browser and the edge, to pick the correct component.
const header = ` `;
const footer = `