Files
claudeandClaude Opus 5.5 a29293bd7e Bump the service worker's index.html revision after injecting
Funkwhale's service worker precaches index.html by a build-time revision,
so returning visitors kept the unmodified page. inject.sh now writes the
modified page's hash into serviceWorker.js, which makes browsers install
the updated worker and fetch the new page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:48:32 -05:00

35 lines
2.2 KiB
Bash
Executable File

#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-or-later
# Funkwhale front image hook: copy into /docker-entrypoint.d/ (e.g. as
# 98-funkwhale-oidc.sh). Adds oidc-login.{js,css} to index.html once.
# Expects the two files under /usr/share/nginx/html/oidc/ and reads
# FUNKWHALE_OIDC_PROVIDER_ID, FUNKWHALE_OIDC_NAME, FUNKWHALE_OIDC_AUTO_REDIRECT
# and FUNKWHALE_OIDC_LOCAL_LOGIN.
set -eu
index=/usr/share/nginx/html/index.html
grep -q 'oidc-login.js' "$index" && exit 0
esc() { printf '%s' "$1" | sed -e 's/&/\&amp;/g' -e 's/"/\&quot;/g' -e 's/</\&lt;/g' -e 's/[|\\]/\\&/g'; }
url="/accounts/oidc/$(esc "${FUNKWHALE_OIDC_PROVIDER_ID:-oidc}")/login/"
tag="<link rel=\"stylesheet\" href=\"/oidc/oidc-login.css\"><script src=\"/oidc/oidc-login.js\" defer data-login-url=\"$url\" data-name=\"$(esc "${FUNKWHALE_OIDC_NAME:-Single sign-on}")\" data-auto-redirect=\"$(esc "${FUNKWHALE_OIDC_AUTO_REDIRECT:-false}")\" data-local-login=\"$(esc "${FUNKWHALE_OIDC_LOCAL_LOGIN:-true}")\"></script>"
sed -i "s|</head>|$tag</head>|" "$index"
echo "funkwhale-oidc: login button added to $index"
# The stock front config sends /accounts/ to the SPA; the OIDC login and
# callback views live in the API. 20-envsubst-on-templates has already
# rendered the config by the time this hook runs.
conf=/etc/nginx/conf.d/default.conf
if ! grep -q 'location /accounts/' "$conf"; then
sed -i 's|^\(\s*\)location /federation/ {|\1location /accounts/ {\n\1 include /etc/nginx/funkwhale_proxy.conf;\n\1 proxy_pass http://funkwhale-api;\n\1}\n\1location /federation/ {|' "$conf"
grep -q 'location /accounts/' "$conf" || { echo "funkwhale-oidc: could not add /accounts/ to $conf" >&2; exit 1; }
echo "funkwhale-oidc: /accounts/ proxied to the API"
fi
# The web app's service worker precaches index.html by the revision in its
# manifest; without a new revision, returning visitors keep the old page.
sw=/usr/share/nginx/html/serviceWorker.js
if [ -f "$sw" ]; then
rev=$(md5sum "$index" | cut -c1-32)
sed -i "s|\"revision\":\"[0-9a-f]*\",\"url\":\"index.html\"|\"revision\":\"$rev\",\"url\":\"index.html\"|" "$sw"
grep -q "\"revision\":\"$rev\",\"url\":\"index.html\"" "$sw" && echo "funkwhale-oidc: service worker revision for index.html set to $rev"
fi