Skip to content

Android WebViewAssetLoader support #15

Description

@ccoltos

Is there any intention to add support for WebViewAssetLoader on Android?

Current issue:

JavaScript cannot fetch("file://android_asset/...") due to security reasons and requires a special assets loaded handling described in here: https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.

This will allow any Web App hosted inside the WebView to access any type of bundled asset resource (from android/app/src/main/assets) through JS fetch/XMLHttpRequest calls.

Solution Suggestion:

Add a property allowAssetsLoader to the WebView, that will intercept on demand the urls and will interpret the custom assets scheme: https://appassets.androidplatform.net/assets/

android/src/main/java/com/reactnativecommunity/RNCWebViewClient.java:

@Nullable
@Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        if(!allowAssetsLoader) {
            return super.shouldInterceptRequest(view, request);
        }

        if(assetLoader == null) {
            RNCWebView reactWebView = (RNCWebView) view;
            assetLoader = new WebViewAssetLoader.Builder()
                    .addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(reactWebView.getReactApplicationContext()))
                    .build();
        }

        return assetLoader.shouldInterceptRequest(request.getUrl());
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important. Nice to have improvements and optimizations.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions