Implemented the scripts' deferral on the Page protocol in the Infrastructure package.

This commit is contained in:
2026-08-04 12:19:12 +02:00
parent 5c070c2b1a
commit c603421c28
2 changed files with 27 additions and 16 deletions
@@ -21,7 +21,7 @@ struct PageTests {
#expect(html.contains(#"name="viewport""#))
#expect(html.contains(#"<meta name="stub" content="marker">"#))
#expect(html.contains(#"<link rel="stylesheet" href="/css/stub.css">"#))
#expect(html.contains(#"<script src="/js/stub.js"></script>"#))
#expect(html.contains(#"<script defer src="/js/stub.js"></script>"#))
#expect(html.contains("Stub content"))
}
@@ -38,13 +38,17 @@ struct PageTests {
}
@Test
func `renders the scripts after the content`() throws {
func `renders the scripts deferred in the head, after the stylesheets`() throws {
let html = StubPage().render()
let content = try #require(html.range(of: "Stub content"))
let stylesheet = try #require(html.range(of: "/css/stub.css"))
let script = try #require(html.range(of: "/js/stub.js"))
let content = try #require(html.range(of: "Stub content"))
#expect(content.lowerBound < script.lowerBound)
// Deferred head scripts start downloading during head parsing but still execute, in order, only after the
// document is parsed the semantics end-of-body tags gave, minus the late download start.
#expect(stylesheet.lowerBound < script.lowerBound)
#expect(script.lowerBound < content.lowerBound)
}
@Test