Implemented the analytics preconnect hint on the Page protocol in the Infrastructure package.

This commit is contained in:
2026-08-04 12:19:20 +02:00
parent c603421c28
commit 16acdf0f3d
4 changed files with 73 additions and 5 deletions
@@ -131,10 +131,11 @@ struct PageTests {
let html = StubPage().render()
#expect(!html.contains("data-website-id"))
#expect(!html.contains(#"rel="preconnect""#))
}
@Test
func `renders the analytics tracker when provided`() {
func `renders the analytics tracker when provided`() throws {
let html = StubPage(analytics: .init(
scriptURL: "https://analytics.example.com/script",
websiteID: "0000-website-id",
@@ -142,6 +143,12 @@ struct PageTests {
)).render()
#expect(html.contains(#"<script defer src="https://analytics.example.com/script" data-website-id="0000-website-id" data-domains="example.com" data-exclude-hash="true" data-do-not-track="true" data-performance="true"></script>"#))
// The preconnect hint warms the tracker origin's connection before the parser reaches the script tag.
let preconnect = try #require(html.range(of: #"<link rel="preconnect" href="https://analytics.example.com">"#))
let script = try #require(html.range(of: #"<script defer src="https://analytics.example.com/script""#))
#expect(preconnect.lowerBound < script.lowerBound)
}
@Test