Skip to content
Snippets Groups Projects
Commit 33167b39 authored by Jeremy Kersten's avatar Jeremy Kersten
Browse files

[IMP] website: add cache for _get_canonical_url_localized


This method is called at least once by lang by request. On a website of ~20
langs (e.g. on a staging of odoo.com) it improves the page speed to generate
a blog post of:
   45 0.028 0.056 with the cache
instead of
   79 0.076 0.095 without the cache

closes odoo/odoo#88011

Signed-off-by: default avatarJérémy Kersten <jke@odoo.com>
parent 80a04f7e
Branches
Tags
No related merge requests found
......@@ -695,7 +695,7 @@ class Website(models.Model):
endpoint.routing['auth'] in ('none', 'public') and
endpoint.routing.get('website', False) and
all(hasattr(converter, 'generate') for converter in converters)):
return False
return False
# dont't list routes without argument having no default value or converter
spec = inspect.getargspec(endpoint.method.original_func)
......@@ -862,6 +862,17 @@ class Website(models.Model):
self.ensure_one()
return self._get_http_domain() or super(BaseModel, self).get_base_url()
@tools.ormcache('path', 'lang')
def _get_canonical_url_localized_cached(self, path, args, lang):
router = http.root.get_db_router(request.db).bind_to_environ(request.httprequest.environ)
for key, val in list(args.items()):
if isinstance(val, models.BaseModel):
if val.env.context.get('lang') != lang:
args[key] = val.with_context(lang=lang)
endpoint = router.match(path_info=path, return_rule=True)[0].endpoint
return router.build(endpoint, args)
def _get_canonical_url_localized(self, lang, canonical_params):
"""Returns the canonical URL for the current request with translatable
elements appropriately translated in `lang`.
......@@ -872,14 +883,11 @@ class Website(models.Model):
"""
self.ensure_one()
if request.endpoint:
router = http.root.get_db_router(request.db).bind_to_environ(request.httprequest.environ)
arguments = dict(request.endpoint_arguments)
for key, val in list(arguments.items()):
if isinstance(val, models.BaseModel):
if val.env.context.get('lang') != lang.code:
arguments[key] = val.with_context(lang=lang.code)
endpoint = router.match(path_info=request.httprequest.path, return_rule=True)[0].endpoint
path = router.build(endpoint, arguments)
path = self._get_canonical_url_localized_cached(
request.httprequest.path,
dict(request.endpoint_arguments),
lang.code
)
else:
# The build method returns a quoted URL so convert in this case for consistency.
path = urls.url_quote_plus(request.httprequest.path, safe='/')
......
......@@ -243,13 +243,13 @@
</t>
</t>
<t t-if="request and request.is_frontend_multilang and website">
<t t-if="request and request.is_frontend_multilang and website and website.is_public_user()">
<t t-set="alternate_languages" t-value="website._get_alternate_languages(canonical_params=canonical_params)"/>
<t t-foreach="alternate_languages" t-as="lg">
<link rel="alternate" t-att-hreflang="lg['hreflang']" t-att-href="lg['href']"/>
</t>
</t>
<link t-if="request and website" rel="canonical" t-att-href="website._get_canonical_url(canonical_params=canonical_params)"/>
<link t-if="request and website and website.is_public_user()" rel="canonical" t-att-href="website._get_canonical_url(canonical_params=canonical_params)"/>
<link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin=""/>
</xpath>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment