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.

No way to load a local HTML file in the webview on Android

See original GitHub issue

Bug description: I have a file with name index.html. I’m trying to load the file into the webview. However, there seems to be no option for it in android.

To Reproduce:

On iOS I tried:

const myHtml = require("./index.html");

// In Render
<WebView
  ref={this._webViewRef}
  source={myHtml}
/>

It worked perfectly fine. However on Android it threw TypeError: Cannot use 'in' operator to search for 'method' So, I tried manually resolving the asset uri like I used to do for Images,

<WebView
  ref={this._webViewRef}
  source={{uri: Image.resolveAssetSource(myHtml).uri}}
/>

However it directly loads the file HTML source instead of the actual webpage.

Expected behavior:

const myHtml = require("./index.html");

// In Render
<WebView
  ref={this._webViewRef}
  source={myHtml}
/>

The above code should just load the webpage on both android and iOS I guess…

Environment:

  • OS: macOS
  • OS version: 10.14 (18A391)
  • react-native version: 0.60.3
  • react-native-webview version: 5.12.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:24
  • Comments:48 (8 by maintainers)

github_iconTop GitHub Comments

26reactions
paldepindcommented, Jul 30, 2019

Per this comment using <WebView source={require('page.html')} /> is not supported by react-native-webview. So, indeed, there seems to be no official way to load a local HTML file in the webview.

It would be really nice if using require with source could work like it did with the webview previously included in react-native. We can’t currently upgrade react-native due to this problem.

22reactions
DaniAkashcommented, Jul 29, 2019

Shouldn’t this be documented?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Load HTML file into WebView - android - Stack Overflow
Use assetLoader from (2) to construct WebViewClient from (3), and set it in your WebView . Your index.html can be loaded by using...
Read more >
Loading in-app content - Android Developers
Right-click the assets folder and then click New > File. Enter index.html and press Return or Enter . Screen showing create-file menus. Figure...
Read more >
Loading local web content on Android - Hellsoft
One way to load the content is to embed a HTTP server in your application and load the content into your WebView through...
Read more >
How to load and display a local html file in Android Webview?
Let's say the html file is called as index.html and is located in the asset folder of the app. The following Activity shows...
Read more >
How to load a local HTML or PDF file? - Vuplex Support
Copy your web page resources to the StreamingAssets folder. For example, you could have a folder with the following files: ... await webViewPrefab...
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