Skip to content

Commit feb32a9

Browse files
authored
Fetch stylesheets from URL (#1293)
1 parent c10c66b commit feb32a9

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Sources/LiveViewNative/Coordinators/LiveSessionCoordinator.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,17 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
149149
// extract the root layout, removing anything within the `<div data-phx-main>`.
150150
let mainDiv = try doc.select("div[data-phx-main]")[0]
151151
try mainDiv.replaceWith(doc.createElement("phx-main"))
152-
self.stylesheet = try? doc.select("Style").reduce(Stylesheet<R>(content: [], classes: [:])) {
153-
(try? Stylesheet<R>(from: $1.text(), in: .init()).merge(with: $0)) ?? $0
152+
async let stylesheet = withThrowingTaskGroup(of: (Data, URLResponse).self) { group in
153+
for style in try doc.select("Style") {
154+
guard let url = URL(string: try style.attr("url"), relativeTo: url)
155+
else { continue }
156+
group.addTask { try await self.configuration.urlSession.data(from: url) }
157+
}
158+
return try await group.reduce(Stylesheet<R>(content: [], classes: [:])) { result, next in
159+
guard let contents = String(data: next.0, encoding: .utf8)
160+
else { return result }
161+
return result.merge(with: try Stylesheet<R>(from: contents, in: .init()))
162+
}
154163
}
155164
self.rootLayout = try LiveViewNativeCore.Document.parse(doc.outerHtml())
156165

@@ -160,6 +169,8 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
160169
try await self.connectSocket(domValues)
161170
}
162171

172+
self.stylesheet = try await stylesheet
173+
163174
try await navigationPath.last!.coordinator.connect(domValues: domValues, redirect: false)
164175
} catch {
165176
self.state = .connectionFailed(error)

0 commit comments

Comments
 (0)