Quellcode durchsuchen

LibWeb: Preload resources hinted by <link rel="preload">

If a page is nice enough to give us some preload hints, we can tell
RequestServer to get started on downloading the resources right away,
instead of waiting until discovering them later on during parsing.
Andreas Kling vor 3 Jahren
Ursprung
Commit
5bb2e6597a

+ 9 - 0
Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp

@@ -36,6 +36,13 @@ void HTMLLinkElement::inserted()
         if (auto sheet = m_css_loader.style_sheet())
             document().style_sheets().add_sheet(sheet.release_nonnull());
     }
+
+    if (m_relationship & Relationship::Preload) {
+        // FIXME: Respect the "as" attribute.
+        LoadRequest request;
+        request.set_url(attribute(HTML::AttributeNames::href));
+        m_preload_resource = ResourceLoader::the().load_resource(Resource::Type::Generic, request);
+    }
 }
 
 void HTMLLinkElement::parse_attribute(const FlyString& name, const String& value)
@@ -48,6 +55,8 @@ void HTMLLinkElement::parse_attribute(const FlyString& name, const String& value
                 m_relationship |= Relationship::Stylesheet;
             else if (part == "alternate")
                 m_relationship |= Relationship::Alternate;
+            else if (part == "preload")
+                m_relationship |= Relationship::Preload;
         }
     }
 }

+ 3 - 0
Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h

@@ -32,9 +32,12 @@ private:
         enum {
             Alternate = 1 << 0,
             Stylesheet = 1 << 1,
+            Preload = 1 << 2,
         };
     };
 
+    RefPtr<Resource> m_preload_resource;
+
     CSSLoader m_css_loader;
     unsigned m_relationship { 0 };
 };