question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

bug: CORS error when loading local files in iOS with live reload

See original GitHub issue

Bug Report

Capacitor Version

๐Ÿ’Š   Capacitor Doctor  ๐Ÿ’Š 

Latest Dependencies:

  @capacitor/cli: 2.4.6
  @capacitor/core: 2.4.6
  @capacitor/android: 2.4.6
  @capacitor/electron: 2.4.6
  @capacitor/ios: 2.4.6

Installed Dependencies:

  @capacitor/cli 2.4.4
  @capacitor/android 2.4.4
  @capacitor/ios 2.4.4
  @capacitor/core 2.4.4
  @capacitor/electron not installed

[success] Android looking great! ๐Ÿ‘Œ
  Found 1 Capacitor plugin for ios:
    cordova-plugin-file (6.0.2)
[success] iOS looking great! ๐Ÿ‘Œ

Platform(s)

  • Android
  • iOS
  • Electron
  • Web

Current Behavior

The local file converted to Web View-friendly path (like capacitor://localhost/capacitor_file/โ€ฆ) cannot be fetched because of CORS error when running app in iOS with live reload. It seems that CORS error is occurred because the source origin is http://xxx.xxx.xx.xx:8100 in default with live reload.

Expected Behavior

I can fetch my local file in OS with live reload.

Code Reproduction

// capacitor.config.json
{
  "server": {
    "url": "http://192.168.11.21:8100",
    "cleartext": true
  }
}

const path = "captured-photos/66026da3-5098-478a-9794-be8926df96e5"
const { uri } = await Filesystem.stat({ directory, path })
const webURI = window.Ionic.WebView.convertFileSrc(uri)

// throws CORS error!
const response = await fetch(webURI)

Other Technical Details

npm --version output: 6.14.10

node --version output: v14.15.1

pod --version output (iOS issues only): 1.10.0

Additional Context

Related issues

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:13

github_iconTop GitHub Comments

2reactions
realityfiltercommented, Aug 31, 2022

We just modified WebViewAssetHandler.swift (under Development Pods/Cordova).

var headers =  [
  "Content-Type": mimeType,
  "Cache-Control": "no-cache",
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Methods": "GET, OPTIONS"
]

I think the npm package patch-package could be used to change this file under node_modules/@capacitor/ios/Capacitor/Capacitor and patch it automatically after npm install.

1reaction
realityfiltercommented, Mar 24, 2022

This is still a problem with capacitor 3. Any workarounds, configuration possibilities?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Safari: "Fetch API cannot load due to access control checks ...
Only after reloading the page to send the CORS request again, I get "Fetch API cannot load due to access control checks" in...
Read more >
921233 - Local Overrides fails with CORS related - Monorail
The issue seem to be that you can't locally override a script that was loaded using CORS policy without disabling CORS in Chrome....
Read more >
Hybrid app subject to CORS if live reload is used, any work around ...
Hello,. When serving a jet app as a hybrid app, the application is subject to CORS restriction if live reload is allowed (i.e....
Read more >
CORS Errors: Cross-Origin Resource Sharing - Ionic Framework
CORS errors happen in web apps if requests are made and servers don't return required headers. Read about Cross-Origin Resource Sharing in Ionic...
Read more >
Dealing with image CORS error in Chrome, Chromium and ...
The issue comes from the way that Chromium caches the images. The way that the initial image is cached is - without the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found