Skip to content

fix(ios): classify asset responses using resolved file URL - #8741

Open
basnijholt wants to merge 1 commit into
ionic-team:mainfrom
basnijholt:fix/ios-asset-handler-resolved-file
Open

basnijholt wants to merge 1 commit into
ionic-team:mainfrom
basnijholt:fix/ios-asset-handler-resolved-file

Conversation

@basnijholt

Copy link
Copy Markdown

Description

Update WebViewAssetHandler.webView(_:start:) to classify responses using the resolved fileUrl while preserving the cordova.js stub and extensionless-file fallback:

  • Match the empty cordova.js stub when the last path component of the request is cordova.js (url.lastPathComponent == "cordova.js") rather than stringToLoad.contains("cordova.js"), keeping url.pathExtension (js) so root and nested cordova.js requests return an empty JavaScript response even when a custom router falls back to index.html. Substring-only paths (such as cordova.json or mycordova.js) are now served instead of returning empty.
  • For all other requests, use fileUrl.pathExtension when non-empty and fall back to url.pathExtension when the resolved file has no extension, for both mimeTypeForExtension and isMediaExtension (data loading and URLResponse vs HTTPURLResponse).

Added regression tests in ios/Tests/CapacitorTests/WebViewAssetHandlerTests.swift.

Change Type

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation
  • Other (CI, chores, etc.)

Rationale / Problems Fixed

When an app provides a custom Router that maps dot-suffixed SPA routes (for example /explore/example.com, /watch/clip.mp4, or /explore/mycordova.js) to index.html, WebViewAssetHandler currently inspects the request URL instead of the resolved file:

  • Dot-suffixed routes receive a MIME type derived from the route suffix instead of text/html.
  • Routes ending in a media extension are returned as URLResponse instead of HTTPURLResponse.
  • Any route containing the substring "cordova.js" skips reading the resolved file and returns empty bytes.

This PR changes only the iOS asset handler.

No related issue: found while using a custom iOS Router with dot-suffixed SPA routes.

Tests or Reproductions

Added ios/Tests/CapacitorTests/WebViewAssetHandlerTests.swift covering:

  • Custom Router mapping /explore/example.com, /watch/clip.mp4, and /explore/mycordova.js to index.html (verifies text/html, HTTPURLResponse, and full index.html bytes).
  • /cordova.js returning an empty JavaScript stub (JS MIME, HTTPURLResponse) both when the router falls back to index.html and when a non-empty cordova.js file exists on disk.
  • Nested /room/cordova.js returning the empty JavaScript stub (JS MIME, HTTPURLResponse).
  • Custom Router mapping /vendor/runtime.js to a real file named cordova.js serving that file's contents with JS MIME.
  • Custom Router mapping /app.js and /clip.mp4 to extensionless files falling back to url.pathExtension for MIME type and media response handling.
  • Real static (.js) and media (.mp4) files preserving their bytes, MIME types, and response classes.

Built and tested on macOS (Xcode 26.6, iPhone 17 / iOS 26.5 simulator):

  • xcodebuild build -scheme Capacitor-Package -destination 'generic/platform=iOS Simulator' — BUILD SUCCEEDED
  • xcodebuild test -scheme Capacitor-Package -destination 'platform=iOS Simulator,name=iPhone 17,OS=26.5' -collect-test-diagnostics never — TEST SUCCEEDED:
    • CapacitorObjCTests.xctest: 6/6 passed
    • CapacitorTests.xctest (XCTest): 24/24 passed (including 7/7 in WebViewAssetHandlerTests)
    • Swift Testing: 132/132 passed in 19 suites

Not run locally:

  • npm run swiftlint -- lint (unverified; CI will check).

Screenshots / Media

N/A

Platforms Affected

  • Android
  • iOS
  • Web

Notes / Comments

  • The same code exists on next.
  • AI assistance: Written with MindRoom DevAgent (google/gemini-4-neon); the xcodebuild build and xcodebuild test commands listed under Tests were run on macOS (Xcode 26.6, iPhone 17 / iOS 26.5 simulator); SwiftLint was not run locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant