-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #25.
- Loading branch information
Showing
4 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Sources/Slipstream/W3C/Elements/DocumentMetadata/Preconnect.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Foundation | ||
|
||
import SwiftSoup | ||
|
||
/// A view that indicates that preemptively initiating a connection to the | ||
/// origin of the specified resource is likely to be beneficial, as it is highly | ||
/// likely that the user will require resources located at that origin, and | ||
/// the user experience would be improved by preempting the latency | ||
/// costs associated with establishing the connection. | ||
/// | ||
/// ```swift | ||
/// struct MySiteMetadata: View { | ||
/// var body: some View { | ||
/// Head { | ||
/// Preconnect(URL(string: "https://rsms.me/")) | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// - SeeAlso: W3C [link](https://html.spec.whatwg.org/multipage/semantics.html#the-link-element) specification. | ||
/// - SeeAlso: W3C [preconnect](https://html.spec.whatwg.org/multipage/links.html#link-type-preconnect) specification. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public struct Preconnect: View { | ||
/// Creates a Preconnect view. | ||
public init(_ url: URL?) { | ||
self.url = url | ||
} | ||
|
||
@_documentation(visibility: private) | ||
public func render(_ container: Element, environment: EnvironmentValues) throws { | ||
guard let url else { | ||
return | ||
} | ||
let element = try container.appendElement("link") | ||
try element.attr("rel", "preconnect") | ||
try element.attr("href", url.absoluteString) | ||
} | ||
|
||
private let url: URL? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
import Testing | ||
|
||
import Slipstream | ||
|
||
struct PreconnectTests { | ||
@Test func nilURL() throws { | ||
try #expect(renderHTML(Preconnect(nil)) == "") | ||
} | ||
|
||
@Test func validURL() throws { | ||
try #expect(renderHTML(Preconnect(URL(string: "https://rsms.me/"))) == #"<link rel="preconnect" href="https://rsms.me/" />"#) | ||
} | ||
} |