From 7998845c35a98115c334cbd51e1a1c6c40db83ac Mon Sep 17 00:00:00 2001 From: Kuba Sekowski Date: Thu, 3 Dec 2026 09:05:03 +0100 Subject: [PATCH 1/2] Pin the engine the docs' live examples load from jsDelivr The live examples loaded the unpinned `hyperformula` package, which jsDelivr resolves to whatever it last cached as `latest` (up to 12 hours on the CDN, 7 days in a browser). After 4.0.0 shipped, the 4.0.0 pages kept running 3.5.0, which has no license-check skip for handsontable.com, so every example threw. The URL is now pinned to the engine version the docs are built from, read from `hyperformula/package.json`, which `code-freeze` already bumps. Co-Authored-By: Claude Opus 5.5 --- docs/README.md | 2 +- docs/astro.config.mjs | 16 ++++++++++++---- script/release/release-README.md | 6 +++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/docs/README.md b/docs/README.md index e68949215..e7ee88b59 100644 --- a/docs/README.md +++ b/docs/README.md @@ -28,7 +28,7 @@ To start a local HyperFormula docs server: ``` 5. In your browser, go to: http://localhost:4321/docs/hyperformula/. Search works only in a full build (`npm run docs:build`), which indexes the pages with [Pagefind](https://pagefind.app/). -The live examples load HyperFormula from the CDN, and HyperFormula checks the license key everywhere except on handsontable.com. On `localhost`, an example shows `#LIC!` once the CDN serves a version that needs a key. `npm run docs:dev` can't give the examples a key. To run them locally with the internal test key, build with the opt-in and serve the build: +The live examples load HyperFormula from the CDN, at the version in `hyperformula/package.json` (`ENGINE_CDN` in `astro.config.mjs`). The release script bumps that version at the code freeze, before the version is on npm, so until it is published the examples in a build of the release branch fail to load the engine. HyperFormula checks the license key everywhere except on handsontable.com. On `localhost`, an example shows `#LIC!` when the version it loads needs a key. `npm run docs:dev` can't give the examples a key. To run them locally with the internal test key, build with the opt-in and serve the build: ```bash DOCS_TEST_LICENSE_KEY=true npm run docs:build diff --git a/docs/astro.config.mjs b/docs/astro.config.mjs index 6ce3f0ba4..1c8623649 100644 --- a/docs/astro.config.mjs +++ b/docs/astro.config.mjs @@ -21,6 +21,12 @@ const normalizeBase = (base) => `/${base.replace(/^\/+|\/+$/g, '')}/`.replace(/^ const base = normalizeBase(process.env.DOCS_BASE || buildConfig.base); const hostname = process.env.DOCS_HOSTNAME || buildConfig.hostname; +/** + * The CDN package the live examples load, at the version of the engine the docs are built from. That version comes + * from `hyperformula/package.json`, which `npm run release -- code-freeze` bumps, so the pin moves with every release. + */ +const ENGINE_CDN = `https://cdn.jsdelivr.net/npm/hyperformula@${DOCS_DATA.version}`; + /** * The image that link previews show for every docs page. Starlight emits `og:title`, `og:description`, and * `twitter:card=summary_large_image` by itself, but no image. This is the handsontable.com default card, which carries @@ -109,10 +115,12 @@ export default defineConfig({ }, plugins: [starlightThemeRapide()], head: [ - // The live examples use the published engine and these globals. - { tag: 'script', attrs: { src: 'https://cdn.jsdelivr.net/npm/hyperformula/dist/hyperformula.full.min.js' } }, - { tag: 'script', attrs: { src: 'https://cdn.jsdelivr.net/npm/hyperformula/dist/languages/enUS.js' } }, - { tag: 'script', attrs: { src: 'https://cdn.jsdelivr.net/npm/hyperformula/dist/languages/frFR.js' } }, + // The live examples use the published engine and these globals. The engine is pinned to the version the + // pages document: an unpinned URL serves whatever jsDelivr last cached as `latest`, for up to 12 hours on the + // CDN and 7 days in a browser, so after a release the pages would run on the previous engine. + { tag: 'script', attrs: { src: `${ENGINE_CDN}/dist/hyperformula.full.min.js` } }, + { tag: 'script', attrs: { src: `${ENGINE_CDN}/dist/languages/enUS.js` } }, + { tag: 'script', attrs: { src: `${ENGINE_CDN}/dist/languages/frFR.js` } }, { tag: 'script', attrs: { src: 'https://cdn.jsdelivr.net/npm/moment/moment.min.js' } }, // Builds the "Source code" tabs of the live examples and toggles them (a copy of the Handsontable docs' script). { tag: 'script', attrs: { src: `${base}example-tabs.js`, defer: true } }, diff --git a/script/release/release-README.md b/script/release/release-README.md index 99f664a7b..112b951a7 100644 --- a/script/release/release-README.md +++ b/script/release/release-README.md @@ -126,7 +126,11 @@ describe decisions made from stale refs. The script marks these fetches the freeze. 3. **Sets the version and the release date** — `version` in `hyperformula/package.json` and `HT_RELEASE_DATE` in `hyperformula/ht.config.js`. Each is skipped when it already matches, - so a re-run does not rewrite files it already wrote. + so a re-run does not rewrite files it already wrote. The `version` is also the + version the docs' live examples load from jsDelivr (`ENGINE_CDN` in + `docs/astro.config.mjs`), so this step moves that pin too. jsDelivr serves a + version only once it is on npm, so until `publish` step 7 the live examples + in a build of `release/`, staging included, fail to load the engine. 4. **Reinstalls dependencies** and regenerates the lock file — skipped when the root `package-lock.json` already records the new version for the `hyperformula` workspace *and* `node_modules` exists. From 7bceb8c97a1c01b8f46b7bb286ba4a0e5a3527d3 Mon Sep 17 00:00:00 2001 From: Kuba Sekowski Date: Thu, 3 Dec 2026 09:11:46 +0100 Subject: [PATCH 2/2] Serve the docs' engine from the build outside production Pinning the CDN URL to the package.json version broke every live example in release-branch builds during a code freeze, because that version is not on npm until publish. Only a production build whose version is on npm now loads the engine from jsDelivr. Every other build, and a production build made before the publish (release.sh pushes master first), serves the engine it was built with from hyperformula/dist. Co-Authored-By: Claude Opus 5.5 --- docs/README.md | 2 +- docs/astro.config.mjs | 17 ++-- docs/src/engine-scripts.mjs | 148 +++++++++++++++++++++++++++++++ docs/src/preview-license-key.mjs | 2 +- script/release/release-README.md | 8 +- 5 files changed, 160 insertions(+), 17 deletions(-) create mode 100644 docs/src/engine-scripts.mjs diff --git a/docs/README.md b/docs/README.md index e7ee88b59..04cad3e47 100644 --- a/docs/README.md +++ b/docs/README.md @@ -28,7 +28,7 @@ To start a local HyperFormula docs server: ``` 5. In your browser, go to: http://localhost:4321/docs/hyperformula/. Search works only in a full build (`npm run docs:build`), which indexes the pages with [Pagefind](https://pagefind.app/). -The live examples load HyperFormula from the CDN, at the version in `hyperformula/package.json` (`ENGINE_CDN` in `astro.config.mjs`). The release script bumps that version at the code freeze, before the version is on npm, so until it is published the examples in a build of the release branch fail to load the engine. HyperFormula checks the license key everywhere except on handsontable.com. On `localhost`, an example shows `#LIC!` when the version it loads needs a key. `npm run docs:dev` can't give the examples a key. To run them locally with the internal test key, build with the opt-in and serve the build: +The live examples on the production site load HyperFormula from the CDN, pinned to the version in `hyperformula/package.json`. Every other build, the dev server included, serves the engine from `hyperformula/dist`, so a preview of the release branch runs the new engine before it is on npm; run `npm run bundle-all` from the repository root first. `src/engine-scripts.mjs` decides which. HyperFormula checks the license key everywhere except on handsontable.com. On `localhost`, an example shows `#LIC!` when the version it loads needs a key. `npm run docs:dev` can't give the examples a key. To run them locally with the internal test key, build with the opt-in and serve the build: ```bash DOCS_TEST_LICENSE_KEY=true npm run docs:build diff --git a/docs/astro.config.mjs b/docs/astro.config.mjs index 1c8623649..65403ed6b 100644 --- a/docs/astro.config.mjs +++ b/docs/astro.config.mjs @@ -10,6 +10,7 @@ import { DOCS_DATA } from './src/docs-data.mjs'; import { docsMarkdownPlugin, legacyAnchorsPlugin, tableWrapperPlugin } from './src/markdown-plugins.mjs'; import { mdCompanions } from './src/md-companions/index.mjs'; import { previewLicenseKey } from './src/preview-license-key.mjs'; +import { engineAssets, engineScripts, resolveUsesCdn } from './src/engine-scripts.mjs'; import { apiPageGroup } from './src/api-sidebar.mjs'; // CommonJS, because the Worker and `script/prepare-cf-assets.js` read it too. @@ -21,11 +22,8 @@ const normalizeBase = (base) => `/${base.replace(/^\/+|\/+$/g, '')}/`.replace(/^ const base = normalizeBase(process.env.DOCS_BASE || buildConfig.base); const hostname = process.env.DOCS_HOSTNAME || buildConfig.hostname; -/** - * The CDN package the live examples load, at the version of the engine the docs are built from. That version comes - * from `hyperformula/package.json`, which `npm run release -- code-freeze` bumps, so the pin moves with every release. - */ -const ENGINE_CDN = `https://cdn.jsdelivr.net/npm/hyperformula@${DOCS_DATA.version}`; +// Where the live examples load the engine from: `src/engine-scripts.mjs`. +const engineFromCdn = await resolveUsesCdn(process.env, DOCS_DATA.version); /** * The image that link previews show for every docs page. Starlight emits `og:title`, `og:description`, and @@ -115,12 +113,8 @@ export default defineConfig({ }, plugins: [starlightThemeRapide()], head: [ - // The live examples use the published engine and these globals. The engine is pinned to the version the - // pages document: an unpinned URL serves whatever jsDelivr last cached as `latest`, for up to 12 hours on the - // CDN and 7 days in a browser, so after a release the pages would run on the previous engine. - { tag: 'script', attrs: { src: `${ENGINE_CDN}/dist/hyperformula.full.min.js` } }, - { tag: 'script', attrs: { src: `${ENGINE_CDN}/dist/languages/enUS.js` } }, - { tag: 'script', attrs: { src: `${ENGINE_CDN}/dist/languages/frFR.js` } }, + // The live examples use the engine and these globals. + ...engineScripts(engineFromCdn, base, DOCS_DATA.version), { tag: 'script', attrs: { src: 'https://cdn.jsdelivr.net/npm/moment/moment.min.js' } }, // Builds the "Source code" tabs of the live examples and toggles them (a copy of the Handsontable docs' script). { tag: 'script', attrs: { src: `${base}example-tabs.js`, defer: true } }, @@ -305,6 +299,7 @@ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= }), mdCompanions({ base, hostname }), // Last, so it reads everything the build wrote. + engineAssets(engineFromCdn, base), previewLicenseKey(process.env), ], }); diff --git a/docs/src/engine-scripts.mjs b/docs/src/engine-scripts.mjs new file mode 100644 index 000000000..e7265ef05 --- /dev/null +++ b/docs/src/engine-scripts.mjs @@ -0,0 +1,148 @@ +/** + * Where the live examples load the engine from. + * + * A build of the production branch loads the published engine from jsDelivr, pinned to the version the docs are built + * from, once that version is on npm. An unpinned URL serves whatever jsDelivr last cached as `latest`, for up to 12 hours on the CDN and 7 days in a + * browser, so after a release the pages would run on the previous engine. The version comes from + * `hyperformula/package.json`, which `npm run release -- code-freeze` bumps, so the pin moves with every release. + * + * Every other build (a preview, staging, a local build, the dev server) serves the engine it was built with, from + * `hyperformula/dist`. During a code freeze the release branch carries a version that is not on npm yet, so jsDelivr + * has nothing to serve for it, and these builds are where the new examples are tested against the new engine. + * + * A production build whose version is not on npm serves its own engine too. `npm run release -- publish` pushes + * `master` before it publishes to npm, so the build that push starts would otherwise point every example at a version + * jsDelivr cannot serve, for as long as the publish takes, or for good if it never happens. + */ +import { copyFileSync, createReadStream, existsSync, mkdirSync } from 'node:fs'; +import { dirname, join, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { PRODUCTION_BRANCH } from './preview-license-key.mjs'; + +const ENGINE_DIST = resolve(fileURLToPath(new URL('.', import.meta.url)), '../../hyperformula/dist'); + +/** + * The files the live examples load, relative to `hyperformula/dist` and to the package root on jsDelivr's `dist/`. + */ +const ENGINE_FILES = ['hyperformula.full.min.js', 'languages/enUS.js', 'languages/frFR.js']; + +/** + * The directory, under the docs base, that a non-production build serves the engine from. + */ +const ENGINE_DIR = 'engine/'; + +/** + * Whether `hyperformula@` is on npm. Anything but a clear yes, a network error included, is a no: serving the + * engine from the build always works, and pointing at a version jsDelivr lacks never does. + * + * @param {string} version The engine version. + * @returns {Promise} + */ +async function isPublished(version) { + try { + const response = await fetch(`https://registry.npmjs.org/hyperformula/${encodeURIComponent(version)}`, { + signal: AbortSignal.timeout(10_000), + }); + + return response.ok; + } catch { + return false; + } +} + +/** + * Whether this build loads the published engine from jsDelivr: a build of the production branch, of a version on npm. + * + * @param {NodeJS.ProcessEnv} env The environment. Workers Builds sets `WORKERS_CI_BRANCH`. + * @param {string} version The engine version the docs are built from. + * @returns {Promise} + */ +export async function resolveUsesCdn(env, version) { + if (env.WORKERS_CI_BRANCH !== PRODUCTION_BRANCH) { + return false; + } + + const published = await isPublished(version); + + if (!published) { + console.warn(`hyperformula@${version} is not on npm, so the live examples serve the engine from hyperformula/dist instead of jsDelivr.`); + } + + return published; +} + +/** + * The `