Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX WebView / WebEngine Cache external JavaScript

Situation: I have a Simple HTML page which have a normal script tag like this

<script src="main.js"></script>

After i load the html , i update the main.js , and make a reload (throught UI Button).

The Problem My new JS is not taken , i must close the Application and open it again.

What i did try: -not using webEngine.reload() , but webEngine.load() -making a new broswer every time a reload occurs. -making a new stage with a new broswer -setting all nodes caching off -in HTML following code

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

-InetAddressCachePolicy:

InetAddressCachePolicy.setNegativeIfNotSet(InetAddressCachePolicy.NEVER);

-VM Options: -Dnetworkaddress.cache.ttl=0.

The Question Is there any way to delete the cache or enforce the WebView to reload all resources other than appending a number to JS filename every time i update it?

Note:I use NetBeans 7.3 With last Java (update 22)

like image 766
Rany Ishak Avatar asked Nov 03 '22 23:11

Rany Ishak


1 Answers

as described here: http://docs.oracle.com/javafx/8/webview/overview.htm

When working with the WebView component, you should remember that it has the default in-memory cache. It means that any cached content is lost once the application containing the WebView component is closed.

So, if we change "on-the-fly" a script or a css file inside a Webview, this file will not be reloaded/refreshed :-(
I've found another workaround, different (and it seems more simple) from that found from @Rany. I'll explain it hoping it can help someone...

Simply add at the end of the call to the script or stylesheet a custom parameter.
For example, instead to write this:

<script type="text/javascript" src="javascript.js"></script>

write this:

<script type="text/javascript" src="javascript.js?c=r_963"></script>

where "r_963" is a "r_" string followed by a random 0-999 int number. The final number must change its value everytime we need a refresh.
The same for stylesheet.
It works for me :-)

like image 129
Alessandro Avatar answered Nov 15 '22 06:11

Alessandro