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]); + }); +});