Skip to content

Add commit-hash-based cache busting to PWA service worker#3

Merged
rorpage merged 3 commits into
mainfrom
copilot/add-cache-busting-mechanism
Mar 27, 2026
Merged

Add commit-hash-based cache busting to PWA service worker#3
rorpage merged 3 commits into
mainfrom
copilot/add-cache-busting-mechanism

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 27, 2026

PWA users were stuck on stale cached assets indefinitely — the service worker used a hardcoded cache key (states-tracker-v1) with no invalidation on deploy.

Changes

sw.js

  • Replace static cache version with __COMMIT_HASH__ placeholder, yielding names like states-tracker-60c280c per deploy
  • Add activate handler to purge all caches not matching the current version
// Before
const CACHE_NAME = 'states-tracker-v1';

// After (placeholder substituted at deploy time)
const CACHE_NAME = 'states-tracker-__COMMIT_HASH__';

vercel.json (new)

  • Adds a Vercel build command that substitutes __COMMIT_HASH__ with the first 7 characters of VERCEL_GIT_COMMIT_SHA (Vercel's built-in env var) via sed before the files are served
  • Sets outputDirectory to . so the project root is served as-is after the substitution

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 27, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
autoplay Ready Ready Preview, Comment Mar 27, 2026 8:46pm

Copilot AI changed the title [WIP] Add cache-busting mechanism for PWA Add commit-hash-based cache busting to PWA service worker Mar 27, 2026
Copilot AI requested a review from rorpage March 27, 2026 20:35
@rorpage
Copy link
Copy Markdown
Owner

rorpage commented Mar 27, 2026

@copilot I'm deploying to Vercel so I don't need the Action. If we can inject the commit hash into the service worker that should suffice right?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 27, 2026

@copilot I'm deploying to Vercel so I don't need the Action. If we can inject the commit hash into the service worker that should suffice right?

Yes, that's exactly right. I've removed the GitHub Actions workflow and replaced it with a vercel.json (commit 91d813a) that injects the commit hash at Vercel build time:

{
  "buildCommand": "bash -c \"sed -i 's/__COMMIT_HASH__/'${VERCEL_GIT_COMMIT_SHA:0:7}'/g' sw.js\"",
  "outputDirectory": "."
}

Vercel exposes VERCEL_GIT_COMMIT_SHA on every build, so the service worker will end up with a unique cache name like states-tracker-60c280c per deployment. The activate event in sw.js then purges any caches from previous versions automatically.

@rorpage
Copy link
Copy Markdown
Owner

rorpage commented Mar 27, 2026

@copilot Mark this as not draft and let's get it committed and pushed

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 27, 2026

@copilot Mark this as not draft and let's get it committed and pushed

I'm unable to convert the draft status from this environment due to API permissions. You can mark it ready for review by clicking the "Ready for review" button at the bottom of the PR page on GitHub.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/graphql
    • Triggering command: /usr/bin/gh gh api graphql -f query=mutation { markPullRequestReadyForReview(input: { pullRequestId: "PR_kwDORczpv87OJ2J3" }) { pullRequest { isDraft } } } (http block)
    • Triggering command: /usr/bin/gh gh pr ready 3 --repo rorpage/autoplay (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from rorpage March 27, 2026 20:57
@rorpage rorpage marked this pull request as ready for review March 27, 2026 21:00
@rorpage rorpage merged commit a7ed97d into main Mar 27, 2026
3 checks passed
@rorpage rorpage deleted the copilot/add-cache-busting-mechanism branch March 27, 2026 21:00
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