在 Astro 中插入 Google Adsense 广告出错:adsbygoogle is not defined 的解决方法
5月28日申请了 Google Adsense 广告,30日就收到审核通过的邮件了。
赶紧去 Adsense 后台设置了一条广告,给的HTML代码如下:
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3757457639408805" crossorigin="anonymous"></script>
<!-- Blog-content-bottom -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-3757457639408805"
data-ad-slot="2335250184"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
赶紧加到网页代码里面,保存,预览,结果发现 Google Adsense 广告没显示出来,在控制台发现了一条出错信息和一条 Warning:
Uncaught ReferenceError: adsbygoogle is not defined
Adsense head tag doesn't support data-astro-exec attribute.

试着用 Cursor 并选择 Cursor 刚刚支持的 Deepseek R1 0528 分析了一下,加一个属性 is:inline 就解决了,修改后的代码如下:
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3757457639408805" crossorigin="anonymous"></script>
<!-- Blog-content-bottom -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-3757457639408805"
data-ad-slot="2335250184"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script
is:inline
>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
发布到线上,刷新页面,完美显示出来了 Google Adsense 广告!