Skip to content

Commit 3cfa4e4

Browse files
Copilotdcalhoun
andcommitted
Use applicationNameForUserAgent instead of customUserAgent
Co-authored-by: dcalhoun <[email protected]>
1 parent 6f3aef8 commit 3cfa4e4

File tree

4 files changed

+13
-62
lines changed

4 files changed

+13
-62
lines changed

ios/Sources/GutenbergKit/Sources/EditorViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro
7878

7979
self.webView = GBWebView(frame: .zero, configuration: config)
8080
self.webView.scrollView.keyboardDismissMode = .interactive
81-
self.webView.customUserAgent = GBWebView.createCustomUserAgent()
81+
self.webView.applicationNameForUserAgent = "GutenbergKit/\(GBKVersion.version)"
8282

8383
self.isWarmupMode = isWarmupMode
8484

ios/Sources/GutenbergKit/Sources/GBWebView.swift

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,6 @@
11
import WebKit
22

33
class GBWebView: WKWebView {
4-
5-
/// Cached custom user agent to avoid repeated WebView instantiation
6-
private static var cachedCustomUserAgent: String?
7-
8-
/// Creates a custom user agent string by appending GutenbergKit identifier to the default user agent
9-
/// The result is cached after the first call to avoid performance overhead
10-
static func createCustomUserAgent() -> String {
11-
// Return cached value if available
12-
if let cached = cachedCustomUserAgent {
13-
return cached
14-
}
15-
16-
// Get the default user agent by creating a temporary WKWebView
17-
let webView = WKWebView()
18-
var defaultUserAgent = ""
19-
20-
// Use DispatchGroup for synchronous wait
21-
// This is safe during view initialization as it's not on a critical path
22-
let group = DispatchGroup()
23-
group.enter()
24-
25-
webView.evaluateJavaScript("navigator.userAgent") { result, error in
26-
if let result = result as? String {
27-
defaultUserAgent = result
28-
}
29-
group.leave()
30-
}
31-
32-
group.wait()
33-
34-
// Cache and return the result
35-
let customUserAgent = "\(defaultUserAgent) GutenbergKit/\(GBKVersion.version)"
36-
cachedCustomUserAgent = customUserAgent
37-
return customUserAgent
38-
}
394

405
#if canImport(UIKit)
416
/// Disables the default bottom bar that competes with the Gutenberg inserter

ios/Sources/GutenbergKit/Sources/Views/HTMLPreview/HTMLWebViewRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ final class HTMLWebViewRenderer {
9494
// Create web view with small initial frame for off-screen rendering
9595
// Frame will be adjusted per render based on viewport width and content height
9696
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 1200, height: 100), configuration: config)
97-
webView.customUserAgent = GBWebView.createCustomUserAgent()
97+
webView.applicationNameForUserAgent = "GutenbergKit/\(GBKVersion.version)"
9898

9999
delegate = RenderDelegate()
100100
webView.navigationDelegate = delegate

ios/Tests/GutenbergKitTests/GBWebViewTests.swift

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,23 @@ import WebKit
44

55
final class GBWebViewTests: XCTestCase {
66

7-
func testCreateCustomUserAgent() {
8-
// When
9-
let customUserAgent = GBWebView.createCustomUserAgent()
7+
func testApplicationNameForUserAgent() {
8+
// Given
9+
let webView = GBWebView()
1010

11-
// Then
12-
XCTAssertTrue(customUserAgent.contains("GutenbergKit/"),
13-
"User agent should contain GutenbergKit identifier")
14-
XCTAssertTrue(customUserAgent.contains("GutenbergKit/\(GBKVersion.version)"),
15-
"User agent should contain version number")
16-
XCTAssertTrue(customUserAgent.contains("Mozilla"),
17-
"User agent should contain Mozilla identifier from default user agent")
18-
}
19-
20-
func testCustomUserAgentEndsWithGutenbergKitIdentifier() {
2111
// When
22-
let customUserAgent = GBWebView.createCustomUserAgent()
12+
webView.applicationNameForUserAgent = "GutenbergKit/\(GBKVersion.version)"
2313

2414
// Then
25-
XCTAssertTrue(customUserAgent.hasSuffix(" GutenbergKit/\(GBKVersion.version)"),
26-
"Custom user agent should end with GutenbergKit identifier")
15+
XCTAssertEqual(webView.applicationNameForUserAgent, "GutenbergKit/\(GBKVersion.version)",
16+
"Application name should be set correctly")
2717
}
2818

29-
func testCustomUserAgentIsConsistent() {
30-
// Given
31-
let firstCall = GBWebView.createCustomUserAgent()
32-
33-
// When
34-
let secondCall = GBWebView.createCustomUserAgent()
35-
19+
func testVersionConstantExists() {
3620
// Then
37-
XCTAssertEqual(firstCall, secondCall,
38-
"Custom user agent should be consistent across calls")
21+
XCTAssertFalse(GBKVersion.version.isEmpty,
22+
"Version constant should not be empty")
23+
XCTAssertTrue(GBKVersion.version.contains("."),
24+
"Version should be in semantic versioning format")
3925
}
4026
}

0 commit comments

Comments
 (0)