|
10 | 10 | * the address Cloudflare serves it at). Nothing else in the page changes. |
11 | 11 | * - /sitemap.xml: every github.io address becomes the ascoding11.com one. |
12 | 12 | * - /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>. |
13 | 14 | * Any other host (ascoding11.github.io never runs this; *.pages.dev previews) and anything that isn't one of |
14 | 15 | * those three gets the response untouched, including every /api/* reply. |
15 | 16 | * |
|
21 | 22 | const GH = 'https://ascoding11.github.io'; |
22 | 23 | const COM = 'https://ascoding11.com'; |
23 | 24 | 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'; |
24 | 26 |
|
25 | 27 | function toCom(u) { |
26 | 28 | if (!u || !u.startsWith(GH + '/') && u !== GH) return u; |
@@ -54,7 +56,14 @@ export async function onRequest(context) { |
54 | 56 | } |
55 | 57 | if (!(res.headers.get('content-type') || '').includes('text/html')) return res; |
56 | 58 |
|
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 |
58 | 67 | .on('link[rel="canonical"]', fixAttr('href')) |
59 | 68 | .on('meta[property="og:url"]', fixAttr('content')) |
60 | 69 | .on('meta[name="twitter:url"]', fixAttr('content')) |
|
0 commit comments