本地测试时,需要开启测试模式,所以需要在脚本中添加
'data-adbreak-test': 'on'
。
但在生产环境中需要删除该属性。
开发人员:
产品
每次上线前都需要注释掉。操作太繁琐,很容易忘记评论。导致生产环境出现错误。是否有类似于
'off'
的值来禁用测试模式?这样的话,我可以通过Nuxt环境变量来控制是否需要开启测试模式。
我检查了文档,除了
'on'
google adsense doc之外,没有发现任何其他值
或者有没有其他方式可以自动控制,无需手动标注?
我已经尝试了
'data-adbreak-test': 'off'
和'data-adbreak-test': ''
,但没有效果
// Check if we are in the production environment
const isProduction = process.env.NODE_ENV === 'production';
// Get a reference to the script element
const script = document.getElementById('your-script-id'); // Replace 'your-script-id' with the actual ID of your script element
// Conditionally add or remove the attribute
if (!isProduction) {
script.setAttribute('data-adbreak-test', 'on');
} else {
script.removeAttribute('data-adbreak-test');
}
You can try this