11#! /usr/bin/env bash
22set -euo pipefail
33
4+ # `npm publish --dry-run` still runs the publish/postpublish lifecycle scripts —
5+ # npm's publish.js guards only the upload with `if (!dryRun)`. Without this guard
6+ # every release purged jsdelivr twice: once during the pre-publish dry run, when
7+ # the new version did not exist on the registry yet so the wait loop below could
8+ # not possibly succeed, and once for real. The dry-run pass purged the @latest
9+ # alias while it still resolved to the PREVIOUS version, which makes jsdelivr
10+ # re-fetch and re-cache the old build under @latest.
11+ if [ " ${npm_config_dry_run:- } " = " true" ]; then
12+ echo " npm publish --dry-run — skipping CDN purge"
13+ exit 0
14+ fi
15+
416PREV_TAG=$( git describe --tags --abbrev=0 HEAD^ 2> /dev/null || true)
517
618if [ -z " $PREV_TAG " ]; then
@@ -24,18 +36,29 @@ echo "$FILES" | sed 's/^/ /'
2436echo " "
2537
2638echo " Waiting for npm registry to serve $CURRENT_VERSION as @latest..."
39+ LATEST=" "
40+ REGISTRY_READY=0
2741for i in $( seq 1 24) ; do
2842 LATEST=$( curl -fsS " https://registry.npmjs.org/hyperclayjs/latest" 2> /dev/null | node -p " JSON.parse(require('fs').readFileSync(0,'utf8')).version" 2> /dev/null || echo " " )
2943 if [ " $LATEST " = " $CURRENT_VERSION " ]; then
3044 echo " npm registry updated after ~$(( i * 5 )) s"
45+ REGISTRY_READY=1
3146 break
3247 fi
33- if [ " $i " -eq 24 ]; then
34- echo " Warning: npm still shows $LATEST after 120s, purging anyway"
35- fi
3648 sleep 5
3749done
3850
51+ # Purging while the registry still answers with the old version is worse than
52+ # not purging: jsdelivr re-resolves @latest, gets the old version back, and
53+ # caches that fresh. Leaving the existing cache to expire is the safer failure.
54+ if [ " $REGISTRY_READY " -eq 0 ]; then
55+ echo " "
56+ echo " WARNING: npm still shows ${LATEST:- unknown} after 120s — NOT purging."
57+ echo " Purging now would re-cache the old build under @latest, which production loads."
58+ echo " Re-run once the registry has caught up: bash scripts/purge-cdn-cache.sh"
59+ exit 0
60+ fi
61+
3962# jsdelivr purge API tolerates ~60 requests/min; purge sequentially at 1/s,
4063# back off on 429 (5s, 10s, 20s), log a hard failure and move on after 3 tries.
4164FAILED=0
@@ -83,3 +106,23 @@ if [ "$FAILED" -gt 0 ]; then
83106 echo " WARNING: $FAILED purge(s) failed — stale files may persist on the CDN for up to 7 days."
84107 echo " Purge manually: curl https://purge.jsdelivr.net/npm/hyperclayjs@latest/<path>"
85108fi
109+
110+ # A 2xx from the purge API only means the request was accepted. Production loads
111+ # hyperclayjs from the unpinned @latest, so confirm the alias actually moved.
112+ echo " "
113+ printf " Verifying jsdelivr @latest now serves %s... " " $CURRENT_VERSION "
114+ SERVED=" "
115+ for i in $( seq 1 12) ; do
116+ SERVED=$( curl -fsS " https://cdn.jsdelivr.net/npm/hyperclayjs@latest/package.json" 2> /dev/null | node -p " JSON.parse(require('fs').readFileSync(0,'utf8')).version" 2> /dev/null || echo " " )
117+ [ " $SERVED " = " $CURRENT_VERSION " ] && break
118+ sleep 5
119+ done
120+
121+ if [ " $SERVED " = " $CURRENT_VERSION " ]; then
122+ echo " ✓"
123+ else
124+ echo " ✗"
125+ echo " WARNING: jsdelivr @latest still serves ${SERVED:- unknown} , expected $CURRENT_VERSION ."
126+ echo " Production loads hyperclayjs from @latest, so it may still be running the old build."
127+ echo " Re-run: bash scripts/purge-cdn-cache.sh"
128+ fi
0 commit comments