Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not use loaderId for lifecycle events #8395

Merged
merged 1 commit into from May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/common/FrameManager.ts
Expand Up @@ -194,17 +194,15 @@ export class FrameManager extends EventEmitter {
} = options;

const watcher = new LifecycleWatcher(this, frame, waitUntil, timeout);
let ensureNewDocumentNavigation = false;
let error = await Promise.race([
navigate(this._client, url, referer, frame._id),
watcher.timeoutOrTerminationPromise(),
]);
if (!error) {
error = await Promise.race([
watcher.timeoutOrTerminationPromise(),
ensureNewDocumentNavigation
? watcher.newDocumentNavigationPromise()
: watcher.sameDocumentNavigationPromise(),
watcher.newDocumentNavigationPromise(),
watcher.sameDocumentNavigationPromise(),
]);
}
watcher.dispose();
Expand All @@ -223,7 +221,6 @@ export class FrameManager extends EventEmitter {
referrer,
frameId,
});
ensureNewDocumentNavigation = !!response.loaderId;
return response.errorText
? new Error(`${response.errorText} at ${url}`)
: null;
Expand Down
26 changes: 13 additions & 13 deletions src/common/LifecycleWatcher.ts
Expand Up @@ -66,7 +66,6 @@ export class LifecycleWatcher {
_timeout: number;
_navigationRequest: HTTPRequest | null = null;
_eventListeners: PuppeteerEventListener[];
_initialLoaderId: string;

_sameDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
_sameDocumentNavigationPromise = new Promise<Error | undefined>((fulfill) => {
Expand Down Expand Up @@ -94,6 +93,7 @@ export class LifecycleWatcher {

_maximumTimer?: NodeJS.Timeout;
_hasSameDocumentNavigation?: boolean;
_newDocumentNavigation?: boolean;
_swapped?: boolean;

constructor(
Expand All @@ -112,7 +112,6 @@ export class LifecycleWatcher {

this._frameManager = frameManager;
this._frame = frame;
this._initialLoaderId = frame._loaderId;
this._timeout = timeout;
this._eventListeners = [
helper.addEventListener(
Expand All @@ -133,6 +132,11 @@ export class LifecycleWatcher {
FrameManagerEmittedEvents.FrameNavigatedWithinDocument,
this._navigatedWithinDocument.bind(this)
),
helper.addEventListener(
this._frameManager,
FrameManagerEmittedEvents.FrameNavigated,
this._navigated.bind(this)
),
helper.addEventListener(
this._frameManager,
FrameManagerEmittedEvents.FrameSwapped,
Expand Down Expand Up @@ -211,6 +215,12 @@ export class LifecycleWatcher {
this._checkLifecycleComplete();
}

_navigated(frame: Frame): void {
if (frame !== this._frame) return;
this._newDocumentNavigation = true;
this._checkLifecycleComplete();
}

_frameSwapped(frame: Frame): void {
if (frame !== this._frame) return;
this._swapped = true;
Expand All @@ -221,19 +231,9 @@ export class LifecycleWatcher {
// We expect navigation to commit.
if (!checkLifecycle(this._frame, this._expectedLifecycle)) return;
this._lifecycleCallback();
if (
this._frame._loaderId === this._initialLoaderId &&
!this._hasSameDocumentNavigation
) {
if (this._swapped) {
this._swapped = false;
this._newDocumentNavigationCompleteCallback();
}
return;
}
if (this._hasSameDocumentNavigation)
this._sameDocumentNavigationCompleteCallback();
if (this._frame._loaderId !== this._initialLoaderId)
if (this._swapped || this._newDocumentNavigation)
this._newDocumentNavigationCompleteCallback();

/**
Expand Down