diff --git a/docshell/base/CanonicalBrowsingContext.cpp b/docshell/base/CanonicalBrowsingContext.cpp
index 91d5fe9c963c..d9da707c45da 100644
--- a/docshell/base/CanonicalBrowsingContext.cpp
+++ b/docshell/base/CanonicalBrowsingContext.cpp
@@ -559,6 +559,15 @@ void CanonicalBrowsingContext::SessionHistoryCommit(uint64_t aLoadId,
bool addEntry = ShouldUpdateSessionHistory(aLoadType);
if (IsTop()) {
+ if (mActiveEntry && !mActiveEntry->GetFrameLoader()) {
+ bool sharesDocument = true;
+ mActiveEntry->SharesDocumentWith(newActiveEntry, &sharesDocument);
+ if (!sharesDocument) {
+ // If the old page won't be in the bfcache,
+ // clear the dynamic entries.
+ RemoveDynEntriesFromActiveSessionHistoryEntry();
+ }
+ }
mActiveEntry = newActiveEntry;
if (LOAD_TYPE_HAS_FLAGS(aLoadType,
diff --git a/docshell/test/navigation/file_bug1326251.html b/docshell/test/navigation/file_bug1326251.html
index f252df98bfbf..57a81f46f141 100644
--- a/docshell/test/navigation/file_bug1326251.html
+++ b/docshell/test/navigation/file_bug1326251.html
@@ -4,6 +4,21 @@
Bug 1326251
diff --git a/docshell/test/navigation/test_bug1326251.html b/docshell/test/navigation/test_bug1326251.html
index 317b881436fb..3c951729e6f4 100644
--- a/docshell/test/navigation/test_bug1326251.html
+++ b/docshell/test/navigation/test_bug1326251.html
@@ -19,17 +19,26 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=
var testCount = 0;
- SimpleTest.waitForExplicitFinish();
-
- var testWindow;
- function runTest() {
- testWindow = window.open("file_bug1326251.html", "", "width=360,height=480");
- testWindow.onunload = function() { }; // to prevent bfcache
+ var bc = new BroadcastChannel("file_bug1326251");
+ bc.onmessage = function(event) {
+ if (event.data == "requestNextTest") {
+ bc.postMessage({ nextTest: testCount++ });
+ } else if (event.data.type == "is") {
+ is(event.data.value1, event.data.value2, event.data.message);
+ } else if (event.data.type == "ok") {
+ ok(event.data.value, event.data.message);
+ } else if (event.data == "finishTest") {
+ SimpleTest.finish();
+ }
}
- function finishTest() {
- testWindow.close();
- SimpleTest.finish();
+ SimpleTest.waitForExplicitFinish();
+
+ function runTest() {
+ // If Fission is disabled, the pref is no-op.
+ SpecialPowers.pushPrefEnv({set: [["fission.bfcacheInParent", true]]}, () => {
+ window.open("file_bug1326251.html", "", "width=360,height=480,noopener");
+ });
}