Skip to content

Commit

Permalink
Add git commit to service worker version
Browse files Browse the repository at this point in the history
  • Loading branch information
haukex committed Dec 30, 2024
1 parent 4d57958 commit 66ff1cc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/src/js/main.ts ident
/src/js/main.ts filter=git_commit
/src/workers/sw.ts filter=git_commit
2 changes: 2 additions & 0 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
- name: Install Dependencies
run: make installdeps
- uses: actions/configure-pages@v5
- name: Smudge
run: perl -i git_commit_filter.pl smudge src/js/main.ts src/workers/sw.ts
- name: Build pages
run: make
- uses: actions/upload-pages-artifact@v3
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ clean:
.PHONY: installdeps
installdeps:
npm ci
git config set --local filter.git_commit.clean "\$$PWD/git_commit_filter.pl clean"
git config set --local filter.git_commit.smudge "\$$PWD/git_commit_filter.pl smudge"

# This upgrades dependencies to their latest version.
# Run `npm outdated` or `npx ncu` to just see a report without modifying versions.
Expand Down
16 changes: 16 additions & 0 deletions git_commit_filter.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env perl
use warnings;
use strict;

my $CMD = shift // '';
my $repl = '$Commit$'; # default for clean
if ($CMD eq 'smudge') {
chomp( my $commit = `git rev-parse --short HEAD` );
die "Bad commit '$commit'" unless $commit=~/\A[a-fA-F0-9]{4,}\z/;
$repl = "\$Commit: $commit \$";
}
elsif ($CMD ne 'clean') { die "Usage: $0 smudge|clean\n" }
while (<>) {
s{\$\s*Commit(?:\:\s+[a-fA-F0-9]+)?\s*\$}{$repl}g;
print;
}
4 changes: 2 additions & 2 deletions src/js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {LRUCache} from './lru'

if (module.hot) module.hot.accept() // for the parcel development environment

const GIT_ID = '$Id$'
const GIT_COMMIT_RAW = '$Commit$' // is updated by git filters
const INIT_TIMEOUT_MS = 2000
const SEARCH_TIMEOUT_MS = 2000
const SMALL_CHUNK_SZ = 50
Expand Down Expand Up @@ -319,7 +319,7 @@ window.addEventListener('DOMContentLoaded', async () => {

// handler for dictionary load failures
const dictLoadFail = (message :string) => {
error_log.innerText = navigator.userAgent + '\n' + GIT_ID + '\n' + message
error_log.innerText = navigator.userAgent + '\n' + GIT_COMMIT_RAW + '\n' + message
dict_load_fail.classList.remove('d-none')
updateState(MainState.Error)
}
Expand Down
10 changes: 7 additions & 3 deletions src/workers/sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@
// eslint-disable-next-line no-var
declare var self: ServiceWorkerGlobalScope

const GIT_COMMIT_RAW = '$Commit$' // is updated by git filters
const GIT_COMMIT = GIT_COMMIT_RAW.indexOf(' ')<0 || GIT_COMMIT_RAW.lastIndexOf(' ')<0 || GIT_COMMIT_RAW.lastIndexOf(' ')<=GIT_COMMIT_RAW.indexOf(' ')
? '?' : GIT_COMMIT_RAW.substring(GIT_COMMIT_RAW.indexOf(' ')+1, GIT_COMMIT_RAW.lastIndexOf(' '))

// manifest is a list of the static resources that belong to the webapp
// version is a hash calculated by parcel for the static resources
import {manifest, version} from '@parcel/service-worker'
import {DB_URL, DB_VER_URL, DB_CACHE_NAME} from './consts'

/* The name of the cache, dependent on the current version, so that when the version changes,
* the previous cache is discarded and resources are fetched again. */
const APP_CACHE_NAME = `DeEnDict-${version}`
const APP_CACHE_NAME = `DeEnDict-${version}-${GIT_COMMIT}`

/** Send a message to the main window, where the event handler will log it as a debug message.
* This is needed because in Firefox, Service Worker console.log messages don't end up in the main console.
Expand Down Expand Up @@ -75,8 +79,8 @@ async function activate() {
}
// activate this Service Worker on existing pages
await self.clients.claim()
console.debug('SW activated')
sendMsg(`activate done (version ${version})`)
console.debug('SW activated, cache',APP_CACHE_NAME)
sendMsg(`activate done, cache ${APP_CACHE_NAME}`)
}
self.addEventListener('activate', e => e.waitUntil(activate()))

Expand Down

0 comments on commit 66ff1cc

Please sign in to comment.