perf: cache meta tags existence

This commit is contained in:
Ankush Menat 2025-01-14 19:19:02 +05:30
parent dc7636de8f
commit 0ff3e6fd4c
2 changed files with 14 additions and 4 deletions

View file

@ -20,3 +20,9 @@ class WebsiteRouteMeta(Document):
def autoname(self): def autoname(self):
if self.name and self.name.startswith("/"): if self.name and self.name.startswith("/"):
self.name = self.name[1:] self.name = self.name[1:]
def clear_cache(self):
from frappe.website.website_components.metatags import has_meta_tags
has_meta_tags.clear_cache()
return super().clear_cache()

View file

@ -1,4 +1,5 @@
import frappe import frappe
from frappe.utils.caching import site_cache
METATAGS = ("title", "description", "image", "author", "published_on") METATAGS = ("title", "description", "image", "author", "published_on")
@ -58,14 +59,17 @@ class MetaTags:
route = self.path route = self.path
if route == "": if route == "":
# homepage # homepage
route = frappe.db.get_single_value("Website Settings", "home_page") route = frappe.get_website_settings("home_page")
route_exists = ( route_exists = route and not route.endswith((".js", ".css")) and has_meta_tags(route)
route and not route.endswith((".js", ".css")) and frappe.db.exists("Website Route Meta", route)
)
if route_exists: if route_exists:
website_route_meta = frappe.get_doc("Website Route Meta", route) website_route_meta = frappe.get_doc("Website Route Meta", route)
for meta_tag in website_route_meta.meta_tags: for meta_tag in website_route_meta.meta_tags:
d = meta_tag.get_meta_dict() d = meta_tag.get_meta_dict()
self.tags.update(d) self.tags.update(d)
@site_cache(ttl=10 * 60, maxsize=16)
def has_meta_tags(route):
return bool(frappe.db.exists("Website Route Meta", route))