From 22daf1861fc358acf4d84c360049736c22249f92 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Wed, 30 Aug 2023 16:28:07 +0200 Subject: [PATCH] fix: make page.goBack work with bfcache in tab mode (#10818) --- .../puppeteer-core/src/common/FrameManager.ts | 11 +++-- .../src/common/LifecycleWatcher.ts | 7 ++- .../puppeteer-core/src/node/ChromeLauncher.ts | 6 ++- test/TestExpectations.json | 6 +++ test/assets/cached/bfcache/index.html | 2 + test/assets/cached/bfcache/target.html | 2 + test/src/bfcache.spec.ts | 47 +++++++++++++++++++ 7 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 test/assets/cached/bfcache/index.html create mode 100644 test/assets/cached/bfcache/target.html create mode 100644 test/src/bfcache.spec.ts diff --git a/packages/puppeteer-core/src/common/FrameManager.ts b/packages/puppeteer-core/src/common/FrameManager.ts index 8ea77603..aeb8a483 100644 --- a/packages/puppeteer-core/src/common/FrameManager.ts +++ b/packages/puppeteer-core/src/common/FrameManager.ts @@ -192,7 +192,7 @@ export class FrameManager extends EventEmitter { }); session.on('Page.frameNavigated', event => { this.#frameNavigatedReceived.add(event.frame.id); - void this.#onFrameNavigated(event.frame); + void this.#onFrameNavigated(event.frame, event.type); }); session.on('Page.navigatedWithinDocument', event => { this.#onFrameNavigatedWithinDocument(event.frameId, event.url); @@ -351,7 +351,7 @@ export class FrameManager extends EventEmitter { ); } if (!this.#frameNavigatedReceived.has(frameTree.frame.id)) { - void this.#onFrameNavigated(frameTree.frame); + void this.#onFrameNavigated(frameTree.frame, 'Navigation'); } else { this.#frameNavigatedReceived.delete(frameTree.frame.id); } @@ -386,7 +386,10 @@ export class FrameManager extends EventEmitter { this.emit(FrameManagerEmittedEvents.FrameAttached, frame); } - async #onFrameNavigated(framePayload: Protocol.Page.Frame): Promise { + async #onFrameNavigated( + framePayload: Protocol.Page.Frame, + navigationType: Protocol.Page.NavigationType + ): Promise { const frameId = framePayload.id; const isMainFrame = !framePayload.parentId; @@ -415,7 +418,7 @@ export class FrameManager extends EventEmitter { frame = await this._frameTree.waitForFrame(frameId); frame._navigated(framePayload); this.emit(FrameManagerEmittedEvents.FrameNavigated, frame); - frame.emit(FrameEmittedEvents.FrameNavigated); + frame.emit(FrameEmittedEvents.FrameNavigated, navigationType); } async #createIsolatedWorld(session: CDPSession, name: string): Promise { diff --git a/packages/puppeteer-core/src/common/LifecycleWatcher.ts b/packages/puppeteer-core/src/common/LifecycleWatcher.ts index 63db7f57..7a1356b2 100644 --- a/packages/puppeteer-core/src/common/LifecycleWatcher.ts +++ b/packages/puppeteer-core/src/common/LifecycleWatcher.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +import Protocol from 'devtools-protocol'; + import {HTTPResponse} from '../api/HTTPResponse.js'; import {assert} from '../util/assert.js'; import {Deferred} from '../util/Deferred.js'; @@ -218,7 +220,10 @@ export class LifecycleWatcher { this.#checkLifecycleComplete(); } - #navigated(): void { + #navigated(navigationType: Protocol.Page.NavigationType): void { + if (navigationType === 'BackForwardCacheRestore') { + return this.#frameSwapped(); + } this.#checkLifecycleComplete(); } diff --git a/packages/puppeteer-core/src/node/ChromeLauncher.ts b/packages/puppeteer-core/src/node/ChromeLauncher.ts index 17002161..dff133b7 100644 --- a/packages/puppeteer-core/src/node/ChromeLauncher.ts +++ b/packages/puppeteer-core/src/node/ChromeLauncher.ts @@ -179,8 +179,10 @@ export class ChromeLauncher extends ProductLauncher { '--disable-dev-shm-usage', '--disable-extensions', // AcceptCHFrame disabled because of crbug.com/1348106. - '--disable-features=Translate,BackForwardCache,AcceptCHFrame,MediaRouter,OptimizationHints', - ...(USE_TAB_TARGET ? [] : ['--disable-features=Prerender2']), + '--disable-features=Translate,AcceptCHFrame,MediaRouter,OptimizationHints', + ...(USE_TAB_TARGET + ? [] + : ['--disable-features=Prerender2,BackForwardCache']), '--disable-hang-monitor', '--disable-ipc-flooding-protection', '--disable-popup-blocking', diff --git a/test/TestExpectations.json b/test/TestExpectations.json index dea439db..b4a1029f 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -3899,6 +3899,12 @@ "parameters": ["firefox", "headful"], "expectations": ["PASS", "TIMEOUT"] }, + { + "testIdPattern": "[bfcache.spec] *", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["webDriverBiDi"], + "expectations": ["SKIP"] + }, { "testIdPattern": "[prerender.spec] Prerender can navigate to a prerendered page via input", "platforms": ["darwin", "linux", "win32"], diff --git a/test/assets/cached/bfcache/index.html b/test/assets/cached/bfcache/index.html new file mode 100644 index 00000000..3d793128 --- /dev/null +++ b/test/assets/cached/bfcache/index.html @@ -0,0 +1,2 @@ + +BFCachednext diff --git a/test/assets/cached/bfcache/target.html b/test/assets/cached/bfcache/target.html new file mode 100644 index 00000000..eafc537b --- /dev/null +++ b/test/assets/cached/bfcache/target.html @@ -0,0 +1,2 @@ + +target diff --git a/test/src/bfcache.spec.ts b/test/src/bfcache.spec.ts new file mode 100644 index 00000000..136a1cbd --- /dev/null +++ b/test/src/bfcache.spec.ts @@ -0,0 +1,47 @@ +/** + * Copyright 2023 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import expect from 'expect'; + +import {launch} from './mocha-utils.js'; + +describe('BFCache', function () { + it('can navigate to a BFCached page', async () => { + const {httpsServer, page, close} = await launch({ + ignoreHTTPSErrors: true, + }); + + try { + page.setDefaultTimeout(2000); + + await page.goto(httpsServer.PREFIX + '/cached/bfcache/index.html'); + + await Promise.all([page.waitForNavigation(), page.locator('a').click()]); + + expect(page.url()).toContain('target.html'); + + await Promise.all([page.waitForNavigation(), page.goBack()]); + + expect( + await page.evaluate(() => { + return document.body.innerText; + }) + ).toBe('BFCachednext'); + } finally { + await close(); + } + }); +});