Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/dox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"pngjs": "7.0.0",
"postcss": "8.5.31",
"postcss-html": "1.8.1",
"robots-parser": "3.0.1",
"stylelint": "17.16.0",
"stylelint-config-standard": "40.0.0",
"stylelint-declaration-strict-value": "1.12.1",
Expand Down
30 changes: 7 additions & 23 deletions apps/dox/scripts/build-utils/dist-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
* Every scanner is pure: text in, failures out. None reads the file system.
*/

import {
BASE,
isPageLinkWithoutSlash,
ORIGIN,
underBase,
} from "../lib/page-links.mjs";

export interface BaseFailure {
value: string;
where: string;
}

const ORIGIN = "https://northguild.dev";
const BASE = "/gmt";
/** The header's NorthGuild link (#424); equal to `LANDING_URL` in `src/lib/site.ts`. */
export const LANDING = `${ORIGIN}/`;
const LEGACY_HOST = "gmt-dox.northguild.workers.dev";

/** `/gmt` or `/gmt/…`, and not a doubled `/gmt/gmt`. */
function underBase(pathname: string): boolean {
if (pathname !== BASE && !pathname.startsWith(`${BASE}/`)) return false;
return !(pathname === `${BASE}/gmt` || pathname.startsWith(`${BASE}/gmt/`));
}

/**
* The reason a URL fails the rule, or `null` when it passes. Relative URLs, hashes, non-http
* schemes and other hosts pass.
Expand Down Expand Up @@ -144,21 +143,6 @@ export function badUrlsInHtml(html: string): BaseFailure[] {
return dedupe(out);
}

/**
* Whether a URL links to a Dox page without the trailing slash: root-absolute or on the site's
* origin, under `/gmt`, and with no dot in its last path segment (a dot names a file).
*/
function isPageLinkWithoutSlash(value: string): boolean {
let pathname: string;
if (value.startsWith("/") && !value.startsWith("//"))
pathname = value.split(/[?#]/)[0];
else if (value.startsWith(`${ORIGIN}/`))
pathname = value.slice(ORIGIN.length).split(/[?#]/)[0];
else return false;
if (!underBase(pathname) || pathname.endsWith("/")) return false;
return !pathname.slice(pathname.lastIndexOf("/") + 1).includes(".");
}

/**
* Every link (`<a>` or `<area>` href) to a Dox page that lacks the trailing slash the static host
* serves the page at. Such a link costs a redirect, and search engines crawl and report the page
Expand Down
131 changes: 114 additions & 17 deletions apps/dox/scripts/deploy-smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
*
* node apps/dox/scripts/deploy-smoke.mjs
*
* Node 22+, no dependencies. Rows 26-29 (the landing page, the zone and the landing's
* robots.txt) belong to the landing repo's deploy and are checked by hand after a deploy.
* Node 22+, one devDependency (`robots-parser`, through `robots-policy.mjs`). Rows 26-28 (the
* landing page and the zone) belong to the landing repo's deploy and are checked by hand after a
* deploy. The landing's robots.txt is checked here ("robots policy") against the table in
* `robots-policy.mjs`.
*
* Requests go through `node:https`, not `fetch`: undici adds its own `sec-fetch-mode`
* header, and the (N) rows must send exactly the headers a browser following a link sends.
Expand All @@ -18,11 +20,14 @@
*
* `DOX_SMOKE_LOCAL=http://localhost:8787` runs only the crawl-hygiene rows (`_headers` and the 404
* page) against a local `wrangler dev` that serves the built `dist`. The redirect rows need the
* live hosts and are skipped.
* live hosts and are skipped, the slash rows (6a, 6b) among them, which check the 308 the Worker
* makes from the assets binding's 307. The workers.dev
* `noindex` rows (6c) match a host name, which `localhost` is not.
*/
import { readFileSync } from "node:fs";
import { request as httpRequest } from "node:http";
import { request as httpsRequest } from "node:https";
import { checkRobots } from "./robots-policy.mjs";

const LOCAL = process.env.DOX_SMOKE_LOCAL?.replace(/\/$/, "");
const ND = LOCAL ?? "https://northguild.dev";
Expand Down Expand Up @@ -122,6 +127,40 @@ const hop = (rows, label, from, to, { headers, final = 200 } = {}) =>
steps: [{ status: 301, location: to }, { status: final }],
});

/**
* A row for a page path without its slash: one permanent hop (308) to the slash URL with the query
* kept, then the page. The one place the expected status of that hop is written.
*/
const slashHop = (rows, label, url) => {
const at = url.search(/[?#]/);
const path = at === -1 ? url : url.slice(0, at);
rows.push({
label,
url,
steps: [
{
status: 308,
location: `${path}/${at === -1 ? "" : url.slice(at)}`,
},
{ status: 200 },
],
});
};

/**
* The first page URL in the live sitemap that matches `re`: the first `<loc>` of the index, then the
* first match in that child sitemap. `undefined` when there is none, so a row can fail by name.
*/
async function firstSitemapUrl(re) {
const index = await send("GET", `${ND}/gmt/sitemap-index.xml`);
const child = /<loc>([^<]+)<\/loc>/.exec(index.body)?.[1];
if (child === undefined) return undefined;
const sitemap = await send("GET", child);
return [...sitemap.body.matchAll(/<loc>([^<]+)<\/loc>/g)]
.map((m) => m[1])
.find((loc) => re.test(loc));
}

/** The meta tag that asks a crawler not to index a page. */
const NOINDEX_META = /<meta\s[^>]*name="robots"[^>]*content="[^"]*noindex/i;

Expand Down Expand Up @@ -296,15 +335,57 @@ async function buildRows() {
}

hop(rows, "3", `${WD}/reference/`, `${ND}/gmt/reference/`);
add({
label: "6",
url: `${WD}/reference`,
steps: [
{ status: 301, location: `${ND}/gmt/reference` },
{ status: 307, location: `${ND}/gmt/reference/` },
{ status: 200 },
],
});
// The old host adds the page slash in its one 301, so an old link never chains.
hop(rows, "6", `${WD}/reference`, `${ND}/gmt/reference/`);
slashHop(rows, "6a", `${ND}/gmt/reference`);
// A function page from the live sitemap, never a name typed here: pages get renamed.
const fnPage = await firstSitemapUrl(
/\/gmt\/reference\/[^/]+\/[^/]+\/[^/]+\/$/,
);
if (fnPage === undefined) {
add({
label: "6b",
url: `${ND}/gmt/sitemap-index.xml`,
steps: [{ status: 200, check: () => "no function page in the sitemap" }],
});
} else {
const slashless = fnPage.slice(0, -1);
slashHop(rows, "6b", `${slashless}?a=1`);
hop(
rows,
"6d",
`${WD}${slashless.slice(`${ND}/gmt`.length)}?a=1`,
`${fnPage}?a=1`,
);
// The old host serves the same pages under `/gmt/`; only `northguild.dev` may be indexed.
// `wrangler dev` serves on `localhost`, where the host rule cannot match, so these are live only.
add({
label: "6c workers.dev noindex",
url: `${WD}${fnPage.slice(ND.length)}`,
steps: [
{
status: 200,
check: ({ headers }) =>
String(headers["x-robots-tag"] ?? "").includes("noindex")
? undefined
: `x-robots-tag is ${headers["x-robots-tag"]}, not noindex`,
},
],
});
add({
label: "6c canonical host",
url: fnPage,
steps: [
{
status: 200,
check: ({ headers }) =>
headers["x-robots-tag"] === undefined
? undefined
: `x-robots-tag is ${headers["x-robots-tag"]}`,
},
],
});
}
hop(rows, "7 install.md", `${WD}/install.md`, `${ND}/gmt/install.md`);
hop(rows, "7 llms.txt", `${WD}/llms.txt`, `${ND}/gmt/llms.txt`);
hop(
Expand Down Expand Up @@ -343,6 +424,22 @@ async function buildRows() {
url: `${WD}/robots.txt`,
steps: [{ status: 301, location: `${ND}/robots.txt` }],
});
add({
label: "robots policy",
url: `${ND}/robots.txt`,
steps: [
{
status: 200,
type: "text/plain",
check: ({ body }) => {
const failures = checkRobots(body);
return failures.length === 0
? undefined
: failures.map((f) => `${f.value}: ${f.where}`).join("; ");
},
},
],
});
add({
label: "13",
url: `${WD}/reference/duty.md`,
Expand All @@ -352,13 +449,13 @@ async function buildRows() {
{ status: 200 },
],
});
// A moved page without its slash: no dot and no slash, so the Worker runs and hands it to the
// assets binding, and only `_redirects` stands between it and the 404 page.
// A moved page without its slash: the old host's 301 adds the slash, and only `_redirects`
// stands between that URL and the 404 page.
add({
label: "13 no slash",
url: `${WD}/reference/duty`,
steps: [
{ status: 301, location: `${ND}/gmt/reference/duty` },
{ status: 301, location: `${ND}/gmt/reference/duty/` },
{ status: 301, location: `${ND}/gmt/reference/workforce/` },
{ status: 200 },
],
Expand Down Expand Up @@ -411,7 +508,7 @@ async function buildRows() {
url: `${ND}/gmt/nope`,
headers,
steps: [
{ status: 307, location: `${ND}/gmt/nope/`, optional: true },
{ status: 308, location: `${ND}/gmt/nope/`, optional: true },
{ status: 404, type: "text/html" },
],
});
Expand Down Expand Up @@ -843,7 +940,7 @@ async function buildRows() {
add({
label: "25",
url: `${ND}/gmtx`,
steps: [{ status: 301, location: `${ND}/gmt/gmtx` }, { status: 404 }],
steps: [{ status: 301, location: `${ND}/gmt/gmtx/` }, { status: 404 }],
});
add({
label: "30",
Expand Down
15 changes: 14 additions & 1 deletion apps/dox/scripts/dist-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,20 @@ describe("the built site keeps the /gmt base", () => {
.split("\n")
.filter((line) => line !== "" && !/^\s/.test(line) && line[0] !== "#");
expect(paths.length).toBeGreaterThan(0);
expect(paths.filter((path) => !path.startsWith("/gmt/"))).toEqual([]);
// One absolute rule is allowed: the old host, which keeps its copy of the pages out of search.
const OLD_HOST = "https://gmt-dox.northguild.workers.dev";
expect(
paths.filter((path) => !path.replace(OLD_HOST, "").startsWith("/gmt/")),
).toEqual([]);
});

it("_headers keeps the old workers.dev host's /gmt/ pages out of search results", () => {
const b = built();
if (b === null) return;
const headers = readFileSync(resolve(b.root, "_headers"), "utf8");
expect(headers).toMatch(
/^https:\/\/gmt-dox\.northguild\.workers\.dev\/gmt\/\*\n\s+X-Robots-Tag: noindex$/m,
);
});

it("every file under _astro/ has a content hash in its name, so its immutable header is true", () => {
Expand Down
9 changes: 9 additions & 0 deletions apps/dox/scripts/gates/registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ export const GATES = [
themes: false,
needs: null,
},
{
id: "rendered-links",
script: "rendered-links.mjs",
tier: "pr",
browsers: ["chromium"],
fullBrowsers: ["chromium"],
themes: false,
needs: null,
},
{
id: "frame-check",
script: "frame-check.mjs",
Expand Down
10 changes: 8 additions & 2 deletions apps/dox/scripts/gates/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe("the gate registry", () => {
"sound-check",
"rail-stored-still",
"canada-layout-check",
"rendered-links",
"frame-check",
"slides-check",
"a11y-axe",
Expand Down Expand Up @@ -78,10 +79,15 @@ describe("the gate registry", () => {
});

// `a11y-tab-walk` lists a subset of the built pages, `table-fit` lists the built pages that hold a
// table and `plate-fit` lists the built tool pages, so
// table, `plate-fit` lists the built tool pages and `rendered-links` the pages that hold a widget, so
// without `dist` each exits 2 before it reads a flag: its list fails, and its unknown-page case
// passes for the wrong reason.
const LISTS_FROM_DIST = new Set(["a11y-tab-walk", "table-fit", "plate-fit"]);
const LISTS_FROM_DIST = new Set([
"a11y-tab-walk",
"table-fit",
"plate-fit",
"rendered-links",
]);

// Each gate answers the page contract without a browser: a JSON list of site paths, and an unknown
// page is a usage error (exit 2), so a typo never passes as an empty run.
Expand Down
30 changes: 30 additions & 0 deletions apps/dox/scripts/lib/built-pages.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* The built pages whose HTML holds something.
*
* `listBuiltPages(distRoot, holds)` reads `<distRoot>/gmt/**\/index.html` and returns the site path
* (`/compare/behaviour/`, no `/gmt` prefix) of every page whose HTML `holds` accepts, sorted. The
* list is read from the build, never typed in, so a page that gains what the check looks for is
* covered without anyone adding it. `distRoot` is `apps/dox/dist`. `listTablePages` and
* `listWidgetPages` are two uses of it.
*/
import { readFileSync, readdirSync } from "node:fs";
import path from "node:path";

export function listBuiltPages(distRoot, holds) {
const site = path.join(distRoot, "gmt");
const out = [];
const walk = (dir, rel) => {
for (const entry of readdirSync(dir, { withFileTypes: true })) {
if (entry.isDirectory()) {
walk(path.join(dir, entry.name), `${rel}${entry.name}/`);
} else if (
entry.name === "index.html" &&
holds(readFileSync(path.join(dir, entry.name), "utf8"))
) {
out.push(rel);
}
}
};
walk(site, "/");
return out.sort();
}
Loading
Loading