diff --git a/Packages/Infrastructure/Sources/Public/Protocols/Page.swift b/Packages/Infrastructure/Sources/Public/Protocols/Page.swift index 5f71622..952ea4c 100644 --- a/Packages/Infrastructure/Sources/Public/Protocols/Page.swift +++ b/Packages/Infrastructure/Sources/Public/Protocols/Page.swift @@ -76,9 +76,9 @@ public extension Page { content } - /// The viewport declaration, the ``summary``, ``canonicalURL``, and ``socialCard`` tags, the ``structuredData`` script and the - /// ``analytics`` tracker script (when provided), and the ``metadata`` followed by the ``stylesheets`` links and the deferred - /// ``scripts`` tags, placed in the document head. + /// The viewport declaration, the ``analytics`` origin preconnect hint, the ``summary``, ``canonicalURL``, and ``socialCard`` + /// tags, the ``structuredData`` script and the ``analytics`` tracker script (when provided), and the ``metadata`` followed by the + /// ``stylesheets`` links and the deferred ``scripts`` tags, placed in the document head. /// /// The charset declaration is omitted: Elementary's `HTMLDocument` scaffolding already emits `` before this markup, /// and HTML5 allows only one. @@ -92,7 +92,15 @@ public extension Page { .name(.viewport), .content("width=device-width, initial-scale=1") ) - + + // Rendered first so the cross-origin handshake starts before the parser reaches the tracker script tag. + if let origin = analytics?.origin { + link( + .rel("preconnect"), + .href(origin) + ) + } + if let summary { meta( .name(.description), diff --git a/Packages/Infrastructure/Sources/Public/Types/Analytics.swift b/Packages/Infrastructure/Sources/Public/Types/Analytics.swift index 4df4997..378df23 100644 --- a/Packages/Infrastructure/Sources/Public/Types/Analytics.swift +++ b/Packages/Infrastructure/Sources/Public/Types/Analytics.swift @@ -1,3 +1,5 @@ +import Foundation + /// The analytics tracker a page embeds: where the script loads from, which site it reports as, which domains it reports from, and how it behaves. /// /// A page carries it as an optional value so the ``Page`` scaffolding renders the tracker's deferred `"#)) + + // The preconnect hint warms the tracker origin's connection before the parser reaches the script tag. + let preconnect = try #require(html.range(of: #""#)) + let script = try #require(html.range(of: #"