Skip to content

Commit

Permalink
Add Preconnect.
Browse files Browse the repository at this point in the history
Part of #25.
  • Loading branch information
jverkoey committed Sep 12, 2024
1 parent b74039c commit a87aecd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ provided below is an organized table of W3C HTML tags and their equivalent Slips
[`<link rel="noreferrer">`](https://html.spec.whatwg.org/multipage/links.html#link-type-noreferrer) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="opener">`](https://html.spec.whatwg.org/multipage/links.html#link-type-opener) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="pingback">`](https://html.spec.whatwg.org/multipage/links.html#link-type-pingback) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="preconnect">`](https://html.spec.whatwg.org/multipage/links.html#link-type-preconnect) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="preconnect">`](https://html.spec.whatwg.org/multipage/links.html#link-type-preconnect) | ``Preconnect``
[`<link rel="prefetch">`](https://html.spec.whatwg.org/multipage/links.html#link-type-prefetch) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="preload">`](https://html.spec.whatwg.org/multipage/links.html#link-type-preload) | ``Preload``
[`<link rel="prev">`](https://html.spec.whatwg.org/multipage/links.html#link-type-prev) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
Expand Down
1 change: 1 addition & 0 deletions Sources/Slipstream/Documentation.docc/W3C/W3CViews.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The complete W3C HTML elements standard can be found [here](https://html.spec.wh
- ``Head``
- ``Title``
- ``Preload``
- ``Preconnect``
- ``Stylesheet``
- ``Meta``
- ``Charset``
Expand Down
41 changes: 41 additions & 0 deletions Sources/Slipstream/W3C/Elements/DocumentMetadata/Preconnect.swift
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?
}
14 changes: 14 additions & 0 deletions Tests/SlipstreamTests/W3C/PreconnectTests.swift
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/" />"#)
}
}

0 comments on commit a87aecd

Please sign in to comment.