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 9a29b19
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/bidi/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ export class BidiPage extends Page {
options: WaitForOptions = {}
): Promise<BidiHTTPResponse | null> {
const [response] = await Promise.all([
this.#frame.waitForNavigation(options),
this.#frame.waitForNavigation({
...options,
ignoreSameDocumentNavigation: true,
}),
this.#frame.browsingContext.reload(),
]).catch(
rewriteNavigationError(
Expand Down
6 changes: 5 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 @@ -218,9 +219,12 @@ export class CdpFrame extends Frame {
waitUntil,
timeout
);
console.log(options.ignoreSameDocumentNavigation);
const error = await Deferred.race([
watcher.terminationPromise(),
watcher.sameDocumentNavigationPromise(),
...(options.ignoreSameDocumentNavigation
? []
: [watcher.sameDocumentNavigationPromise()]),
watcher.newDocumentNavigationPromise(),
]);
try {
Expand Down
20 changes: 20 additions & 0 deletions test/src/navigation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ 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();
expect(
await page.evaluate(() => {
return document;
})
).toBeTruthy();
});
it('should navigate to empty page with networkidle0', async () => {
const {page, server} = await getTestState();

Expand Down

0 comments on commit 9a29b19

Please sign in to comment.