Skip to content

Add Pace.version, run only the first copy of pace on a page - #580

Open
CodeByZach wants to merge 2 commits into
candidate-2.0from
small-properties
Open

CodeByZach wants to merge 2 commits into
candidate-2.0from
small-properties

Conversation

@CodeByZach

Copy link
Copy Markdown
Owner

Adds Pace.version, makes Pace.running read-only, removes internal properties, and runs only the first copy of pace on a page.

  • Pace.version: the package version, stamped into pace.js by the build from package.json.
  • Pace.running is read-only.
  • Removed: Pace.bar, Pace.sources and Pace.bindings, which exposed pace's internals.
  • A second copy of pace (another script tag or bundle, or one loaded after Pace 1.x) logs a warning and does nothing else; a bundled one exports the first copy. Before, a page that loaded pace twice showed two bars and tracked every request twice. The first copy is found through window[Symbol.for('pace-js')], which page markup can't replace.
  • 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.

Tests cover each property and each case of a second copy; all 10 fail on the previous code.

- 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.
- An extra source that throws while pace constructs it is reported like
  an uncaught error (reportError), and the run goes on without it. The
  errors are reported once the run is set up, so an error listener that
  calls Pace.start() doesn't start a second run. Before, Pace.start()
  threw, and if a run was finishing, Pace.running stayed true and <body>
  kept pace-running for good. This also covers values that pass the
  constructor check but still throw, such as Symbol.
- The bar is added once <body> exists, and Pace.stop() works before it
  does. A target that exists before <body>, such as 'html' with pace in
  <head>, threw at load and left Pace.running true; history.pushState
  called from <head> after pace made the restart throw, so the page
  never showed the bar.
- README: pace.min.js is about 5.7 kB gzipped; it said about 4 kB, which
  stopped being true as the file grew. The smallest theme is 0.3 kB, not
  0.2. Pace.getOptions() returns the current settings; a bar that is
  showing keeps the non-request settings it started with. progress fires
  up to about 30 times a second, and restart also fires on pushState and
  replaceState.
- The warnings for the version 1 eventLag and elements settings also say
  that their tuning settings and checkInterval were removed.

Corrects earlier commit messages:
- f46eecd: a frozen RegExp with the g flag made send() throw inside
  RegExp.prototype.test, which writes lastIndex; pace's own reset of
  lastIndex did nothing. A <form id="paceOptions"> stopped the bar only
  when it had a control named after a setting, such as target.
- 6bddc5e: in Sentry, the first copy sets the shared version, but each
  version keeps its own state. Datadog's second copy warns, then replaces
  the global. Lit warns only in development builds.
- 6a2c31b: a library that patched XMLHttpRequest.prototype after pace
  patched the plain prototype object of pace's wrapper function, so it
  never saw a request. WebSocket is still replaced, by a subclass, so
  instanceof, its constants and prototype patches keep working.
  Reopening a request, and sending it again from its own load handler,
  already worked before that commit.
- b17e3c0, and the matching code comment: setTimeout treats Infinity as
  0 but wraps larger numbers around 2^32 (2^32 + 300 ms fires after
  about 300 ms), so only 0 to 2^31 - 1 means the delay given.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant