[FIX] website: missing alternate URLs for pages

Install multiple langages, each time translating the website. In a
private browsing session access /contactus, show the page source, the
multiple alternate URL (`<link rel="alternative">` in the `<head>`) are
all pointing the canonical URL instead of the alternative.

closes odoo/odoo#95683

X-original-commit: 3d4b4d3dcff864613a9e7038137e21674425ed08
Signed-off-by: Romain Derie (rde) <rde@odoo.com>
Signed-off-by: Julien Castiaux <juc@odoo.com>
This commit is contained in:
Julien Castiaux
2022-07-08 19:53:07 +02:00
committed by Julien Castiaux
parent 1806a4cddd
commit dce5dade01
2 changed files with 37 additions and 4 deletions
+3 -3
View File
@@ -1331,11 +1331,11 @@ class Website(models.Model):
router = http.root.get_db_router(request.db).bind('')
path = router.build(rule.endpoint, args)
if lang != self.default_lang_id:
path = f'/{lang.url_code}{path if path != "/" else ""}'
except (NotFound, AccessError, MissingError):
# The build method returns a quoted URL so convert in this case for consistency.
path = urls.url_quote_plus(request.httprequest.environ['REQUEST_URI'], safe='/')
path = urls.url_quote_plus(request.httprequest.path, safe='/')
if lang != self.default_lang_id:
path = f'/{lang.url_code}{path if path != "/" else ""}'
canonical_query_string = f'?{urls.url_encode(canonical_params)}' if canonical_params else ''
return self.get_base_url() + path + canonical_query_string
+34 -1
View File
@@ -302,7 +302,7 @@ class WithContext(HttpCase):
"There should be no crash when a public user is accessing `/` which is rerouting to another page with a different URL.")
root_html = html.fromstring(r.content)
canonical_url = root_html.xpath('//link[@rel="canonical"]')[0].attrib['href']
self.assertEqual(canonical_url, website.domain + "/")
self.assertIn(canonical_url, [f"{website.domain}/", f"{website.domain}/page_1"])
def test_t_cache_footer_debug_link(self):
# Debug link (flag) should be use the current url.
@@ -312,3 +312,36 @@ class WithContext(HttpCase):
self.assertIn('<a class="o_debug_mode" href="?debug="', r.text)
r = self.url_open('/contactus?name=MyName') # don't force debug here, already done on previous step
self.assertIn('<a class="o_debug_mode" href="?debug=&amp;name=MyName"', r.text)
def test_07_alternatives(self):
website = self.env.ref('website.default_website')
lang_fr = self.env['res.lang']._activate_lang('fr_FR')
lang_fr.write({'url_code': 'fr'})
website.language_ids = self.env.ref('base.lang_en') + lang_fr
website.default_lang_id = self.env.ref('base.lang_en')
with self.subTest(url='/page_1'):
res = self.url_open('/page_1')
res.raise_for_status()
root_html = html.fromstring(res.content)
canonical_url = root_html.xpath('//link[@rel="canonical"]')[0].attrib['href']
alternate_en_url = root_html.xpath('//link[@rel="alternate"][@hreflang="en"]')[0].attrib['href']
alternate_fr_url = root_html.xpath('//link[@rel="alternate"][@hreflang="fr"]')[0].attrib['href']
self.assertEqual(canonical_url, f'{self.base_url()}/page_1')
self.assertEqual(alternate_en_url, f'{self.base_url()}/page_1')
self.assertEqual(alternate_fr_url, f'{self.base_url()}/fr/page_1')
with self.subTest(url='/fr/page_1'):
res = self.url_open('/fr/page_1')
res.raise_for_status()
root_html = html.fromstring(res.content)
canonical_url = root_html.xpath('//link[@rel="canonical"]')[0].attrib['href']
alternate_en_url = root_html.xpath('//link[@rel="alternate"][@hreflang="en"]')[0].attrib['href']
alternate_fr_url = root_html.xpath('//link[@rel="alternate"][@hreflang="fr"]')[0].attrib['href']
self.assertEqual(canonical_url, f'{self.base_url()}/fr/page_1')
self.assertEqual(alternate_en_url, f'{self.base_url()}/page_1')
self.assertEqual(alternate_fr_url, f'{self.base_url()}/fr/page_1')