Skip to content

Add cache busting to remove need for Cloudflare purging #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

mrdarrengriffin
Copy link
Collaborator

No description provided.

Copy link

cloudflare-workers-and-pages bot commented Mar 31, 2025

Deploying openhomefoundation-org-website with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8505f6e
Status: ✅  Deploy successful!
Preview URL: https://11b2ec3b.openhomefoundation-org-website.pages.dev
Branch Preview URL: https://cache-bust.openhomefoundation-org-website.pages.dev

View logs

@mrdarrengriffin mrdarrengriffin marked this pull request as ready for review April 9, 2025 08:16
@mrdarrengriffin mrdarrengriffin requested a review from Copilot April 9, 2025 08:16
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • src/_includes/base.liquid: Language not supported

Co-authored-by: Copilot <[email protected]>
@@ -5,6 +5,8 @@ module.exports = (eleventyConfig) => {
eleventyConfig.addPlugin(require("./config/css-config.js"));
eleventyConfig.addPlugin(require("./config/js-config.js"));

// --------------------- Global Data -----------------------
eleventyConfig.addGlobalData("CACHE_KEY", Buffer.from(String(new Date().valueOf())).toString('base64').replaceAll("=", ""));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be automated without the need to remember to add this data to each place it's wants.

  eleventyConfig.addTransform(
    "cache-buster-assets",
    function (content, outputPath) {
      const staticDir = resolve(eleventyConfig.dir.input, "src", "assets");
      if (outputPath.endsWith(".html")) {
        return content.replace(/="\/assets\/([^"]+)"/g, function (_, matcher) {
          const filepath = matcher.split("?")[0];
          const timestamp = statSync(
            resolve(staticDir, filepath)
          ).mtime.getTime();
          return `="/assets/${filepath}?v=${timestamp}"`;
        });
      }
      return content;
    }
  );

Copy link
Collaborator Author

@mrdarrengriffin mrdarrengriffin Apr 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I am on the fence about this. I like having control over what is busted. E.g. images, fonts don't need to be busted on each build

Copy link
Member

@ludeeus ludeeus Apr 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They will not; this used file-modified timestamp, not build timestamp.
Which means that unless the file changes, the v param will stay the same.

Copy link
Member

@ludeeus ludeeus Apr 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is a variant for css/js only:

eleventyConfig.addTransform("cache-buster", function (content, outputPath) {
  const staticDir = resolve(eleventyConfig.dir.input, "src", "static");
  if (outputPath.endsWith(".html")) {
    return content.replace(
      /="\/static\/([^"]+\.(css|js))"/g,
      function (_, matcher) {
        const filepath = matcher.split("?")[0];
        const fileStat = statSync(resolve(staticDir, filepath));
        const timestamp = fileStat.mtime.getTime();
        return `="/static/${filepath}?v=${timestamp}"`;
      }
    );
  }
  return content;
});

And again, as it uses mtime, it will only update when actually needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell, using the modified time won't work since the dist build is made from scratch on each build, meaning this will purge every time. Or am I getting this wrong?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The time is based on the source, not the dist.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes. I knew I was getting something mixed up

@mrdarrengriffin
Copy link
Collaborator Author

mrdarrengriffin commented May 29, 2025

@ludeeus Can we get this current PR implementation approved and work on iterating it in the future. Currently having no implementation causes more problems. Currently, I don't have the time to spend on this but if you can get it working your way, feel free to implement

@mrdarrengriffin mrdarrengriffin requested a review from ludeeus May 29, 2025 13:11
@ludeeus
Copy link
Member

ludeeus commented May 29, 2025

I will not approve something I do not agree with, so someone else will have to do that.
My posted suggestion(s) are working.

@ludeeus ludeeus removed their request for review May 29, 2025 13:26
@mrdarrengriffin
Copy link
Collaborator Author

Yeah, I understand. Thanks for the other PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants