# page-lifecycle **Repository Path**: mirrors_magic-akari/page-lifecycle ## Basic Information - **Project Name**: page-lifecycle - **Description**: PageLifecycle.js is a tiny JavaScript library that allows developers to easily observe Page Lifecycle API state changes cross browser - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-10-22 - **Last Updated**: 2026-09-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # PageLifecycle.js - [Overview](#overview) - [Installation](#installation) - [Usage](#usage) - [API](#api) - [Properties](#properties) - [Methods](#methods) - [Events](#events) - [Browser Support](#browser-support) ## Overview PageLifecycle.js is a tiny JavaScript library (<1K gzipped) that allows developers to easily observe [Page Lifecycle API](https://developers.google.com/web/updates/2018/07/page-lifecycle-api) state changes and implement [Page Lifecycle best practices](https://developers.google.com/web/updates/2018/07/page-lifecycle-api#developer-recommendations-for-each-state) consistently across all browsers. **▶️ View demo: [page-lifecycle.glitch.me](https://page-lifecycle.glitch.me/) 👀** ## Installation You can install this library from npm by running: ```sh npm install --save-dev page-lifecycle ``` ## Usage Releases of this library include three minified, production-ready versions: **1. ES5: [`dist/lifecycle.es5.js`](/dist/lifecycle.es5.js)** (UMD) ⭐ Use this version for maximum compatibility with legacy browsers (that can't run ES2015+ code). As a UMD bundle, it can be required in CommonJS or AMD environments, or it can be loaded with a script tag as the browser global `lifecycle`. ```html ``` **2. ES2015: [`dist/lifecycle.mjs`](/dist/lifecycle.mjs)** (ESM) 🔥 Use this version if you only support ES module-capable browsers or if you're using ` ``` **3. ES2015 (native): [`dist/lifecycle.native.mjs`](/dist/lifecycle.native.mjs)** (ESM w/o `EventTarget` and `Event` shims) ⚠️ Use this version if you're only targeting browsers that [support extending `EventTarget` and `Event` constructors](https://www.chromestatus.com/features/5721972856061952). *Note: this version is the smallest but will only work in some browsers. The implementation instructions are the same as the ES2015 version above.* ## API The PageLifecycle.js library exports a `lifecycle` object, which is a singleton instance of the [`Lifecycle`](/src/Lifecycle.mjs) class. The `Lifecycle` class has the following properties, methods, and events: ### Properties
| Name | Type | Description |
|---|---|---|
state |
string |
Returns the current Page Lifecycle state. |
pageWasDiscarded |
boolean |
Returns the value of document.wasDiscarded (or false if not present).
|
| Name | Description |
|---|---|
addEventListener |
Parameters:
Adds a callback function to be invoked whenever the passed event type is detected. (Note: at the moment only the "statechange" event type is supported.) |
removeEventListener |
Parameters:
Removes a function from the current list of listeners for the passed event type. (Note: at the moment only the "statechange" event type is supported.) |
addUnsavedChanges |
Parameters:
Adds an item to an internal pending-changes stack. Calling this method adds a generic The argument passed should be unique to this state, as it can only be removed by passing the same argument to |
removeUnsavedChanges |
Parameters:
Removes an item matching the passed argument from an internal pending-changes stack. If the stack is empty, the generic |
| Name | Description |
|---|---|
statechange |
Properties:
The |
![]() ✔ |
![]() ✔ |
![]() ✔ |
![]() ✔ |
![]() 9+ |
![]() ✔ |