Server Side Includes (SSI) with CloudFlare Workers

Hello!

I opened this issue a while ago, https://github.com/cloudflare/worker-sites-template/issues/50, does anyone have a way to do server side includes with Workers?

I just want a way to load in header/footer without having to maintain 10+ files with their own versions of each.

Thanks!

I found a way to do it, even if it isn’t ideal.

On the client side,

<body onload="getHeader()">

<script>
  function getHeader() {
    var req = new XMLHttpRequest();
    req.onreadystatechange = displayHeader;
    req.open("GET", "/header.html");
    req.send();

    function displayHeader() {
      if (req.readyState != 4) return; 
      document.getElementById("header").innerHTML = req.responseText;
    }
  }
</script>

<div id="header"></div>

will grab the header from the appropriate place, and render it within the “header” div. It isn’t ideal to be doing this on the client side, but it works well enough and workers is fast enough that it doesn’t effect loading times much.

1 Like