Commit c5f72e00 authored by jan.koester's avatar jan.koester
Browse files

theme: LCP fetchpriority=high for first image, defer only external scripts

parent df9e3fe4
Loading
Loading
Loading
Loading
+17 −9
Changes for src/theme.cpp: 17 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -503,12 +503,10 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml
        // Inject theme color CSS variables into <head>
        injectThemeColors(tid, index);

        // Add loading="lazy" to all <img> tags except the banner logo (above the fold)
        // Add "defer" to inline <script> tags to reduce TBT
        // Merge per-widget <style> blocks into one consolidated block to reduce CSSOM recalculations
        // Optimize images: first content image gets fetchpriority="high" (LCP candidate),
        // all others get loading="lazy". Add "defer" to external scripts.
        {
            libhtmlpp::HtmlString mergedCSS;
            bool hasMergedCSS = false;
            bool firstContentImage = true;

            std::function<void(libhtmlpp::Element*)> optimizeDOM = [&](libhtmlpp::Element *node) {
                if (!node) return;
@@ -519,28 +517,38 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml
                    if (tag == "img") {
                        bool isBanner = false;
                        bool hasLoading = false;
                        bool hasFetchpriority = false;
                        for (auto *attr = hel->firstAttribute(); attr; attr = attr->nextAttribute()) {
                            if (attr->getKey() == "id" && attr->getValue() == "banner")
                                isBanner = true;
                            if (attr->getKey() == "loading")
                                hasLoading = true;
                        }
                        if (!isBanner && !hasLoading)
                            if (attr->getKey() == "fetchpriority")
                                hasFetchpriority = true;
                        }
                        if (!isBanner && firstContentImage && !hasFetchpriority) {
                            // First content image is the LCP candidate
                            hel->setAttribute("fetchpriority", "high");
                            firstContentImage = false;
                        } else if (!isBanner && !hasLoading) {
                            hel->setAttribute("loading", "lazy");
                        }
                    }

                    for (auto *child = hel->firstChild(); child; child = child->nextElement())
                        optimizeDOM(child);
                } else if (node->getType() == libhtmlpp::ScriptEL) {
                    // Add defer to script elements to avoid blocking the main thread
                    // Add defer only to external script elements (with src attribute)
                    libhtmlpp::ScriptElement *sel = static_cast<libhtmlpp::ScriptElement*>(node);
                    bool hasDefer = false;
                    bool hasAsync = false;
                    bool hasSrc = false;
                    for (auto *attr = sel->firstAttribute(); attr; attr = attr->nextAttribute()) {
                        if (attr->getKey() == "defer") hasDefer = true;
                        if (attr->getKey() == "async") hasAsync = true;
                        if (attr->getKey() == "src") hasSrc = true;
                    }
                    if (!hasDefer && !hasAsync)
                    if (hasSrc && !hasDefer && !hasAsync)
                        sel->setAttribute("defer", "");
                }
            };