diff --git a/addons/website/static/src/client_actions/website_preview/website_preview.js b/addons/website/static/src/client_actions/website_preview/website_preview.js index 4219b79493f..3f320234755 100644 --- a/addons/website/static/src/client_actions/website_preview/website_preview.js +++ b/addons/website/static/src/client_actions/website_preview/website_preview.js @@ -232,6 +232,7 @@ export class WebsitePreview extends Component { reloadIframe(url) { return new Promise((resolve, reject) => { + this.websiteService.websiteRootInstance = undefined; this.iframe.el.addEventListener('OdooFrameContentLoaded', resolve, { once: true }); if (url) { this.iframe.el.contentWindow.location = url; @@ -311,6 +312,7 @@ export class WebsitePreview extends Component { this.iframe.el.contentWindow.addEventListener('beforeunload', this._onPageUnload.bind(this)); this._replaceBrowserUrl(); this.iframe.el.contentWindow.addEventListener('hashchange', this._replaceBrowserUrl.bind(this)); + this.iframe.el.contentWindow.addEventListener('pagehide', this._onPageHide.bind(this)); this.websiteService.pageDocument = this.iframe.el.contentDocument; @@ -374,6 +376,8 @@ export class WebsitePreview extends Component { } else if (href && target !== '_blank' && !isEditing && this._isTopWindowURL(linkEl)) { ev.preventDefault(); this.router.redirect(href); + } else if (this.iframe.el.contentWindow.location.pathname !== new URL(href).pathname) { + this.websiteService.websiteRootInstance = undefined; } }); this.iframe.el.contentDocument.addEventListener('keydown', ev => { @@ -390,7 +394,6 @@ export class WebsitePreview extends Component { } _onPageUnload() { - this.websiteService.websiteRootInstance = undefined; this.iframe.el.setAttribute('is-ready', 'false'); // Before leaving the iframe, its content is replicated on an // underlying iframe, to avoid for white flashes (visible on @@ -404,6 +407,14 @@ export class WebsitePreview extends Component { $().getScrollingElement(this.iframefallback.el.contentDocument)[0].scrollTop = $().getScrollingElement(this.iframe.el.contentDocument)[0].scrollTop; } } + _onPageHide() { + // Normally, at this point, the websiteRootInstance is already set to + // `undefined`, as we want to do that as early as possible to prevent + // the editor to be in an unstable state. But some events not managed + // by the websitePreview could trigger a `pagehide`, so for safety, + // it is set to undefined again. + this.websiteService.websiteRootInstance = undefined; + } } WebsitePreview.template = 'website.WebsitePreview'; WebsitePreview.components = { diff --git a/addons/website/static/src/services/website_service.js b/addons/website/static/src/services/website_service.js index aadfbe23f5d..3a45e287391 100644 --- a/addons/website/static/src/services/website_service.js +++ b/addons/website/static/src/services/website_service.js @@ -190,6 +190,7 @@ export const websiteService = { }, goToWebsite({ websiteId, path, edition, translation, lang } = {}) { + this.websiteRootInstance = undefined; if (lang) { invalidateSnippetCache = true; path = `/website/lang/${lang}?r=${encodeURIComponent(path)}`;