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.

Code from the injectedJavaScriptBeforeContentLoaded prop runs AFTER the web code.

See original GitHub issue

Bug description:

According to official docs of react-native-webview (The injectedJavaScriptBeforeContentLoaded prop):

This is a script that runs before the web page loads for the first time. It only runs once, even if the page is reloaded or navigated away. This is useful if you want to inject anything into the window, localstorage, or document prior to the web code executing.

But we can see that the script on the page runs first, and only then runs the code from the injectedJavaScriptBeforeContentLoaded prop.

The setup is:

To Reproduce:

  1. git clone git@github.com:Kreozot/webview-scripts-test.git
  2. cd webview-scripts-test
  3. yarn
  4. yarn run android

Expected behavior:

Alert “injectedJavaScriptBeforeContentLoaded” shows before “script executed”.

Screenshots/Videos:

Alert “injectedJavaScriptBeforeContentLoaded” shows after “script executed”.

Environment:

  • OS: Android
  • OS version: 9
  • react-native version: 0.63.2
  • react-native-webview version: 10.8.3

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:11
  • Comments:17

github_iconTop GitHub Comments

2reactions
allan-simoncommented, Jul 19, 2021

I confirm it still exist (with the same device it sometimes execute before all script, sometime after the script in <head> but before the script in <body> etc. sounds like there’s a missing /lock or sync issue causing the injection execution to be based purely on luck in android ) , @Titozzz @chiaramooney can this issue be reopened ?

Also do you mind if I open a PR in the documentation to warn about this current issue waiting for it to solved?

1reaction
TheAlmightyBobcommented, Nov 17, 2022

Reopening because:

  1. The multiple folks chiming in on this issue here
  2. The original PR (#1099) itself acknowledged that the solution didn’t seem 100% reliable
  3. While I haven’t been able to repro any problems myself, I tossed an experiment in an app for others to try and there were indeed some failures.

That said, it’s unclear to me if there’s a possible “fix” here or if this is just an Android WebView limitation we need to warn folks about.

I did see this comment (https://github.com/react-native-webview/react-native-webview/pull/1099#issuecomment-611215560) suggesting an alternative approach, but that seems quite complex/hacky/risky for general use. Could be a useful workaround (via custom Android component) for folks who really need it though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code from the injectedJavaScriptBeforeContentLoaded prop ...
This is a script that runs before the web page loads for the first time. It only runs once, even if the page...
Read more >
injectedJavaScript is not working in Webview of react native
First one is your web view needs to be wrapped in a containing View ... mentioned injectedJavaScript prop is that it only runs...
Read more >
React Native WebView: A complete guide - LogRocket Blog
React Native WebView renders web content in a native view. ... The JavaScript code that is provided to the injectedJavaScript prop is only ......
Read more >
React native webview: The comprehensive guide you need to ...
React native webview is a component that lets your React Native app load webpages. Now, we will show you the comprehensive guide about...
Read more >
The complete guide to React Native WebView
As we mentioned, the injectedJavaScript prop runs after content load. But what if you need to run the JavaScript code before loading 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