Skip to content

Commit

Permalink
Upate intro to "Preventing code from running during prerendering"
Browse files Browse the repository at this point in the history
Co-authored-by: Hamish Willee <[email protected]>
  • Loading branch information
domenic and hamishwillee authored Nov 1, 2024
1 parent 7a2ea85 commit 70f9814
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ A generic {{domxref("Event")}}.

### Preventing code from running during prerendering

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:
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 Down

0 comments on commit 70f9814

Please sign in to comment.