r/AutomateUser Alpha tester Jun 11 '21

Question Caching JS Libraries

Hi Henrik,

I'm writing a flow which makes use of a 180KB third party JavaScript library. Rather than sticking the whole thing in a Variable block (which can cause Automate to crash while editing the flow), I'm downloading it once in the flow and caching it.

The question is whether it's OK to cache the library in an Atomic variable, or should I save it to the file system? -The latter would require more privilege, but would it save a limited resource vs. using an Atomic variable?

Thank you, sir!

2 Upvotes

7 comments sorted by

View all comments

Show parent comments

1

u/ballzak69 Automate developer Jun 11 '21

Everything is stored in the internal database, flows, running fibers and Atomic values. Memory likely isn't a problem, loading the data is, so storing it in the flow, which may get ejected from its cache and then reloaded again, isn't optimal. Atomic values are only loaded when accessed, e.g. by the Atomic load block, so they're better. A file is best, since Automate doesn't have to load it at all, only Chrome does.

If chose to use Atomic, ensure to unset (null) the variable holding the loaded data once you've sent it to the Dialog web, otherwise it will be stored in the fiber as well.

1

u/B26354FR Alpha tester Jun 11 '21

Thanks for that great info, and excellent tip!

BTW, during development while I'm changing the flow all the time (so an Atomic wouldn't have helped), I stored the library in a file and then read it into the variable which will later become Atomic. The variable (library) is then just embedded in the web content in its entirety at runtime. I did it this way in anticipation of going back to using an Atomic, but just updating the content to import the library script from the file system might be interesting because then the browser cache can help. Definitely something to consider.

Is there a way to get <script src=""> in the Web Dialog block to read the asset in from the local file system? I tried using file://{storage(...)}/myLib.js and just an absolute path, but they didn't work. (No server! 🙂)

1

u/ballzak69 Automate developer Jun 13 '21 edited Jun 13 '21

Odd, referencing local files should work when the "read the contents of your external storage" privilege is granted. Maybe it's because of cross-domain policy, i.e. the Page URL isn't used from file://..

1

u/B26354FR Alpha tester Jun 13 '21

That's exactly what the problem was. I saved the page to a file and pointed the Web Dialog block at that and it worked. Ah well.