From d7b2b7882fa7ea6d7f50bcdd59a51674ddd0fb15 Mon Sep 17 00:00:00 2001 From: "Arthur Detroux (ard)" Date: Fri, 12 Aug 2022 07:53:25 +0000 Subject: [PATCH] [FIX] website: invalidate snippet cache when switching website Commit [1] keeps the snippet cache alive through different instance of the website snippet menu. This allows for the user to switch pages, even apps, while keeping the snippets in cache, decreasing the startup time of the snippet menu. However, the cache is not invalidated when switching website or installing a new theme. This commit adds a `invalidateSnippetCache` property to the websiteService. If this property is set to `true`, the cache will be invalidated next time the menus loads its snippet. This property is currently set to `true` when changing website and switching theme. [1]: https://github.com/odoo/odoo/commit/03c552690b15cbf2e7d6b7812386ac64042219af task-2687506 X-original-commit: bb503962a36f3798c50049f2844e7b288b632955 Part-of: odoo/odoo#102799 --- .../static/src/components/editor/editor.js | 8 ++++ .../components/views/theme_preview_form.js | 1 + .../static/src/services/website_service.js | 13 +++++++ .../tours/snippet_cache_across_websites.js | 39 +++++++++++++++++++ addons/website/tests/test_ui.py | 14 +++++++ 5 files changed, 75 insertions(+) create mode 100644 addons/website/static/tests/tours/snippet_cache_across_websites.js diff --git a/addons/website/static/src/components/editor/editor.js b/addons/website/static/src/components/editor/editor.js index 3e26164b5f3..14765e9c7b9 100644 --- a/addons/website/static/src/components/editor/editor.js +++ b/addons/website/static/src/components/editor/editor.js @@ -22,6 +22,14 @@ export class WebsiteEditorComponent extends Component { }); this.wysiwygOptions = {}; + // TODO: This is done here because the snippet menu cannot access + // OWL services. Once it can, the logic for invalidating the + // cache should probably be moved there. + if (this.websiteService.invalidateSnippetCache) { + this.wysiwygOptions.invalidateSnippetCache = true; + this.websiteService.invalidateSnippetCache = false; + } + useChildSubEnv(legacyEnv); onWillStart(async () => { diff --git a/addons/website/static/src/components/views/theme_preview_form.js b/addons/website/static/src/components/views/theme_preview_form.js index 169f4cd4621..ca396b6f6d1 100644 --- a/addons/website/static/src/components/views/theme_preview_form.js +++ b/addons/website/static/src/components/views/theme_preview_form.js @@ -23,6 +23,7 @@ export function useLoaderOnClick() { async onClickViewButton(params) { const name = params.clickParams.name; if (['button_refresh_theme', 'button_choose_theme'].includes(name)) { + website.invalidateSnippetCache = true; website.showLoader({ showTips: name !== 'button_refresh_theme' }); try { const resParams = params.getResParams(); diff --git a/addons/website/static/src/services/website_service.js b/addons/website/static/src/services/website_service.js index 764cfa43dfc..046ab059018 100644 --- a/addons/website/static/src/services/website_service.js +++ b/addons/website/static/src/services/website_service.js @@ -42,6 +42,8 @@ export const websiteService = { let blockingProcesses = []; let modelNamesProm = null; const modelNames = {}; + let invalidateSnippetCache = false; + let lastWebsiteId = null; const context = reactive({ showNewContentModal: false, @@ -71,6 +73,10 @@ export const websiteService = { }); return { set currentWebsiteId(id) { + if (id && id !== lastWebsiteId) { + invalidateSnippetCache = true; + lastWebsiteId = id; + } currentWebsiteId = id; websiteSystrayRegistry.trigger('EDIT-WEBSITE'); }, @@ -166,6 +172,13 @@ export const websiteService = { set actionJsId(jsId) { actionJsId = jsId; }, + get invalidateSnippetCache() { + return invalidateSnippetCache; + }, + set invalidateSnippetCache(value) { + invalidateSnippetCache = value; + }, + goToWebsite({ websiteId, path, edition, translation } = {}) { action.doAction('website.website_preview', { clearBreadcrumbs: true, diff --git a/addons/website/static/tests/tours/snippet_cache_across_websites.js b/addons/website/static/tests/tours/snippet_cache_across_websites.js new file mode 100644 index 00000000000..5eafd88fbaf --- /dev/null +++ b/addons/website/static/tests/tours/snippet_cache_across_websites.js @@ -0,0 +1,39 @@ +/** @odoo-module **/ + +import wTourUtils from 'website.tour_utils'; + +wTourUtils.registerWebsitePreviewTour('snippet_cache_across_websites', { + edition: true, + test: true, + url: '/@/' +}, [ + { + content: "Check that the custom snippet is displayed", + trigger: '#snippet_custom_body span:contains("custom_snippet_test")', + run: () => null, + }, + // There's no need to save, but canceling might or might not show a popup... + ...wTourUtils.clickOnSave(), + { + content: "Click on the website switch to switch to website 2", + trigger: '.o_website_switcher_container button', + }, + { + content: "Switch to website 2", + // Ensure data-website-id exists + extra_trigger: 'iframe html[data-website-id="1"]', + trigger: '.o_website_switcher_container .dropdown-item:nth-child(2)' + }, + { + content: "Wait for the iframe to be loaded", + trigger: 'iframe html:not([data-website-id="1"])', + run: () => null, + }, + wTourUtils.clickOnEdit(), + { + content: "Check that the custom snippet is not here", + extra_trigger: '#oe_snippets:not(:has(#snippet_custom_body span:contains("custom_snippet_test")))', + trigger: '#oe_snippets:has(#snippet_custom.d-none)', + run: () => null, + }, +]); diff --git a/addons/website/tests/test_ui.py b/addons/website/tests/test_ui.py index 9e05486ed55..93ad7d0a385 100644 --- a/addons/website/tests/test_ui.py +++ b/addons/website/tests/test_ui.py @@ -317,3 +317,17 @@ class TestUi(odoo.tests.HttpCase): def test_23_website_multi_edition(self): self.start_tour('/@?enable_editor=1', 'website_multi_edition', login='admin') + + def test_24_snippet_cache_across_websites(self): + default_website = self.env.ref('website.default_website') + self.env['ir.ui.view'].with_context(website_id=default_website.id).save_snippet( + name='custom_snippet_test', + arch=""" +
+
Custom Snippet Website 1
+
+ """, + thumbnail_url='/website/static/src/img/snippets_thumbs/s_text_block.svg', + snippet_key='s_text_block', + template_key='website.snippets') + self.start_tour('/@/', 'snippet_cache_across_websites', login='admin')