Skip to content

Commit 8be07ed

Browse files
committed
Add AdSense site code to ascoding11.com's home and showcase (Cloudflare only)
The middleware adds the adsbygoogle.js snippet to the <head> of / and /showcase/ on ascoding11.com, so AdSense finds it whichever page its crawler lands on. ascoding11.github.io is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016ZY4GbLzrkZcWwD8xrStCd
1 parent 340e65d commit 8be07ed

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎functions/_middleware.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* the address Cloudflare serves it at). Nothing else in the page changes.
1111
* - /sitemap.xml: every github.io address becomes the ascoding11.com one.
1212
* - /robots.txt: the Sitemap line points at https://ascoding11.com/sitemap.xml.
13+
* - / and /showcase/: AdSense's site code (adsbygoogle.js with the publisher ID) is added to the <head>.
1314
* Any other host (ascoding11.github.io never runs this; *.pages.dev previews) and anything that isn't one of
1415
* those three gets the response untouched, including every /api/* reply.
1516
*
@@ -21,6 +22,7 @@
2122
const GH = 'https://ascoding11.github.io';
2223
const COM = 'https://ascoding11.com';
2324
const COM_HOST = /^(www\.)?ascoding11\.com$/;
25+
const ADSENSE = '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1712937102765568"\n crossorigin="anonymous"></script>\n';
2426

2527
function toCom(u) {
2628
if (!u || !u.startsWith(GH + '/') && u !== GH) return u;
@@ -54,7 +56,14 @@ export async function onRequest(context) {
5456
}
5557
if (!(res.headers.get('content-type') || '').includes('text/html')) return res;
5658

57-
return new HTMLRewriter()
59+
let rw = new HTMLRewriter();
60+
// AdSense's site code, in the <head> of the two pages a visit to ascoding11.com/ can land on (the home page,
61+
// and showcase/, where its script sends bare visits), so AdSense finds it whether or not it runs JavaScript.
62+
if (path === '/' || path === '/showcase/') {
63+
const html = await res.clone().text();
64+
if (!html.includes('adsbygoogle.js?client=')) rw = rw.on('head', { element(el) { el.append(ADSENSE, { html: true }); } });
65+
}
66+
return rw
5867
.on('link[rel="canonical"]', fixAttr('href'))
5968
.on('meta[property="og:url"]', fixAttr('content'))
6069
.on('meta[name="twitter:url"]', fixAttr('content'))

0 commit comments

Comments
 (0)