fix: configure Google Ads for Vercel

This commit is contained in:
ssbg04 committed 2026-10-23 19:53:38 +08:00
1 parent 00ed9f2992
commit 4cb4a46def
6 files changed
+25 -8

No files matched your search

+1 -1
View File
@@ -27,4 +27,4 @@ VITE_ADSENSE_CLIENT=ca-pub-XXXXXXXXXXXXXXXX
VITE_ADSENSE_SLOT=XXXXXXXXXX
```
Add the same values to a local `.env.local` file when testing. The AdSense script is loaded globally, while ad slots render only when both values are present. Use the exact publisher and slot IDs from AdSense; do not use guessed or test IDs in production.
Add the same values to a local `.env.local` file when testing. For Vercel, open **Project Settings → Environment Variables**, add both variables for **Production**, **Preview**, and **Development** as needed, then redeploy. Because these are `VITE_` variables, Vite injects them at build time; changing them requires a new deployment. The AdSense script and ad slots render only when both values are present. Use the exact publisher and slot IDs from AdSense; do not use guessed or test IDs in production.
-1
View File
@@ -33,7 +33,6 @@
<!-- PWA & Mobile Web App Meta Tags -->
<link rel="manifest" href="/manifest.json" />
<meta name="theme-color" content="#0a0a0a" />
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
+1 -1
View File
@@ -3,4 +3,4 @@
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://pagead2.googlesyndication.com https://googleads.g.doubleclick.net https://ep2.adtrafficquality.google https://media.ethicalads.io blob:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com data:; img-src 'self' data: blob: https:; media-src 'self' data: blob: https:; connect-src 'self' https: blob: data:; frame-src 'self' https://googleads.g.doubleclick.net https://tpc.googlesyndication.com; worker-src 'self' blob:; object-src 'none'; frame-ancestors 'none'; base-uri 'self';
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://pagead2.googlesyndication.com https://googleads.g.doubleclick.net https://ep2.adtrafficquality.google blob:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com data:; img-src 'self' data: blob: https:; media-src 'self' data: blob: https:; connect-src 'self' https: blob: data:; frame-src 'self' https://googleads.g.doubleclick.net https://tpc.googlesyndication.com https://www.google.com; worker-src 'self' blob:; object-src 'none'; frame-ancestors 'none'; base-uri 'self';
+12 -4
View File
@@ -26,10 +26,18 @@ export const AdSlot: React.FC<AdSlotProps> = ({ placement, className = '' }) =>
useEffect(() => {
if (!canRenderAd || !adRef.current || typeof window === 'undefined') return;
try {
(window.adsbygoogle = window.adsbygoogle || []).push({});
} catch {
// Ad blockers and unapproved accounts can prevent the ad request.
const pushAd = () => {
try {
(window.adsbygoogle = window.adsbygoogle || []).push({});
} catch {
return;
}
};
if (document.querySelector('script[data-adsense-loader]')) {
pushAd();
} else {
window.setTimeout(pushAd, 250);
}
}, [canRenderAd]);
+10
View File
@@ -3,6 +3,16 @@ import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App.tsx';
const adsenseClient = import.meta.env.VITE_ADSENSE_CLIENT;
if (adsenseClient && typeof document !== 'undefined' && !document.querySelector('script[data-adsense-loader]')) {
const script = document.createElement('script');
script.async = true;
script.src = `https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=${encodeURIComponent(adsenseClient)}`;
script.crossOrigin = 'anonymous';
script.dataset.adsenseLoader = 'true';
document.head.appendChild(script);
}
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
+1 -1
View File
@@ -21,7 +21,7 @@
},
{
"key": "Content-Security-Policy-Report-Only",
"value": "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://media.ethicalads.io blob:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com data:; img-src 'self' data: blob: https:; media-src 'self' data: blob:; connect-src 'self' https: blob: data:; worker-src 'self' blob:; object-src 'none'; frame-ancestors 'none'; base-uri 'self';"
"value": "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://pagead2.googlesyndication.com https://googleads.g.doubleclick.net https://ep2.adtrafficquality.google blob:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com data:; img-src 'self' data: blob: https:; media-src 'self' data: blob: https:; connect-src 'self' https: blob: data:; frame-src 'self' https://googleads.g.doubleclick.net https://tpc.googlesyndication.com https://www.google.com; worker-src 'self' blob:; object-src 'none'; frame-ancestors 'none'; base-uri 'self';"
}
]
}