Skip to content

Commit 694f281

Browse files
committed
Merge branch 'upmain' into coin-economy
2 parents 6f70bb1 + 8be07ed commit 694f281

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

‎_routes.json‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"version": 1,
3+
"description": "Which addresses run Pages Functions on ascoding11.com: the API, plus robots.txt, sitemap.xml and the public pages so functions/_middleware.js can point their canonical links at ascoding11.com. Everything else is served as a plain file. Never published on ascoding11.github.io (Jekyll skips files starting with _).",
4+
"include": [
5+
"/api/*",
6+
"/robots.txt",
7+
"/sitemap.xml",
8+
"/",
9+
"/projects/",
10+
"/hype-cycle-exchange/",
11+
"/boxed/",
12+
"/productivity/",
13+
"/docket/",
14+
"/prompted/",
15+
"/engineering/",
16+
"/harvest-steps/",
17+
"/daedalus/",
18+
"/marketplace/",
19+
"/privacy",
20+
"/privacy.html",
21+
"/hype-cycle-exchange/guide/",
22+
"/boxed/guide/",
23+
"/docket/guide/",
24+
"/prompted/guide/",
25+
"/about/",
26+
"/showcase/"
27+
],
28+
"exclude": []
29+
}

‎functions/_middleware.js‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Makes ascoding11.com its own main address for Google, without changing anything on ascoding11.github.io.
3+
*
4+
* Both addresses serve the same files from main, and every page's canonical link, og:url, the sitemap and
5+
* robots.txt name ascoding11.github.io. That is right for github.io, but on ascoding11.com it tells Google the
6+
* "real" page lives on github.io, so ascoding11.com looks like a copy (a problem for AdSense, which reviews
7+
* ascoding11.com). This Cloudflare Pages middleware rewrites those few things as pages leave ascoding11.com:
8+
* - HTML pages: <link rel="canonical">, <meta property="og:url"> and <meta name="twitter:url"> that point at
9+
* https://ascoding11.github.io/... now point at https://ascoding11.com/... (privacy.html becomes /privacy,
10+
* the address Cloudflare serves it at). Nothing else in the page changes.
11+
* - /sitemap.xml: every github.io address becomes the ascoding11.com one.
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>.
14+
* Any other host (ascoding11.github.io never runs this; *.pages.dev previews) and anything that isn't one of
15+
* those three gets the response untouched, including every /api/* reply.
16+
*
17+
* GitHub Pages runs Jekyll, which skips files starting with "_", so this file and /_routes.json are never
18+
* published on ascoding11.github.io. /_routes.json limits which addresses run Functions at all (each run counts
19+
* toward Cloudflare's free daily allowance): /api/*, robots.txt, sitemap.xml and the public pages. The Back Room
20+
* is noindex and isn't listed. When adding a public page that's in sitemap.xml, add its path to /_routes.json too.
21+
*/
22+
const GH = 'https://ascoding11.github.io';
23+
const COM = 'https://ascoding11.com';
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';
26+
27+
function toCom(u) {
28+
if (!u || !u.startsWith(GH + '/') && u !== GH) return u;
29+
return COM + u.slice(GH.length).replace(/\/privacy\.html$/, '/privacy');
30+
}
31+
32+
function fixAttr(name) {
33+
return { element(el) { const v = el.getAttribute(name); if (v) el.setAttribute(name, toCom(v)); } };
34+
}
35+
36+
function textResponse(res, body) {
37+
const headers = new Headers(res.headers);
38+
headers.delete('content-length');
39+
headers.delete('etag');
40+
return new Response(body, { status: res.status, statusText: res.statusText, headers });
41+
}
42+
43+
export async function onRequest(context) {
44+
const res = await context.next();
45+
const url = new URL(context.request.url);
46+
if (!COM_HOST.test(url.hostname) || res.status !== 200) return res;
47+
48+
const path = url.pathname;
49+
if (path === '/robots.txt') {
50+
const t = await res.text();
51+
return textResponse(res, t.replace(/^Sitemap:.*$/im, 'Sitemap: ' + COM + '/sitemap.xml'));
52+
}
53+
if (path === '/sitemap.xml') {
54+
const t = await res.text();
55+
return textResponse(res, t.replace(/<loc>([^<]*)<\/loc>/g, (m, u) => '<loc>' + toCom(u) + '</loc>'));
56+
}
57+
if (!(res.headers.get('content-type') || '').includes('text/html')) return res;
58+
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
67+
.on('link[rel="canonical"]', fixAttr('href'))
68+
.on('meta[property="og:url"]', fixAttr('content'))
69+
.on('meta[name="twitter:url"]', fixAttr('content'))
70+
.transform(res);
71+
}

0 commit comments

Comments
 (0)