From c46eeeee6e349459350c61ae8ca6fbeea37ef6e2 Mon Sep 17 00:00:00 2001 From: Tiago <70700766+tiagorangel1@users.noreply.github.com> Date: Mon, 16 Jun 2025 10:46:21 +0100 Subject: [PATCH] standalone: add ability to change assets host versions --- docs/guide/standalone.md | 6 ++++-- standalone/package.json | 2 +- standalone/src/index.js | 24 ++++++++++++------------ 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/docs/guide/standalone.md b/docs/guide/standalone.md index ef9b1fc..9510c5c 100644 --- a/docs/guide/standalone.md +++ b/docs/guide/standalone.md @@ -58,7 +58,7 @@ Example: > ``` -> [!TIP] +> [!TIP] > Does generating challenges not work? Make sure your server is publicly accessible and that `CORS_ORIGIN` is set correctly to allow requests from your website's origin. ### Server-side @@ -131,4 +131,6 @@ And by setting `window.CAP_CUSTOM_WASM_URL` to the path of the `cap_wasm_bg.wasm window.CAP_CUSTOM_WASM_URL = "https:///assets/cap_wasm_bg.wasm"; ``` -By default, these fetch from `process.env.CACHE_HOST`. You can change this by setting the `CACHE_HOST` environment variable when running the server. If you don't set it, it defaults to `https://cdn.jsdelivr.net`. You can also set it to `disable` to disable the assets server entirely. \ No newline at end of file +You can and should also change the version of the widget and WASM files by setting `process.env.WIDGET_VERSION` and `process.env.WASM_VERSION`. This will help you avoid breaking changes in the future. + +By default, these fetch from `process.env.CACHE_HOST`. You can change this by setting the `CACHE_HOST` environment variable when running the server. If you don't set it, it defaults to `https://cdn.jsdelivr.net`. You can also set it to `disable` to disable the assets server entirely. diff --git a/standalone/package.json b/standalone/package.json index e0f6c6c..2588bb1 100644 --- a/standalone/package.json +++ b/standalone/package.json @@ -1,6 +1,6 @@ { "name": "cap-standalone", - "version": "1.0.16", + "version": "1.0.17", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "bun run --watch src/index.js", diff --git a/standalone/src/index.js b/standalone/src/index.js index 0b211e8..67f43b8 100644 --- a/standalone/src/index.js +++ b/standalone/src/index.js @@ -61,24 +61,24 @@ const updateCache = async () => { if (!(currentTime - lastUpdate > updateInterval)) return; const CACHE_HOST = process.env.CACHE_HOST || "https://cdn.jsdelivr.net"; - if (CACHE_HOST === "disable") return; + const WIDGET_VERSION = process.env.WIDGET_VERSION || "latest"; + const WASM_VERSION = process.env.WASM_VERSION || "latest"; + try { const [widgetSource, floatingSource, wasmSource, wasmLoaderSource] = await Promise.all([ - fetch(`${CACHE_HOST}/npm/@cap.js/widget@latest`).then((r) => - r.text() + fetch(`${CACHE_HOST}/npm/@cap.js/widget@${WIDGET_VERSION}`).then((r) => r.text()), + fetch(`${CACHE_HOST}/npm/@cap.js/widget@${WIDGET_VERSION}/cap-floating.min.js`).then( + (r) => r.text() + ), + fetch(`${CACHE_HOST}/npm/@cap.js/wasm@${WASM_VERSION}/browser/cap_wasm_bg.wasm`).then( + (r) => r.arrayBuffer() + ), + fetch(`${CACHE_HOST}/npm/@cap.js/wasm@${WASM_VERSION}/browser/cap_wasm.min.js`).then( + (r) => r.text() ), - fetch( - `${CACHE_HOST}/npm/@cap.js/widget/cap-floating.min.js` - ).then((r) => r.text()), - fetch( - `${CACHE_HOST}/npm/@cap.js/wasm/browser/cap_wasm_bg.wasm` - ).then((r) => r.arrayBuffer()), - fetch( - `${CACHE_HOST}/npm/@cap.js/wasm/browser/cap_wasm.min.js` - ).then((r) => r.text()), ]); cacheConfig["lastUpdate"] = currentTime;