Skip to content

Commit

Permalink
Capture navigationType for the measurement in question
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hyndman committed Apr 10, 2023
1 parent ba2bc2d commit a47bdd1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ export type Metric = {
// (this can be either a mutation or a load event target, whichever
// occurred last)
lastVisibleChange?: HTMLElement | TimestampedMutationRecord;

// describes how the navigation being measured was initiated
navigationType: // Navigation started by clicking a link, entering the URL in the browser's address bar or form submission.
| 'navigate'
// Navigation is through the browser's reload operation.
| 'reload'
// Navigation is through the browser's history traversal operation.
| 'back_forward'
// Navigation is initiated by a prerender hint.
| 'prerender'
// Navigation was triggered with a script operation, e.g. in a single page application.
| 'script';
};
};
```
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const init = (options?: TtvcOptions) => {
window.addEventListener('pageshow', (event) => {
// abort if this is the initial pageload
if (!event.persisted) return;
void calculator.start(event.timeStamp);
void calculator.start(event.timeStamp, true);
});
};

Expand Down
26 changes: 25 additions & 1 deletion src/visuallyCompleteCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import {requestAllIdleCallback} from './requestAllIdleCallback';
import {InViewportImageObserver} from './inViewportImageObserver';
import {Logger} from './util/logger';

export type NavigationType =
// Navigation started by clicking a link, entering the URL in the browser's address bar or form submission.
| 'navigate'
// Navigation is through the browser's reload operation.
| 'reload'
// Navigation is through the browser's history traversal operation.
| 'back_forward'
// Navigation is initiated by a prerender hint.
| 'prerender'
// Navigation was triggered with a script operation, e.g. in a single page application.
| 'script';

export type Metric = {
// time since timeOrigin that the navigation was triggered
// (this will be 0 for the initial pageload)
Expand All @@ -22,6 +34,8 @@ export type Metric = {

// the most recent visual update; this can be either a mutation or a load event target
lastVisibleChange?: HTMLElement | TimestampedMutationRecord;

navigationType: NavigationType;
};
};

Expand Down Expand Up @@ -82,7 +96,7 @@ class VisuallyCompleteCalculator {
}

/** begin measuring a new navigation */
async start(start = 0) {
async start(start = 0, isBfCacheRestore = false) {
const navigationIndex = (this.navigationCount += 1);
this.activeMeasurementIndex = navigationIndex;
Logger.info('VisuallyCompleteCalculator.start()');
Expand Down Expand Up @@ -115,12 +129,22 @@ class VisuallyCompleteCalculator {
// identify timestamp of last visible change
const end = Math.max(start, this.lastImageLoadTimestamp, this.lastMutation?.timestamp ?? 0);

const navigationEntries = performance.getEntriesByType(
'navigation'
) as PerformanceNavigationTiming[];
const navigationType = isBfCacheRestore
? 'back_forward'
: start !== 0
? 'script'
: navigationEntries[navigationEntries.length - 1].type;

// report result to subscribers
this.next({
start,
end,
duration: end - start,
detail: {
navigationType,
didNetworkTimeOut,
lastVisibleChange:
this.lastImageLoadTimestamp > (this.lastMutation?.timestamp ?? 0)
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/bfcache/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test.describe('TTVC', () => {
expect(entries.length).toBe(1);
expect(entries[0].duration).toBeGreaterThanOrEqual(PAGELOAD_DELAY);
expect(entries[0].duration).toBeLessThanOrEqual(PAGELOAD_DELAY + FUDGE);
expect(entries[0].detail.navigationType).toBe('navigate');

await page.goto(`/test/bfcache/about?delay=${PAGELOAD_DELAY}&cache=true`, {
waitUntil: 'networkidle',
Expand All @@ -26,6 +27,7 @@ test.describe('TTVC', () => {
expect(entries.length).toBe(1);
expect(entries[0].duration).toBeGreaterThanOrEqual(PAGELOAD_DELAY);
expect(entries[0].duration).toBeLessThanOrEqual(PAGELOAD_DELAY + FUDGE);
expect(entries[0].detail.navigationType).toBe('navigate');

await page.goBack({waitUntil: 'networkidle'});

Expand All @@ -34,5 +36,6 @@ test.describe('TTVC', () => {
// note: webkit clears previous values from this list on page restore
expect(entries[entries.length - 1].duration).toBeGreaterThanOrEqual(0);
expect(entries[entries.length - 1].duration).toBeLessThanOrEqual(FUDGE);
expect(entries[entries.length - 1].detail.navigationType).toBe('back_forward');
});
});

0 comments on commit a47bdd1

Please sign in to comment.