Skip to content

Commit

Permalink
fix(puppet): stabilize frame loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Nov 23, 2021
1 parent cd4b6af commit 43b104a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
28 changes: 21 additions & 7 deletions puppet-chrome/lib/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export default class Frame extends TypedEventEmitter<IPuppetFrameEvents> impleme
private loaderIdResolvers = new Map<string, IResolvablePromise<Error | null>>();
private readonly devtoolsSession: DevtoolsSession;

private defaultLoaderId: string;

private startedLoaderId: string;
private resolveLoaderTimeout: NodeJS.Timeout;

Expand Down Expand Up @@ -255,11 +257,15 @@ export default class Frame extends TypedEventEmitter<IPuppetFrameEvents> impleme
public onLoaded(internalFrame: PageFrame): void {
this.internalFrame = internalFrame;
this.updateUrl();
this.setLoader(internalFrame.loaderId);
if (internalFrame.loaderId && this.url) {
this.loaderIdResolvers.get(internalFrame.loaderId).resolve();
if (!internalFrame.loaderId) return;

// if we this is the first loader and url is default, this is the first loader
if (this.isDefaultUrl && !this.defaultLoaderId && this.loaderIdResolvers.size === 0) {
this.defaultLoaderId = internalFrame.loaderId;
}
if (internalFrame.loaderId && internalFrame.unreachableUrl) {
this.setLoader(internalFrame.loaderId);

if (this.url || internalFrame.unreachableUrl) {
// if this is a loaded frame, just count it as loaded. it shouldn't fail
this.loaderIdResolvers.get(internalFrame.loaderId).resolve();
}
Expand Down Expand Up @@ -320,8 +326,16 @@ export default class Frame extends TypedEventEmitter<IPuppetFrameEvents> impleme
}

public async waitForLoader(loaderId?: string): Promise<Error | null> {
const hasLoaderError = await this.loaderIdResolvers.get(loaderId ?? this.activeLoaderId)
?.promise;
if (!loaderId) {
loaderId = this.activeLoaderId;
if (loaderId === this.defaultLoaderId) {
// wait for an actual frame to load
const frameLoader = await this.waitOn('frame-loader-created', null, 60e3);
loaderId = frameLoader.loaderId;
}
}

const hasLoaderError = await this.loaderIdResolvers.get(loaderId)?.promise;
if (hasLoaderError) return hasLoaderError;

if (!this.getActiveContextId(false)) {
Expand Down Expand Up @@ -368,7 +382,7 @@ export default class Frame extends TypedEventEmitter<IPuppetFrameEvents> impleme
lifecycle[name] = timestamp ?? Date.now();
}

if (!this.isDefaultUrl) {
if (loaderId !== this.defaultLoaderId) {
this.emit('frame-lifecycle', { frame: this, name, loaderId: pageLoaderId, timestamp });
}
}
Expand Down
2 changes: 2 additions & 0 deletions puppet/test/TestPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export async function goto(
}

export async function setContent(page: IPuppetPage, content: string) {
// @ts-ignore
page.mainFrame.defaultLoaderId = null;
await page.evaluate(`((content) => {
window.stop();
document.open();
Expand Down
4 changes: 2 additions & 2 deletions puppet/test/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('Load test', () => {
page = createTestPage(await context.newPage());
await page.goto(server.url('link.html'));

const navigate = page.mainFrame.waitOn('frame-navigated', null, 60e3);
const navigate = page.mainFrame.waitOn('frame-navigated', null, 75e3);
await page.click('a');
await navigate;
expect(page.mainFrame.url).toBe(`${server.crossProcessBaseUrl}/empty.html`);
Expand All @@ -71,5 +71,5 @@ describe('Load test', () => {
}
});
await Promise.all(concurrent);
}, 60e3);
}, 75e3);
});

0 comments on commit 43b104a

Please sign in to comment.