Best Known Methods for Download Button & CDN Redirect

Well I started off on some web searches when it dawned on me that the folks around here have probably already solved this problem and have some great insights especially as it relates to Blocs.

I have a couple of download buttons on my website that in turn point to files that are downloaded from my CDN. This is working just fine, but there’s a couple of things I’d like to improve upon…

Although ultimately, the CDN links will be used and especially for technically minded folks, will always be discoverable, it’s not super graceful having these lengthy and different domain URLs directly shown to users.

The other issue is that as I market my website and its products, when submitting URLs for downloads, I’m effectively hardcoding in my files, of which will be changing over the course of time due to version increments. So instead, it might be nice to have a dedicated URL that simply fetches the latest file regardless of what it’s called (maybe defined by me within the HTML/JS) so that I have a single universal download URL that will always work.

So ultimately my question is, what are the best known methods folks are using in Blocs today to handle this scenario? I see @Norm is using at the Blocs website a form/script to handle the download. My preference at this moment though is to go formless and reduce the friction of having someone download my apps.

I’ll answer my own question here and detail what I ultimately did, which was literally about 60 seconds of work.

My server backend is nginx, so I simply created a redirect for a static URL that then points to the latest download file from my CDN. This was super easy to do as I’m a Lifeboat from Strawberry Software user which makes this effortless rather than hacking on the nginx.conf file.

But for those who stumble upon this post and would like to do something similar, here’s an example change for the nginx.conf file:

location = /download/latest {
		return 301 https://myCDN.com/file.zip;
	}

From the Blocs side of things, I’ve wired up my download button to…

So now all I need to do down the road is change my redirect target URL whenever new versions of my product roll out.

Hope this helps someone else down the road.

Or you can use your cPanel and add redirects. NoCode.

Yes, absolutely if you’re on a more “webby” hosting platform instead of a standalone VPS.

Also as a follow-up to my solution above, although this got me almost all of the way there, the one thing I was missing was a tie into my GoatCounter analytics platform as this bypasses any of my html that includes my analytics scripts.

So I’ve adjusted things so that the following happens…

  1. nginx redirect goes to an html file
  2. html file includes my GoatCounter script

I spent some time hacking on this between a meta refresh, javascript refresh, etc. but couldn’t get things exactly where I wanted it from not returning to the originating download page, to not opening in a new tab/window, etc.

In the end and after a few minutes with ChatGPT, here’s the solution it generated:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Automatic File Download, Redirect, and GoatCounter Tracking</title>
</head>
<body>
  <!-- GoatCounter tracking script -->
  <script data-goatcounter="foo" async src="bar/count.js"></script>

  <script>
    // Function to initiate download and redirect
    function initiateDownloadAndRedirect() {
      // Replace 'path/to/your/file.pdf' with the actual path to your file
      var fileUrl = 'path/to/your/file.pdf';
      var fileName = 'filename.pdf';
      var redirectUrl = 'https://www.example.com'; // Replace with your desired redirect URL

      // Create a hidden anchor element
      var downloadAnchor = document.createElement('a');
      downloadAnchor.href = fileUrl;
      downloadAnchor.download = fileName;

      // Append the anchor to the body and trigger a click event
      document.body.appendChild(downloadAnchor);
      downloadAnchor.click();

      // Redirect to another page after the download starts
      setTimeout(function() {
        document.body.removeChild(downloadAnchor);
        window.location.href = redirectUrl;
      }, 100);
    }

    // Check if GoatCounter script has finished loading
    function checkGoatCounter() {
      if (typeof goatcounter !== 'undefined') {
        // GoatCounter script has loaded, call the function to initiate download and redirect
        setTimeout(initiateDownloadAndRedirect(), 1000);
      } else {
        // Retry after a short delay if GoatCounter script is not yet loaded
        setTimeout(checkGoatCounter, 50);
      }
    }

    // Call the function to check GoatCounter script loading status
    checkGoatCounter();
  </script>

  <!-- Manual download link -->
  <noscript>
    <p>If your browser doesn't support JavaScript or you've disabled it, you can <a href="path/to/your/file.pdf" download="filename.pdf">download the file manually</a>.</p>
  </noscript>
</body>
</html>

There of course is a bit of a refresh flash when the workflow happens, but this makes everything work pretty great from my side. I now have a dedicated URL that can be used to download the latest file, my analytics gets called and to increment my download file it’s just a simple HTML file adjustment.

I would agree this is a bit hacky with the creation of an anchor that gets automatically clicked, but this works better than any solution I was able to come up with after a bit of back and forth between BBEdit and my web browser.

UPDATED: GoatCounter wasn’t always capturing the download and from what I can tell the redirect might have been happening before the script completed. So I wrapped the initiateDownloadAndRedirect in a 1 second setTimeout and that makes this work much better.