Skip to content

Commit

Permalink
fix: reload should not trip on fragment navigations
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Mar 21, 2024
1 parent b096ffa commit 04333d1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/puppeteer-core/src/api/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export interface WaitForOptions {
* @defaultValue `'load'`
*/
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
/**
* @internal
*/
ignoreSameDocumentNavigation?: boolean;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion packages/puppeteer-core/src/cdp/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export class CdpFrame extends Frame {
options: {
timeout?: number;
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
ignoreSameDocumentNavigation?: boolean;
} = {}
): Promise<HTTPResponse | null> {
const {
Expand All @@ -220,7 +221,9 @@ export class CdpFrame extends Frame {
);
const error = await Deferred.race([
watcher.terminationPromise(),
watcher.sameDocumentNavigationPromise(),
...(options.ignoreSameDocumentNavigation
? []
: [watcher.sameDocumentNavigationPromise()]),
watcher.newDocumentNavigationPromise(),
]);
try {
Expand Down
5 changes: 4 additions & 1 deletion packages/puppeteer-core/src/cdp/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,10 @@ export class CdpPage extends Page {
options?: WaitForOptions
): Promise<HTTPResponse | null> {
const [result] = await Promise.all([
this.waitForNavigation(options),
this.waitForNavigation({
...options,
ignoreSameDocumentNavigation: true,
}),
this.#primaryTargetClient.send('Page.reload'),
]);

Expand Down
21 changes: 21 additions & 0 deletions test/src/navigation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ describe('navigation', function () {
const response = await page.goto(server.PREFIX + '/grid.html');
expect(response!.status()).toBe(200);
});
it('should work when reload causes history API in beforeunload', async () => {
const {page, server} = await getTestState();

await page.goto(server.EMPTY_PAGE);
await page.evaluate(() => {
window.addEventListener(
'beforeunload',
() => {
return history.replaceState(null, 'initial', window.location.href);
},
false
);
});
await page.reload();
// Evaluate still works.
expect(
await page.evaluate(() => {
return 1;
})
).toBe(1);
});
it('should navigate to empty page with networkidle0', async () => {
const {page, server} = await getTestState();

Expand Down

0 comments on commit 04333d1

Please sign in to comment.