[FIX] website_event_track_online: PWA not working with multi-lang

Before this commit, the PWA didn't work when the language was specified
in the URL like in "/fr_FR/event".
Note that it worked fine for the default language.
The reason is that we were out of the scope.

After this commit, we build the scope dynamically depending on the
language entered in the URL.

Steps to reproduce:
- Configure a second lang to the website
- Go to website
- Choose another language than the default one. (e.g. "French - BE")
- Go to the event page
=> Bug due to /fr_BE in the URL

closes odoo/odoo#55924

X-original-commit: 27a3837df5ecb9a0eada49ceddecfa447be1833d
Signed-off-by: Adrien Dieudonné (adr) <adr@odoo.com>
This commit is contained in:
Romeo Fragomeli
2020-08-14 09:55:14 +00:00
parent 847a228230
commit 2dffd1c8f0
2 changed files with 21 additions and 5 deletions
@@ -8,6 +8,7 @@ from datetime import timedelta
import pytz
from odoo import exceptions, http, fields
from odoo.addons.http_routing.models.ir_http import url_for
from odoo.addons.website_event_track.controllers.main import WebsiteEventTrackController
from odoo.http import request
from odoo.modules.module import get_module_resource
@@ -243,8 +244,8 @@ class EventTrackOnlineController(WebsiteEventTrackController):
'name': _('%s Online Events') % company.name,
'short_name': company.name,
'description': _('%s Online Events') % company.name,
'scope': '/event',
'start_url': '/event',
'scope': url_for('/event'),
'start_url': url_for('/event'),
'display': 'standalone',
'background_color': '#ffffff',
'theme_color': '#875A7B',
@@ -270,6 +271,6 @@ class EventTrackOnlineController(WebsiteEventTrackController):
body = fp.read()
response = request.make_response(body, [
('Content-Type', 'text/javascript'),
('Service-Worker-Allowed', '/event'),
('Service-Worker-Allowed', url_for('/event')),
])
return response
@@ -3,6 +3,7 @@ odoo.define("website_event_track_online.website_event_pwa_widget", function (req
var config = require("web.config");
var publicWidget = require("web.public.widget");
var utils = require("web.utils");
var PWAInstallBanner = publicWidget.Widget.extend({
xmlDependencies: ["/website_event_track_online/static/src/xml/website_event_pwa.xml"],
@@ -41,7 +42,7 @@ odoo.define("website_event_track_online.website_event_pwa_widget", function (req
start: function () {
var superProm = this._super.apply(this, arguments);
window.addEventListener("beforeinstallprompt", this.beforeInstallPromptHandler);
return superProm.then(this._registerServiceWorker);
return superProm.then(this._registerServiceWorker.bind(this));
},
/**
@@ -57,6 +58,19 @@ odoo.define("website_event_track_online.website_event_pwa_widget", function (req
// Private
//--------------------------------------------------------------------------
/**
* Returns the website's language
* @private
* @return {String}
*/
_getLangPrefix: function () {
var lang = utils.get_cookie('frontend_lang');
if (lang !== undefined && window.location.href.indexOf(`/${lang}/`) >= 0) {
return `/${lang}`;
}
return '';
},
/**
* @private
*/
@@ -72,8 +86,9 @@ odoo.define("website_event_track_online.website_event_pwa_widget", function (req
if (!("serviceWorker" in navigator)) {
return;
}
var langPrefix = this._getLangPrefix();
navigator.serviceWorker
.register("/event/service-worker.js", { scope: "/event" })
.register(`${langPrefix}/event/service-worker.js`, { scope: `${langPrefix}/event` })
.then((registration) => {
console.info("Registration successful, scope is:", registration.scope);
})