Rails adblock JS 文件在生产/开发中表现不同

Rails adblock JS file behaving differently in production / development

我有一个文件,ads.js 位于我 rails 的 assets/javascripts 文件夹中。它包含一行代码 - canRunAds = true,我使用下面的脚本在我的应用程序的其他地方查询。如果它 returns undefined,我会提供一条广告拦截消息。

它在开发中完美运行,当我测试它时,canRunAds 确实会 return undefined。然而,在生产中,打开 adblock - 它记录消息 Adblock not detected - 即它没有按预期工作。奇怪的是,我还得到 Chrome 告诉我 ads.js 文件的 ERR_BLOCKED_BY_CLIENT...所以文件被阻止了,但是 canRunAds 仍然 returning true,怎么样?

我已经预编译了我的资源,ads.js 文件肯定在里面。我对 rails 如何加载资产的了解非常有限,我不知道接下来要 Google 做什么来解决这个问题。有人告诉我 rails 生产有时会 "reload & autoload all the things whenever" 但这就是我必须继续的所有信息...

任何 help/pointers 将不胜感激!

    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;

终于想通了。

它在 development 中工作,因为 rails 正在单独提供像 ads.js 这样的静态资产文件(所以 adblock 获取文件名并阻止它)。我认为这是因为变量 config.serve_static_files = true 已设置(虽然在生产中为 false)。

在生产中,文件内容被铲入 application.js 因此文件名没有被提取。我将其放入 public 文件夹,结果成功了。