Skip to content

Commit

Permalink
Add an example of measuring prerendering activations (#36526)
Browse files Browse the repository at this point in the history
* Add an example of tracking prerendering activations

We found some web developers attempting to use the pattern for delaying code during prerendering for tracking prerendering activations, which does not work well. Add an explicit example of how to do the latter.

* Apply linter suggestion

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Tracking -> Measuring

Co-authored-by: Barry Pollard <[email protected]>

* Change wording per Barry's suggestion

Co-authored-by: Barry Pollard <[email protected]>

* Upate intro to "Preventing code from running during prerendering"

Co-authored-by: Hamish Willee <[email protected]>

* Move the note between examples

Co-authored-by: Hamish Willee <[email protected]>

* Remove trailing whitespace

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Also update document.prerendering page

* Try to fix the build

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Barry Pollard <[email protected]>
Co-authored-by: Hamish Willee <[email protected]>
Co-authored-by: Brian Thomas Smith <[email protected]>
  • Loading branch information
5 people authored Nov 1, 2024
1 parent 831041c commit 420ee5d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
21 changes: 20 additions & 1 deletion files/en-us/web/api/document/prerendering/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ When a prerendered document is activated, {{domxref("PerformanceNavigationTiming
function pagePrerendered() {
return (
document.prerendering ||
self.performance?.getEntriesByType?.("navigation")[0]?.activationStart > 0
performance.getEntriesByType("navigation")[0]?.activationStart > 0
);
}
```
Expand All @@ -52,6 +52,24 @@ if (document.prerendering) {
> [!NOTE]
> See the [Speculation Rules API](/en-US/docs/Web/API/Speculation_Rules_API) landing page and particularly the [Unsafe speculative loading conditions](/en-US/docs/Web/API/Speculation_Rules_API#unsafe_speculative_loading_conditions) section for more information on the kinds of activities you might wish to delay.
To measure how often a prerender is activated, combine all three APIs: `document.prerendering` to detect cases where the page is currently prerendering, `prerenderingchange` to watch for activations in that case, and `activationStart` to check for cases where the page was prerendered in the past.
```js
if (document.prerendering) {
document.addEventListener(
"prerenderingchange",
() => {
console.log("Prerender activated after this script ran");
},
{ once: true },
);
} else if (performance.getEntriesByType("navigation")[0]?.activationStart > 0) {
console.log("Prerender activated before this script ran");
} else {
console.log("This page load was not via prerendering");
}
```
## Specifications
{{Specifications}}
Expand All @@ -64,3 +82,4 @@ if (document.prerendering) {
- [Speculation Rules API](/en-US/docs/Web/API/Speculation_Rules_API)
- {{domxref("Document.prerenderingchange_event", "prerenderingchange")}} event
- {{domxref("PerformanceNavigationTiming.activationStart")}} property
32 changes: 31 additions & 1 deletion files/en-us/web/api/document/prerenderingchange_event/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ A generic {{domxref("Event")}}.

## Examples

The following code sets up an event listener to run a function once prerendering has finished, on a prerendered page (the prerendering is detected via {{domxref("Document.prerendering")}}), or runs it immediately on a non-prerendered page:
### Preventing code from running during prerendering

The example shows how to defer code, that would otherwise run during prerendering, until after page activation.
This is useful for deferring analytics code, which is only relevant when and if the page is actually viewed.

The code checks if prerendering is running using {{domxref("Document.prerendering")}}, and if so adds an event listener to run an analytics initialization function once the page is activated.
On a page that is not prerendering the analytics code is run immediately.

```js
if (document.prerendering) {
Expand All @@ -40,9 +46,32 @@ if (document.prerendering) {
}
```

Note that this kind of code should not be used for measuring how often a prerender is activated, because the code may run after a prerendered page has already activated.

> [!NOTE]
> See the [Speculation Rules API](/en-US/docs/Web/API/Speculation_Rules_API) landing page and particularly the [Unsafe speculative loading conditions](/en-US/docs/Web/API/Speculation_Rules_API#unsafe_speculative_loading_conditions) section for more information on the kinds of activities you might wish to delay until after prerendering has finished.
### Measuring prerendering activations

This code shows how to measure how often a prerender is activated.
It uses the `prerenderingchange` to track activation events, and {{domxref("Performance.getEntriesByType()")}} to track navigation activations.

```js
if (document.prerendering) {
document.addEventListener(
"prerenderingchange",
() => {
console.log("Prerender activated after this script ran");
},
{ once: true },
);
} else if (performance.getEntriesByType("navigation")[0]?.activationStart > 0) {
console.log("Prerender activated before this script ran");
} else {
console.log("This page load was not via prerendering");
}
```
## Specifications
{{Specifications}}
Expand All @@ -55,3 +84,4 @@ if (document.prerendering) {
- [Speculation Rules API](/en-US/docs/Web/API/Speculation_Rules_API)
- {{domxref("Document.prerendering", "prerendering")}} property
- {{domxref("PerformanceNavigationTiming.activationStart")}} property

0 comments on commit 420ee5d

Please sign in to comment.