4.1.0 • Published 11 months ago

url-changed v4.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

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

4.1.0

11 months ago

4.0.0

11 months ago

3.0.0

11 months ago

2.0.0

11 months ago

1.0.0

11 months ago