diff --git a/src/core/drive/page_renderer.ts b/src/core/drive/page_renderer.ts index 1d9392879..52b615b31 100644 --- a/src/core/drive/page_renderer.ts +++ b/src/core/drive/page_renderer.ts @@ -2,22 +2,21 @@ import { Renderer } from "../renderer" import { PageSnapshot } from "./page_snapshot" import { ReloadReason } from "../native/browser_adapter" import { activateScriptElement, waitForLoad, getBodyElementId } from "../../util" -import { nextEventLoopTick } from "../../util" export class PageRenderer extends Renderer { - static renderElement(currentElement: HTMLBodyElement, newElement: HTMLBodyElement) { - if (document.body && newElement instanceof HTMLBodyElement) { - const currentBody = PageRenderer.getBodyElement(currentElement) || currentElement - const newBody = PageRenderer.getBodyElement(newElement) || newElement + static renderElement(currentBody: HTMLBodyElement, newBody: HTMLBodyElement) { + if (document.body && newBody instanceof HTMLBodyElement) { + const currentElement = PageRenderer.getRenderedElement(currentBody) || currentBody + const newElement = PageRenderer.getRenderedElement(newBody) || newBody - currentBody.replaceWith(newBody) + currentElement.replaceWith(newElement) } else { - document.documentElement.appendChild(newElement) + document.documentElement.appendChild(newBody) } } get shouldRender() { - return this.newSnapshot.isVisitable && this.trackedElementsAreIdentical && this.bodyElementMatches + return this.newSnapshot.isVisitable && this.trackedElementsAreIdentical && this.renderedElementMatches } get reloadReason(): ReloadReason { @@ -33,9 +32,9 @@ export class PageRenderer extends Renderer { } } - if (!this.bodyElementMatches) { + if (!this.renderedElementMatches) { return { - reason: "body_element_mismatch", + reason: "rendered_element_mismatch", } } } @@ -45,8 +44,6 @@ export class PageRenderer extends Renderer { } async render() { - await nextEventLoopTick() - if (this.willRender) { this.replaceBody() } @@ -90,8 +87,8 @@ export class PageRenderer extends Renderer { return this.currentHeadSnapshot.trackedElementSignature == this.newHeadSnapshot.trackedElementSignature } - get bodyElementMatches() { - return PageRenderer.getBodyElement(this.newElement) !== null + get renderedElementMatches() { + return PageRenderer.getRenderedElement(this.newElement) !== null } static get bodySelector() { @@ -100,7 +97,7 @@ export class PageRenderer extends Renderer { return bodyId ? `#${bodyId}` : "body" } - static getBodyElement(element: HTMLElement): HTMLElement | null { + static getRenderedElement(element: HTMLElement): HTMLElement | null { if (element.matches(this.bodySelector)) { return element } else { diff --git a/src/core/drive/page_view.ts b/src/core/drive/page_view.ts index 31cf6a99d..10390e8c4 100644 --- a/src/core/drive/page_view.ts +++ b/src/core/drive/page_view.ts @@ -1,4 +1,3 @@ -import { nextEventLoopTick } from "../../util" import { View, ViewDelegate, ViewRenderOptions } from "../view" import { ErrorRenderer } from "./error_renderer" import { PageRenderer } from "./page_renderer" @@ -39,11 +38,10 @@ export class PageView extends View snapshot && this.visitCachedSnapshot(snapshot)) + const snapshot = this.view.cacheSnapshot() + if (snapshot) this.visitCachedSnapshot(snapshot) this.snapshotCached = true } }