From 0db3290b7fc6a63cfa62f03bbeade9df1bc7dca2 Mon Sep 17 00:00:00 2001 From: Benoit Socias Date: Tue, 19 Mar 2024 13:00:45 +0100 Subject: [PATCH] [FIX] website: not fail when sharing page with an SEO title Since [1] when the social share widget was introduced, a hashtag is generated from the name of the website that is found inside the page title. This fails when the page title is replaced through the SEO feature. This commit prevents the failure but not extracting the website name if it is missing from the title. Note that the `registry.socialShare` fixed here is only used by the forum post page in the whole codebase. It is different from the social snippet. The social snippet is doing the same code (kind of duplicate), but it does not care about removing the | right part. It means that if you drag & drop the "Share" snippet in a forum post page (like in the footer) and click on it, it will work since it does not try to split on the title's pipe. In master, we will evaluate if we should remove this `registry.socialShare` and use the `Share` snippet instead. Steps to reproduce: - Install website_forum. - Go to a forum post. - Set SEO title to a value without `|` (pipe). - Share to X. => An error popup was shown. [1]: https://github.com/odoo/odoo/commit/1c91e27c8c8cb7492d26f03e27c4991b53a1675d opw-3799914 closes odoo/odoo#158478 X-original-commit: f89f4a8f1ad0ee3fb62ba246ea17988955dbdc4b Signed-off-by: Romain Derie (rde) Signed-off-by: Benoit Socias (bso) --- addons/website/static/src/js/content/snippets.animation.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/addons/website/static/src/js/content/snippets.animation.js b/addons/website/static/src/js/content/snippets.animation.js index 8394734de8f..1fcd167c66e 100644 --- a/addons/website/static/src/js/content/snippets.animation.js +++ b/addons/website/static/src/js/content/snippets.animation.js @@ -971,8 +971,11 @@ registry.socialShare = publicWidget.Widget.extend({ _renderSocial: function (social) { var url = this.$el.data('urlshare') || document.URL.split(/[?#]/)[0]; url = encodeURIComponent(url); - var title = document.title.split(" | ")[0]; // get the page title without the company name - var hashtags = ' #' + document.title.split(" | ")[1].replace(' ', '') + ' ' + this.hashtags; // company name without spaces (for hashtag) + const titleParts = document.title.split(" | "); + const title = titleParts[0]; // Get the page title without the company name + const hashtags = titleParts.length === 1 + ? ` ${this.hashtags}` + : ` #${titleParts[1].replace(" ", "")} ${this.hashtags}`; // Company name without spaces (for hashtag) var socialNetworks = { 'facebook': 'https://www.facebook.com/sharer/sharer.php?u=' + url, 'twitter': 'https://twitter.com/intent/tweet?original_referer=' + url + '&text=' + encodeURIComponent(title + hashtags + ' - ') + url,