url-changed
TypeScript icon, indicating that this package has built-in type declarations

4.1.0 • Public • Published

url-changed

URL change detection for when Navigation API is not supported.

Install

npm install url-changed

Usage

import { urlChanged } from 'url-changed';

const settings = {
  // see below
};

const stopWatching = urlChanged(
  (newUrl, oldUrl) => console.log(`${oldUrl} -> ${newUrl}`),
  settings
);

Settings

If the Navigation API is available, it is used exclusively for URL change detection. Otherwise one or more fallbacks are used to achieve a similar effect.

The 2nd parameter to urlChanged specifies which fallbacks to enable. All fallbacks are disabled by default but at least one fallback must be enabled.

Body mutation

Any time the <body>'s subtree changes, check if the url has changed.

urlChanged(fn, {
  bodyMutation: true
});

hashchange

When hashchange event fires on window, check if the url has changed.

urlChanged(fn, {
  hashchange: true
});

popstate

When popstate event fires on window, check if the url has changed.

urlChanged(fn, {
  popstate: true
});

Polling

Every x milliseconds, check if the url has changed. This is relatively brute force.

urlChanged(fn, {
  // milliseconds, must be > 0
  poll: 100
});

Ignore Navigation API

Use fallbacks regardless of Navigation API existence. Use this to test fallback behavior.

urlChanged(fn, {
  forceFallbacks: true,

  // Must enable at least one fallback
  bodyMutation: true
});

License

MIT

Package Sidebar

Install

npm i url-changed

Weekly Downloads

3

Version

4.1.0

License

MIT

Unpacked Size

15.4 kB

Total Files

8

Last publish

Collaborators

  • psalaets