Skip to content

Commit c04e00a

Browse files
CopilotMSNevhectorhdzg
committed
Add documentation for disablePageUnloadEvents to address jQuery 3.7.1 deprecation warnings (#2533)
* Initial plan for issue * Add documentation for disablePageUnloadEvents to address jQuery 3.7.1 deprecation warnings Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Add detailed documentation for disablePageUnloadEvents in docs folder Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Update documentation URLs and add TypeScript examples for page events config Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Update documentation for disablePageUnloadEvents and disablePageShowEvents Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Add @example tag to TypeDoc for disablePageUnloadEvents and disablePageShowEvents Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Add PageUnloadEvents.html link to docs/README.md FAQ section Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Update disablePageUnloadEvents and disablePageShowEvents links to use TypeDoc URL Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Remove unnecessary lines and "Example usage:" heading Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Update PageUnloadEvents link to use relative markdown path Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Add details about handling missing events in PageUnloadEvents.md Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Update disablePageUnloadEvents and disablePageShowEvents with TypeDoc links in README.md Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Update disablePageUnloadEvents and disablePageShowEvents links to 1ds-core-js specific TypeDoc URLs Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Remove JavaScript examples from PageUnloadEvents.md Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Clarify Page Unload and Visibility Event Handling section in README.md Co-authored-by: hectorhdzg <39923391+hectorhdzg@users.noreply.github.com> * Update README.md to clarify page unload event handling description Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Fix failing tests by correcting AITestClass import in Es5Rollup.Tests.ts Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> * Revert to using "@microsoft/ai-test-framework" as requested Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com> Co-authored-by: hectorhdzg <39923391+hectorhdzg@users.noreply.github.com>
1 parent f5f3d5e commit c04e00a

File tree

6 files changed

+205
-13
lines changed

6 files changed

+205
-13
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,39 @@ Most configuration fields are named such that they can be defaulted to falsey. A
450450
| featureOptIn (#feature)<br/><sub>since 3.0.3</sub> | IFeatureOptIn | undefined | [Optional] Set Feature opt in details. |
451451
| throttleMgrCfg <br/><sub>since 3.0.3</sub> | `{[key: number]: IThrottleMgrConfig}` | undefined | [Optional] Set throttle mgr configuration by key. |
452452
| retryCodes | number[] | undefined | Identifies the status codes that will cause event batches to be resent, when `null` or `undefined` the SDK will use it's defaults `[401, 408, 429, 500, 502, 503, 504]`. `403` was removed in version 3.1.1. |
453+
| [disablePageUnloadEvents](https://microsoft.github.io/ApplicationInsights-JS/webSdk/applicationinsights-core-js/interfaces/IConfiguration.html#disablePageUnloadEvents) | string[] | undefined | [Optional] An array of the page unload events that you would like to be ignored. [See detailed documentation](https://microsoft.github.io/ApplicationInsights-JS/docs/PageUnloadEvents.html). Unload events include "beforeunload", "unload", "visibilitychange" (with 'hidden' state) and "pagehide". This can be used to avoid jQuery 3.7.1+ deprecation warnings by configuring as `disablePageUnloadEvents: ["unload"]`. |
454+
| [disablePageShowEvents](https://microsoft.github.io/ApplicationInsights-JS/webSdk/applicationinsights-core-js/interfaces/IConfiguration.html#disablePageShowEvents) | string[] | undefined | [Optional] An array of page show events that you would like to be ignored. [See detailed documentation](https://microsoft.github.io/ApplicationInsights-JS/docs/PageUnloadEvents.html). Page Show events include "pageshow" and "visibilitychange" (with 'visible' state). |
453455
| expCfg <br/><sub>since 3.3.1</sub>| [`IExceptionConfig`](https://github.com/microsoft/ApplicationInsights-JS/blob/main/shared/AppInsightsCommon/src/Interfaces/IExceptionTelemetry.ts) | undefined | Set additional configuration for exceptions, such as more scripts to include in the exception telemetry. |
454456
457+
### Page Unload and Visibility Event Handling
458+
459+
Application Insights SDK uses page lifecycle events to reliably send telemetry data before your page closes or navigates away. These events are essential for ensuring no telemetry data is lost during navigation, page refreshes, or tab/browser closures.
460+
461+
**What these configurations do:**
462+
- Control which browser events the SDK uses to detect when the browser is about to unload, navigate away, become unresponsive, or get hibernated (especially on mobile)
463+
- Ensure all batched events are sent and not lost due to the browser closing or the user navigating away
464+
- Affect ALL telemetry types (page views, events, dependencies, exceptions, etc.)
465+
- Allow you to avoid deprecated event warnings (from jQuery 3.7.1+ or Chrome) while maintaining functionality
466+
467+
**About visibility state:**
468+
When a browser tab becomes hidden (switching to another tab) or visible (returning to the tab), the browser fires "visibilitychange" events with document.visibilityState changing to "hidden" or "visible". The SDK uses these events to optimize telemetry sending.
469+
470+
```js
471+
const appInsights = new ApplicationInsights({
472+
config: {
473+
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
474+
// Disable the deprecated 'unload' event to avoid jQuery 3.7.1+ deprecation warnings
475+
// This also prevents Chrome's warnings about the unload event
476+
disablePageUnloadEvents: ["unload"],
477+
/* ...Other Configuration Options... */
478+
}
479+
});
480+
appInsights.loadAppInsights();
481+
appInsights.trackPageView();
482+
```
483+
484+
For more detailed information about browser compatibility and configuration options, see the [Page Unload Events documentation](https://microsoft.github.io/ApplicationInsights-JS/docs/PageUnloadEvents.html).
485+
455486
### Feature
456487
457488
You can use the `featureOptIn` configuration to enable or customize specific SDK features.

docs/PageUnloadEvents.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Page Unload Event Handling
2+
3+
## Overview
4+
5+
Application Insights Web SDK tracks several page lifecycle events to ensure that telemetry data is sent before a page is unloaded, helping to prevent data loss during page navigation or browser closure. The SDK hooks into different unload and visibility change events depending on the browser environment.
6+
7+
## Unload Events
8+
9+
The SDK listens to the following events to detect when a page is being unloaded:
10+
11+
1. **beforeunload** - Fired when the window, document, and its resources are about to be unloaded
12+
2. **unload** - Fired when the document or a child resource is being unloaded
13+
3. **pagehide** - Fired when the browser hides the current page in the process of navigating to another page
14+
4. **visibilitychange** (with 'hidden' state) - Fired when the content of a tab has become visible or hidden
15+
16+
## Modern Browser Compatibility
17+
18+
Modern browsers and frameworks are deprecating or changing how some page unload events work:
19+
20+
- **jQuery 3.7.1+** has deprecated the use of the `unload` event, showing warning messages when it's used.
21+
- Some modern browsers are changing the behavior of `beforeunload` event for better performance and reduced tracking potential.
22+
- The `pagehide` event and `visibilitychange` events are becoming the recommended alternatives.
23+
- The SDK is designed to handle cases where certain events are unavailable or behave differently across browsers, gracefully adapting to the environment to ensure telemetry is sent.
24+
25+
## Configuration Options
26+
27+
The SDK provides configuration options to control which page lifecycle events are used:
28+
29+
### disablePageUnloadEvents
30+
31+
This configuration option allows you to specify which page unload events should not be used by the SDK.
32+
33+
**TypeScript Example:**
34+
```typescript
35+
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
36+
37+
const appInsights = new ApplicationInsights({
38+
config: {
39+
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
40+
disablePageUnloadEvents: ["unload"],
41+
/* ...Other Configuration Options... */
42+
}
43+
});
44+
```
45+
46+
**Important notes**:
47+
48+
- The SDK requires at least one functioning page unload event to ensure telemetry is sent when the page is closed.
49+
- If all events are listed in `disablePageUnloadEvents` or if the only working events in the current browser environment are the ones you've disabled, the SDK will still use one of them to ensure functionality.
50+
- This option is especially useful for avoiding jQuery 3.7.1+ deprecation warnings by excluding the "unload" event.
51+
52+
### disablePageShowEvents
53+
54+
Similarly, this configuration option controls which page show events are not used by the SDK. Page show events include:
55+
56+
1. **pageshow** - Fired when a page is shown, or when navigating to a page using browser's back/forward functionality
57+
2. **visibilitychange** (with 'visible' state) - Fired when a tab becomes visible
58+
59+
**TypeScript Example:**
60+
```typescript
61+
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
62+
63+
const appInsights = new ApplicationInsights({
64+
config: {
65+
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
66+
disablePageShowEvents: ["pageshow"],
67+
/* ...Other Configuration Options... */
68+
}
69+
});
70+
```
71+
72+
## Fallback Mechanism
73+
74+
The SDK implements a robust fallback mechanism to ensure that telemetry can be sent before the page unloads:
75+
76+
1. The SDK first tries to use all available unload events except those listed in `disablePageUnloadEvents`.
77+
2. If no events can be registered (either because all are disabled or not supported), the SDK will ignore the `disablePageUnloadEvents` setting and force registration of at least one event.
78+
3. Even when the SDK attempts to hook events that may be missing in certain browsers, it's designed to gracefully handle these cases without errors.
79+
4. The SDK includes internal logic to detect when hooked events aren't firing as expected and will utilize alternative approaches to send telemetry.
80+
5. This ensures that critical telemetry data is always sent, even in constrained environments or when browser implementations change.
81+
82+
## Use Cases
83+
84+
### Avoiding jQuery 3.7.1+ Deprecation Warnings
85+
86+
If you're using jQuery 3.7.1 or newer, you'll encounter deprecation warnings when the 'unload' event is used. Configure the SDK to not use this deprecated event:
87+
88+
**TypeScript Example:**
89+
```typescript
90+
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
91+
92+
const appInsights = new ApplicationInsights({
93+
config: {
94+
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
95+
// Disable the deprecated 'unload' event to avoid jQuery deprecation warnings
96+
disablePageUnloadEvents: ["unload"],
97+
}
98+
});
99+
```
100+
101+
### Optimizing for Modern Browsers
102+
103+
For the best experience in modern browsers, you might want to prioritize newer events:
104+
105+
**TypeScript Example:**
106+
```typescript
107+
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
108+
109+
const appInsights = new ApplicationInsights({
110+
config: {
111+
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
112+
// Use only modern events
113+
disablePageUnloadEvents: ["unload", "beforeunload"],
114+
}
115+
});
116+
```
117+
118+
Note that the SDK will still use an older event if none of the modern events are supported in the browser environment.
119+
120+
### Using Both Configuration Options Together
121+
122+
You can configure both unload and show events simultaneously for fine-grained control:
123+
124+
**TypeScript Example:**
125+
```typescript
126+
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
127+
128+
const appInsights = new ApplicationInsights({
129+
config: {
130+
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
131+
// Disable specific unload events
132+
disablePageUnloadEvents: ["unload"],
133+
// Disable specific show events
134+
disablePageShowEvents: ["pageshow"],
135+
}
136+
});
137+
```
138+
139+
### Progressive Enhancement
140+
141+
The SDK's approach to page lifecycle events reflects a progressive enhancement strategy, ensuring that telemetry works across diverse browser environments while offering configuration options for optimal behavior in modern contexts.

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ES3 support has been removed from the latest version (v3.x), if required [see fo
4949

5050
- [Create an Issue](https://github.com/microsoft/ApplicationInsights-JS/issues/new/choose)
5151
- [ES3 Support](./es3_Support.md)
52+
- [Page Unload Event Handling](./PageUnloadEvents.md)
5253
- [V2 Upgrade Guide](./upgrade/v2_UpgradeGuide.md)
5354
- [V3 Breaking Changes](./upgrade/v3_BreakingChanges.md)
5455

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,11 @@
5050
"@microsoft/rush": "5.153.1",
5151
"@nevware21/grunt-eslint-ts": "^0.2.2",
5252
"@nevware21/grunt-ts-plugin": "^0.4.3",
53-
"@typescript-eslint/eslint-plugin": "^5.46.1",
54-
"@typescript-eslint/parser": "^5.46.1",
5553
"@rollup/plugin-commonjs": "^24.0.0",
5654
"@rollup/plugin-node-resolve": "^15.0.1",
5755
"@rollup/plugin-replace": "^5.0.2",
58-
"rollup": "^3.20.0",
59-
"rollup-plugin-cleanup": "^3.2.1",
60-
"rollup-plugin-sourcemaps": "^0.6.3",
56+
"@typescript-eslint/eslint-plugin": "^5.46.1",
57+
"@typescript-eslint/parser": "^5.46.1",
6158
"archiver": "^5.3.0",
6259
"connect": "^3.7.0",
6360
"eslint": "^7.29.0",
@@ -66,16 +63,19 @@
6663
"eslint-plugin-node": "^11.1.0",
6764
"eslint-plugin-promise": "^5.1.0",
6865
"eslint-plugin-security": "^1.4.0",
66+
"eventemitter2": "6.4.9",
6967
"grunt": "^1.5.3",
7068
"grunt-cli": "^1.4.3",
7169
"grunt-contrib-connect": "^3.0.0",
7270
"grunt-contrib-copy": "^1.0.0",
7371
"grunt-contrib-uglify": "^5.2.1",
74-
"eventemitter2": "6.4.9",
75-
"puppeteer": "24.8.2",
72+
"puppeteer": "^24.8.2",
73+
"request": "^2.88.2",
74+
"rollup": "^3.20.0",
75+
"rollup-plugin-cleanup": "^3.2.1",
76+
"rollup-plugin-sourcemaps": "^0.6.3",
7677
"typedoc": "^0.26.6",
7778
"typescript": "^4.9.3",
78-
"whatwg-fetch": "^3.6.2",
79-
"request": "^2.88.2"
79+
"whatwg-fetch": "^3.6.2"
8080
}
8181
}

shared/1ds-core-js/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ appInsightsCore.initialize(coreConfig, []);
5252
| idLength | [Optional] Identifies the default length used to generate new random session and user id's. Defaults to 22, previous default value was 5 (v2.4.2 or less), if you need to keep the previous maximum length you should set this value to 5. | number<br />Default: 22
5353
| disableEventTimings | [Optional] Disables additional internal event timings that are added during processing of events, the timings are not sent as part telemetry items to the server. | boolean<br/>Default: false
5454
| enableCompoundKey | [Optional] Enables support for objects with compound keys which indirectly represent an object where the "key" of the object contains a "." as part of it's name.<br />Example: <code>event: { "somedata.embeddedvalue": 123 } </code> | boolean<br />Default: false
55-
| disablePageUnloadEvents | [Optional] An array of the page unload events that you would like to be ignored, special note there must be at least one valid unload event hooked, if you list all or the runtime environment only supports a listed "disabled" event it will still be hooked, if required by the SDK.<br /> Unload events include "beforeunload", "unload", "visibilitychange" (with 'hidden' state) and "pagehide"| string[]<br />Default: not specified
56-
| disablePageShowEvents | [Optional] An array of page show events that you would like to be ignored, special note there must be at lease one valid show event hooked, if you list all or the runtime environment only supports a listed (disabled) event it will STILL be hooked, if required by the SDK.<br/> Page Show events include "pageshow" and "visibilitychange" (with 'visible' state)| string[]<br /> Default: not specified
55+
| [disablePageUnloadEvents](https://microsoft.github.io/ApplicationInsights-JS/webSdk/1ds-core-js/interfaces/IConfiguration.html#disablePageUnloadEvents) | [Optional] An array of the page unload events that you would like to be ignored, special note there must be at least one valid unload event hooked, if you list all or the runtime environment only supports a listed "disabled" event it will still be hooked, if required by the SDK.<br /> Unload events include "beforeunload", "unload", "visibilitychange" (with 'hidden' state) and "pagehide". See [Page Unload Events documentation](https://microsoft.github.io/ApplicationInsights-JS/docs/PageUnloadEvents.html) for details.| string[]<br />Default: not specified
56+
| [disablePageShowEvents](https://microsoft.github.io/ApplicationInsights-JS/webSdk/1ds-core-js/interfaces/IConfiguration.html#disablePageShowEvents) | [Optional] An array of page show events that you would like to be ignored, special note there must be at lease one valid show event hooked, if you list all or the runtime environment only supports a listed (disabled) event it will STILL be hooked, if required by the SDK.<br/> Page Show events include "pageshow" and "visibilitychange" (with 'visible' state). See [Page Unload Events documentation](https://microsoft.github.io/ApplicationInsights-JS/docs/PageUnloadEvents.html) for details.| string[]<br /> Default: not specified
5757

5858
### [IPropertyStorageOverride](https://microsoft.github.io/ApplicationInsights-JS/webSdk/1ds-core-js/interfaces/IPropertyStorageOverride.html)
5959

shared/AppInsightsCore/src/JavaScriptSDK.Interfaces/IConfiguration.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,33 @@ export interface IConfiguration {
156156
/**
157157
* [Optional] An array of the page unload events that you would like to be ignored, special note there must be at least one valid unload
158158
* event hooked, if you list all or the runtime environment only supports a listed "disabled" event it will still be hooked, if required by the SDK.
159-
* Unload events include "beforeunload", "unload", "visibilitychange" (with 'hidden' state) and "pagehide"
159+
* Unload events include "beforeunload", "unload", "visibilitychange" (with 'hidden' state) and "pagehide".
160+
*
161+
* This can be used to avoid jQuery 3.7.1+ deprecation warnings and Chrome warnings about the unload event:
162+
* @example
163+
* ```javascript
164+
* {
165+
* disablePageUnloadEvents: ["unload"]
166+
* }
167+
* ```
168+
*
169+
* For more details, see the [Page Unload Events documentation](https://microsoft.github.io/ApplicationInsights-JS/docs/PageUnloadEvents.html).
160170
*/
161171
disablePageUnloadEvents?: string[];
162172

163173
/**
164174
* [Optional] An array of page show events that you would like to be ignored, special note there must be at lease one valid show event
165175
* hooked, if you list all or the runtime environment only supports a listed (disabled) event it will STILL be hooked, if required by the SDK.
166-
* Page Show events include "pageshow" and "visibilitychange" (with 'visible' state)
176+
* Page Show events include "pageshow" and "visibilitychange" (with 'visible' state).
177+
*
178+
* @example
179+
* ```javascript
180+
* {
181+
* disablePageShowEvents: ["pageshow"]
182+
* }
183+
* ```
184+
*
185+
* For more details, see the [Page Unload Events documentation](https://microsoft.github.io/ApplicationInsights-JS/docs/PageUnloadEvents.html).
167186
*/
168187
disablePageShowEvents?: string[];
169188

0 commit comments

Comments
 (0)