I have a single file, ads.js
located in my assets/javascripts
folder in rails. It contains one line of code - canRunAds = true
, which I query elsewhere in my app with the script below. If it returns undefined
, I serve up an adblock message.
It works perfectly in development, when I'm testing it, and canRunAds
will indeed return undefined
. In production, however, with adblock turned on - it logs the message Adblock not detected
- i.e. it doesn't work as expected. Weirdly, I also get Chrome telling me ERR_BLOCKED_BY_CLIENT
for the ads.js
file... so the file is being blocked, but canRunAds
is still returning true
, how?
I've precompiled my assets, and the ads.js
file is definitely in there. My knowledge on how rails loads assets is limited enough that I don't know what to Google next to resolve this. Someone told me rails production will sometimes "reload & autoload all the things whenever" but that's all the info I have to go on...
Any help/pointers would be much appreciated!
function detectAdblock(){
if( window.canRunAds === undefined){
console.log("Adblock detected")
var donateBanners = document.getElementsByClassName("donations");
for (i = 0; i < donateBanners.length; i++) {
donateBanners[i].style.display = "block";
console.log("Banner displayed! " + i)
}
}
else {
console.log("Adblock not detected");
}
}
window.onload = detectAdblock;
Figured it out eventually.
It was working in development
because rails was serving up static asset files like ads.js
individually (so adblock picked up on the filename and blocked it). I think this is because the variable config.serve_static_files = true
is set (whilst being false in production).
In production, the file contents were being shovelled into application.js
so the filename wasn’t being picked up. I threw it into the public
folder and that did the trick.