From 6bddc5ec392135f753a6dadd25f9d3b02ea8dc8f Mon Sep 17 00:00:00 2001 From: Zachary Miller Date: Mon, 16 Nov 2026 10:30:14 -0500 Subject: [PATCH] Add Pace.version, run only the first copy of pace on a page - Pace.version is the package version. The build stamps it from package.json into pace.js, as it already does for the header, so CI's check that the build is up to date also catches a mismatch. - Pace.running is read-only: an accessor, so assigning to it does nothing (or throws in strict-mode code) instead of changing what pace reports. - Pace.bar, Pace.sources and Pace.bindings are removed: they exposed pace's internals. The event handlers live in pace's closure. - A second copy of pace on a page (another script tag or bundle) logs a warning and does nothing else; a bundled one exports the first copy. Before, it merged into window.Pace and wrapped XMLHttpRequest, fetch and history again, so the page showed two bars. The first copy is found through window[Symbol.for('pace-js')], which page markup can't replace (element names are strings), or, for Pace 1.x, which doesn't set it, a window.Pace with a start function. A copy registers itself only once it's complete. zone.js and Sentry also let the first copy win; Datadog and Lit warn about a second one. - Any other window.Pace, such as an element with id="Pace", is replaced. Before, pace added its functions to it, and a cross-origin frame named Pace stopped pace at load. --- README.md | 11 +++++ pace.js | 100 ++++++++++++++++++++++++++----------- pace.min.js | 2 +- scripts/build.mjs | 24 +++++---- tests/properties.spec.mjs | 101 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 200 insertions(+), 38 deletions(-) create mode 100644 tests/properties.spec.mjs diff --git a/README.md b/README.md index 8dbda83..22ea6ab 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,17 @@ whenever `pushState` or `replaceState` is called by default. - `Pace.ignore`: Explicitly ignore one or more requests, see Tracking below +And these properties: + +- `Pace.running`: Whether a run is in progress, from `Pace.start()` until the bar is hidden or `Pace.stop()`. Read-only: +assigning to it does nothing, or throws a TypeError in strict-mode code such as modules. + +- `Pace.version`: The version of pace.js, such as `'2.0.0'`. + +If a page loads pace.js again after it's already there (a second script tag or bundle, or after Pace 1.x), the later +copy only logs a warning and the first one keeps running. Pace 1.x loaded after this version can't tell, so load only +one copy. + Events ------ diff --git a/pace.js b/pace.js index 4a923e3..05666bf 100644 --- a/pace.js +++ b/pace.js @@ -4,6 +4,46 @@ * Licensed MIT © HubSpot, Inc. and Zachary Miller */ (function () { + const VERSION = '1.3.1'; + + // Hands `value` to a module loader. False when pace was loaded by a ' }); + await idle(page); + expect(logged).toEqual([`Pace: pace.js ${version} was loaded after another copy (${version}); only the first copy runs.`]); + await clearEvents(page); + await request(page, { ms: 900 }); + await idle(page); + expect(await page.evaluate(() => events)).toEqual(RESTARTED); + await expect(page.locator('.pace')).toHaveCount(1); + }); + + test('leaves the first copy and its hooks alone', async ({ page, open }) => { + await open({ + head: NO_EVENT_LAG + ` +`, + }); + expect(await page.evaluate(() => { + const hooks = [XMLHttpRequest.prototype.open, XMLHttpRequest.prototype.send, window.fetch, window.WebSocket, history.pushState, history.replaceState]; + return window.Pace === first.Pace && hooks.every((hook, i) => hook === first.hooks[i]); + })).toBe(true); + }); + + test('finds the first copy even when window.Pace no longer points to it', async ({ page, open }) => { + const logged = warnings(page); + await open({ head: NO_EVENT_LAG + '' }); + await page.waitForFunction(() => document.querySelector('.pace.pace-inactive')); + await expect(page.locator('.pace')).toHaveCount(1); + expect(logged).toEqual([`Pace: pace.js ${version} was loaded after another copy (${version}); only the first copy runs.`]); + }); + + test('an older pace already on the page counts as a first copy', async ({ page, open }) => { + // Pace 1.x puts an object with a start function on window.Pace. + const logged = warnings(page); + await open({ head: '' }); + await page.waitForTimeout(500); + await expect(page.locator('.pace')).toHaveCount(0); + expect(await page.evaluate(() => typeof Pace.getOptions)).toBe('undefined'); + expect(logged).toEqual([`Pace: pace.js ${version} was loaded after another copy; only the first copy runs.`]); + }); + + test('loaded with a bundler, exports the first copy', async ({ page, open }) => { + await open({ head: NO_EVENT_LAG + '' }); + expect(await page.evaluate(() => [module.exports === window.Pace, module.exports.version])).toEqual([true, version]); + }); +});