Skip to content

Fix ajax when loading local files on iOS web view - #6610

Merged
mourner merged 2 commits into
mapbox:masterfrom
oscarfonts:fix-ios-file-status-zero
Aug 15, 2018
Merged

mourner merged 2 commits into
mapbox:masterfrom
oscarfonts:fix-ios-file-status-zero

Conversation

@oscarfonts

Copy link
Copy Markdown
Contributor

When running in iOS webview (cordova app), we detected that loading local resources (file:///... urls) is returning a "0" status code (instead of "200 OK").

Included this case in util.ajax, so local files are fetched without checking the response status code value.

Tried to add a unit test, but sinon's FakeXMLHttpRequest doesn't provide the standard responseURL property we are reading. At least made sure actual unit tests are passing. :)

@mourner mourner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oscarfonts looks like this PR has gone askew. Can you rebase on master and make sure the PR only contains the commit you added?

I'm also not sure this should be fixed on the GL JS side since it follows the specs, and requesting file:// files with Ajax is usually prohibited due to security restrictions. Could there be a workaround on the Cordova/app side instead?

@oscarfonts
oscarfonts force-pushed the fix-ios-file-status-zero branch from 9ea7464 to 4031b50 Compare July 25, 2018 09:41
@oscarfonts

Copy link
Copy Markdown
Contributor Author

Cleared PR with rebase. Thanks!

Didn't find a simpler way to pass a local (offline) resource to mapbox-gl.

Alternatives were: either forking the library to replace ajax calls with some other (non-http, probably environment specific) mechanism to access local resources, or embedding a local http server into the App.

I can keep my fork if you feel this too hacky, I just thought it could go upstream for a more offline-friendly library, so to speak.

@mourner

mourner commented Jul 25, 2018

Copy link
Copy Markdown
Member

@oscarfonts is the way Cordova behaves with Ajax + local files (e.g. the 0 status code) documented somewhere?

@oscarfonts

Copy link
Copy Markdown
Contributor Author

@mourner

mourner commented Aug 9, 2018

Copy link
Copy Markdown
Member

@oscarfonts sorry for a delay — maybe let's just accept xhr.status === 0 in onload without checking the URL? If this works for jQuery, it should probably be safe for GL JS..

Before the request is complete, the value of status will be 0 . It is worth noting that browsers report a status of 0 in case of XMLHttpRequest errors too.

The docs suggest that status is 0 by default and only changes to an appropriate number when it gets a response (in onload), but if it doesn't, it implies that the XHR is "fake" and we can simply accept the response (since errors will already be handled in onerror).

@oscarfonts

Copy link
Copy Markdown
Contributor Author

@mourner ok, done

@mourner
mourner merged commit aad9b54 into mapbox:master Aug 15, 2018
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.

2 participants