fix: make page.goBack work with bfcache in tab mode (#10818)

This commit is contained in:
Alex Rudenko 2023-08-30 16:28:07 +02:00 committed by GitHub
parent a9446f3d43
commit 22daf1861f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 7 deletions

View File

@ -192,7 +192,7 @@ export class FrameManager extends EventEmitter {
}); });
session.on('Page.frameNavigated', event => { session.on('Page.frameNavigated', event => {
this.#frameNavigatedReceived.add(event.frame.id); this.#frameNavigatedReceived.add(event.frame.id);
void this.#onFrameNavigated(event.frame); void this.#onFrameNavigated(event.frame, event.type);
}); });
session.on('Page.navigatedWithinDocument', event => { session.on('Page.navigatedWithinDocument', event => {
this.#onFrameNavigatedWithinDocument(event.frameId, event.url); this.#onFrameNavigatedWithinDocument(event.frameId, event.url);
@ -351,7 +351,7 @@ export class FrameManager extends EventEmitter {
); );
} }
if (!this.#frameNavigatedReceived.has(frameTree.frame.id)) { if (!this.#frameNavigatedReceived.has(frameTree.frame.id)) {
void this.#onFrameNavigated(frameTree.frame); void this.#onFrameNavigated(frameTree.frame, 'Navigation');
} else { } else {
this.#frameNavigatedReceived.delete(frameTree.frame.id); this.#frameNavigatedReceived.delete(frameTree.frame.id);
} }
@ -386,7 +386,10 @@ export class FrameManager extends EventEmitter {
this.emit(FrameManagerEmittedEvents.FrameAttached, frame); this.emit(FrameManagerEmittedEvents.FrameAttached, frame);
} }
async #onFrameNavigated(framePayload: Protocol.Page.Frame): Promise<void> { async #onFrameNavigated(
framePayload: Protocol.Page.Frame,
navigationType: Protocol.Page.NavigationType
): Promise<void> {
const frameId = framePayload.id; const frameId = framePayload.id;
const isMainFrame = !framePayload.parentId; const isMainFrame = !framePayload.parentId;
@ -415,7 +418,7 @@ export class FrameManager extends EventEmitter {
frame = await this._frameTree.waitForFrame(frameId); frame = await this._frameTree.waitForFrame(frameId);
frame._navigated(framePayload); frame._navigated(framePayload);
this.emit(FrameManagerEmittedEvents.FrameNavigated, frame); this.emit(FrameManagerEmittedEvents.FrameNavigated, frame);
frame.emit(FrameEmittedEvents.FrameNavigated); frame.emit(FrameEmittedEvents.FrameNavigated, navigationType);
} }
async #createIsolatedWorld(session: CDPSession, name: string): Promise<void> { async #createIsolatedWorld(session: CDPSession, name: string): Promise<void> {

View File

@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
import Protocol from 'devtools-protocol';
import {HTTPResponse} from '../api/HTTPResponse.js'; import {HTTPResponse} from '../api/HTTPResponse.js';
import {assert} from '../util/assert.js'; import {assert} from '../util/assert.js';
import {Deferred} from '../util/Deferred.js'; import {Deferred} from '../util/Deferred.js';
@ -218,7 +220,10 @@ export class LifecycleWatcher {
this.#checkLifecycleComplete(); this.#checkLifecycleComplete();
} }
#navigated(): void { #navigated(navigationType: Protocol.Page.NavigationType): void {
if (navigationType === 'BackForwardCacheRestore') {
return this.#frameSwapped();
}
this.#checkLifecycleComplete(); this.#checkLifecycleComplete();
} }

View File

@ -179,8 +179,10 @@ export class ChromeLauncher extends ProductLauncher {
'--disable-dev-shm-usage', '--disable-dev-shm-usage',
'--disable-extensions', '--disable-extensions',
// AcceptCHFrame disabled because of crbug.com/1348106. // AcceptCHFrame disabled because of crbug.com/1348106.
'--disable-features=Translate,BackForwardCache,AcceptCHFrame,MediaRouter,OptimizationHints', '--disable-features=Translate,AcceptCHFrame,MediaRouter,OptimizationHints',
...(USE_TAB_TARGET ? [] : ['--disable-features=Prerender2']), ...(USE_TAB_TARGET
? []
: ['--disable-features=Prerender2,BackForwardCache']),
'--disable-hang-monitor', '--disable-hang-monitor',
'--disable-ipc-flooding-protection', '--disable-ipc-flooding-protection',
'--disable-popup-blocking', '--disable-popup-blocking',

View File

@ -3899,6 +3899,12 @@
"parameters": ["firefox", "headful"], "parameters": ["firefox", "headful"],
"expectations": ["PASS", "TIMEOUT"] "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", "testIdPattern": "[prerender.spec] Prerender can navigate to a prerendered page via input",
"platforms": ["darwin", "linux", "win32"], "platforms": ["darwin", "linux", "win32"],

View File

@ -0,0 +1,2 @@
<!DOCTYPE html>
<body>BFCached<a href="target.html">next</a></body>

View File

@ -0,0 +1,2 @@
<!DOCTYPE html>
<body>target</body>

47
test/src/bfcache.spec.ts Normal file
View File

@ -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();
}
});
});