Fork me on GitHub
#shadow-cljs
<
2020-02-08
>
thheller10:02:28

@pri one guess: you are using deps.edn or project.clj but configured said path in shadow-cljs.edn?

pre21:02:06

The project structure is from a "working example" off a blog. source-path is set to src and deps in deps.edn, in shadow-cljs. In deps.edn the paths var is set to src.

Gleb Posobin22:02:24

I am using shadow-cljs to generate a chrome extension. The content script does not seem to hot reload, while the background script reloads fine on file updates. I.e. init-fn in content script continues to println the same thing after page refresh it did when I loaded the extension in chrome. What could be wrong?

Gleb Posobin22:02:52

But when I unload and then load the extension again in Chrome, the content script behaves differently. So it compiles the changes, but they for some reason are not picked up by the running extension.

darwin23:02:08

I’m not familiar with shadow-cljs support for chrome extensions, but in general hot reloading content script is really tricky, because eval of dynamic code prohibiter or restricted https://developer.chrome.com/extensions/contentSecurityPolicy#interactions

darwin23:02:44

I don’t remeber the details but I ended up not supporting hot reloading of content scripts in chromex library.

darwin23:02:37

I just briefly glanced over shadow-cljs sources and it looks like @thheller added support for hot-reloading of content script - maybe the feature is just broken for you

Gleb Posobin23:02:44

I thought that the content security policy might be a problem. There is this message by @thheller with an example of a CSP that I thought would fix that, but it did not: https://github.com/thheller/shadow-cljs/issues/279#issuecomment-392007641

darwin23:02:53

maybe Chrome devs tightened the rules and what worked before no longer works, at least they have been nagging my chrome extension to review requrested permissions and explain them to the review team

Gleb Posobin23:02:56

Hmm, interesting, so the stuff in the function that I marked with ^:dev/after-load is indeed reloaded, but not in the init function.

Gleb Posobin23:02:39

So when I do this:

(defn ^:dev/after-load reloaded []
  (println "The content script was reloaded!"))

(defn init []
  (reloaded))
After changing the string in the reloaded function, the new string is printed in the console of the page. But when I reload the page after that, the old string gets printed.

Gleb Posobin23:02:27

Looks like the previous version of the content script is loaded on page refresh.

darwin23:02:05

make sure that the genearated source javascript file on disk is actually what you expect, this will rule out shadow-cljs not recompiling the file properly and the problem will be chrome caching the content script file itself somewhere (likely)

Gleb Posobin23:02:52

The file on disk is updated correctly.

darwin23:02:23

not sure how to tell chrome to reload whole content script javascript file

darwin23:02:50

manually you can go to extensions page and reload the extension via UI, I would expect that to load new version of on-disk file

Gleb Posobin23:02:26

Yes, that does reload the file correctly.

darwin23:02:27

you might also try to open devtools and check disabling caching in DevTools Settings (F1)

darwin23:02:47

I mean disabling network caching when DevTools is open

darwin23:02:10

not sure if this would apply to content scripts as well, probaly not, but worth trying

Gleb Posobin23:02:13

That flag doesn't help =/

darwin23:02:51

ok, that is only related to page network requests, not extensions see this: https://bugs.chromium.org/p/chromium/issues/detail?id=112471

darwin23:02:05

but since shadow-cljs already knows how to do hot reloading in theory it could add a workaround for this issue, content script init code could contain some test for stale content and request a clean hot-reload for you

Gleb Posobin23:02:51

Adding :module-hash-names true to the shadow-cljs config helped with that.

Gleb Posobin23:02:25

@darwin Thank you for your help!

darwin23:02:18

good to know! maybe open an issue with this workaround so this knowledge does not get lost here on slack btw. I started with shadow-cljs just recently. I want to look into this :chrome-extension target soon, it looks like a hidden gem 🙂 … and maybe create an example shadow-cljs app using chromex

8
rberger02:02:58

That would be a dream come true!

Gleb Posobin23:02:23

Ah, no, it didn't help facepalm

darwin23:02:54

😞 you need to kick chrome extensions subsystem somehow, to reload the files

Gleb Posobin23:02:56

Yes, an example would be great! I struggled the first day or so to set everything up.

darwin23:02:02

otherwise it will assume they didn’t change

darwin23:02:19

maybe you can do custom hack yourself, simple http get the js file in content script init code and eval it (the same way shadow-cljs evals the code during hot-reload)

darwin23:02:53

it is dirty, but it should work