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')