Skip to content

Commit 340e65d

Browse files
committed
Make ascoding11.com its own canonical address (Cloudflare only)
On ascoding11.com, a Pages middleware points canonical links, og:url, sitemap.xml and robots.txt at ascoding11.com instead of github.io, so Google (and AdSense) stop treating it as a copy of the github.io site. ascoding11.github.io is unchanged: Jekyll skips files starting with _, so neither new file is published there. _routes.json limits Function runs to /api/*, robots.txt, sitemap.xml and the public pages. No What's new entry: players can't see this change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016ZY4GbLzrkZcWwD8xrStCd
1 parent a1998f9 commit 340e65d

2 files changed

Lines changed: 91 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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
* Any other host (ascoding11.github.io never runs this; *.pages.dev previews) and anything that isn't one of
14+
* those three gets the response untouched, including every /api/* reply.
15+
*
16+
* GitHub Pages runs Jekyll, which skips files starting with "_", so this file and /_routes.json are never
17+
* published on ascoding11.github.io. /_routes.json limits which addresses run Functions at all (each run counts
18+
* toward Cloudflare's free daily allowance): /api/*, robots.txt, sitemap.xml and the public pages. The Back Room
19+
* is noindex and isn't listed. When adding a public page that's in sitemap.xml, add its path to /_routes.json too.
20+
*/
21+
const GH = 'https://ascoding11.github.io';
22+
const COM = 'https://ascoding11.com';
23+
const COM_HOST = /^(www\.)?ascoding11\.com$/;
24+
25+
function toCom(u) {
26+
if (!u || !u.startsWith(GH + '/') && u !== GH) return u;
27+
return COM + u.slice(GH.length).replace(/\/privacy\.html$/, '/privacy');
28+
}
29+
30+
function fixAttr(name) {
31+
return { element(el) { const v = el.getAttribute(name); if (v) el.setAttribute(name, toCom(v)); } };
32+
}
33+
34+
function textResponse(res, body) {
35+
const headers = new Headers(res.headers);
36+
headers.delete('content-length');
37+
headers.delete('etag');
38+
return new Response(body, { status: res.status, statusText: res.statusText, headers });
39+
}
40+
41+
export async function onRequest(context) {
42+
const res = await context.next();
43+
const url = new URL(context.request.url);
44+
if (!COM_HOST.test(url.hostname) || res.status !== 200) return res;
45+
46+
const path = url.pathname;
47+
if (path === '/robots.txt') {
48+
const t = await res.text();
49+
return textResponse(res, t.replace(/^Sitemap:.*$/im, 'Sitemap: ' + COM + '/sitemap.xml'));
50+
}
51+
if (path === '/sitemap.xml') {
52+
const t = await res.text();
53+
return textResponse(res, t.replace(/<loc>([^<]*)<\/loc>/g, (m, u) => '<loc>' + toCom(u) + '</loc>'));
54+
}
55+
if (!(res.headers.get('content-type') || '').includes('text/html')) return res;
56+
57+
return new HTMLRewriter()
58+
.on('link[rel="canonical"]', fixAttr('href'))
59+
.on('meta[property="og:url"]', fixAttr('content'))
60+
.on('meta[name="twitter:url"]', fixAttr('content'))
61+
.transform(res);
62+
}

0 commit comments

Comments
 (0)