Dev.to WebDev 🛠 Dev 👁 0 📖 6 min read

Two new kinds of page meant edits in ten places, and only two of them were compile errors

Every public page on nakodo.app that is not hand-built is a typed object in one registry. 90 of them, in ten families, rendered by one component and listed in one sitemap of 106 URLs. Last week I added two families: fea

Every public page on nakodo.app that is not hand-built is a typed object in one registry. 90 of them, in ten families, rendered by one component and listed in one sitemap of 106 URLs.

Last week I added two families: features and templates. Each needed a hub page, a dynamic route, a social card, a sitemap entry, a place in the navigation and a place in the auth allowlist. The interesting part was not writing the pages. It was finding out which of the ten places I had to touch would tell me if I forgot.

The union is the whole system

export type Kind = "guide" | "niche" | "tiktok-niche" | "instagram-niche" | "audience" | "tool" | "post" | "alternative" | "feature" | "template";

Two string literals longer than it was. The counts behind them today: guides 28, alternatives 12, YouTube niches 12, audiences 8, templates 6, blog posts 6, features 5, calculators 5, TikTok niches 4, Instagram niches 4.

Two structures are keyed by that union, and both of them are Record<Kind, ...>, which is the part that does the work:

export const KINDS: Record<Kind, { base: string; name: string; hub: { name: string; path: string } | null }> = {
  guide: { base: "/guides", name: "Guides", hub: { name: "Guides", path: "/guides" } },
  // ...
  feature: { base: "/features", name: "Features", hub: { name: "Features", path: "/features" } },
  template: { base: "/templates", name: "Templates", hub: { name: "Templates", path: "/templates" } },
};
const PRIORITY: Record<Kind, number> = {
  guide: 0.7,
  // ...
  feature: 0.8,
  template: 0.7,
};

I checked what happens when you forget them rather than assuming, by deleting both lines in a scratch worktree:

src/content/registry.ts(23,14): error TS2741: Property 'feature' is missing in type
  '{ guide: ...; "tiktok-niche": ...; 5 more ...; template: ...; }'
  but required in type 'Record<Kind, { base: string; name: string; hub: ... | null; }>'.

src/app/sitemap.ts(5,7): error TS2741: Property 'feature' is missing in type
  '{ guide: number; tool: number; ...; template: number; }'
  but required in type 'Record<Kind, number>'.

Record<Kind, T> rather than Partial<Record<Kind, T>> or an object with an inferred type is the entire reason those two are free. An inferred object would have compiled, and a missing sitemap priority would have become undefined in an XML field.

The eight places that say nothing

Here is the honest list of what else a new kind needs, with what tells you when it is missing.

The ENTRIES spread. Nothing.

export const ENTRIES: Entry[] = [
  ...entries("guide", GUIDES),
  // ...
  ...entries("feature", FEATURES),
  ...entries("template", TEMPLATES),
];

Forget this and the kind compiles, the record is complete, the route exists, and generateStaticParams returns an empty array, so the pages are simply not built. The hub lists nothing. There is no error anywhere, because an empty family is a legal family.

The two lists in the auth allowlist. A test, and it is the one I rely on most:

const PUBLIC_EXACT = new Set([..., "/features", "/templates", "/tools", ...]);
const PUBLIC_PREFIXES = [..., "/features/", "/templates/", ...];

The hub path goes in the exact set, the children in the prefixes. Deleting both lines fails a test that walks the sitemap:

✖ every page in the sitemap is open to signed-out visitors
  AssertionError: /features would redirect crawlers to /login

The route folder. A 404, which is loud enough. features/page.tsx for the hub and features/[slug]/page.tsx for the pages, both one-liners over the registry:

export const dynamicParams = false;
export const generateStaticParams = () => entriesOf("feature").map((e) => ({ slug: e.page.slug }));

The hub's own sitemap entry. Nothing. Hubs are hand-built pages, so they live in a second array with a name, a description for llms.txt and the date the page last changed in substance. A hub missing from it works perfectly and is invisible to crawlers.

The header and the footer. Nothing, and this is the one that actually matters for the pages being found. Features went into the top navigation and the "Company" footer group; templates and the tools hub went into the "Guides" group. The footer's five groups now carry 37 links on every page, up from 33: three I added by hand, and a fourth that arrived on its own, because the "Product" group spreads entriesOf("tool") and a fifth calculator shipped in the same commit.

The proxy matcher. Nothing, and it cannot be anything.

The regex Next reads before your code runs

Our session check skips public pages entirely, using Next's matcher config. The comment above it explains why it looks the way it does:

export const config = {
  // A public page missing here only costs a session check. Matchers must be
  // literals (Next reads them at build time), hence the one long pattern.
  matcher: [
    "/((?!_next/static|_next/image|alcy/|api/cron|...|(?:guides|blog|youtubers|tiktokers|instagrammers|alternatives|features|templates|tools)(?:/|(?:\\.rsc|\\.segments/.+)?$)|(?:for|og|o)/|.*\\.(?:svg|png|jpg|...)$).*)",
  ],
};

That cannot be generated from KINDS, because Next parses the file statically to build its routing manifest and a computed value is not there to read. So the one list that has to stay in step with the registry is the one list that cannot import it.

The two new kinds needed one edit each inside the alternation, and tools needed a move. It had been in the group of prefixes written as (?:for|tools|og|o)/, which only matches tools/ with a slash. Now that /tools is a page in its own right, it belongs in the group whose suffix is (?:/|...$), which matches both the bare path and anything under it. Miss that distinction and the hub page silently starts paying for a session check it does not use.

The four-line check I did not have

While writing this I realised the matcher is checkable after all. You cannot generate it, but you can read it back and assert over it:

const src = await readFile("src/proxy.ts", "utf8");
const pattern = src.match(/matcher: \[\s*"([\s\S]*?)",\s*\]/)[1].replace(/\\\\/g, "\\");
const re = new RegExp(`^${pattern}$`, "i");
const paths = [...STATIC_PAGES.map((p) => p.path), ...ENTRIES.map((e) => e.path)];
console.log({ total: paths.length, proxyRunsOn: paths.filter((p) => re.test(p)).length });

The replace(/\\\\/g, "\\") is the only awkward bit: the pattern is a TypeScript string literal, so every backslash in it is doubled in the source. The result:

{ total: 106, proxyRunsOn: 0 }

All 106 public pages skip the session check, which is what I hoped and had never verified. And the paths you would want to still run it do:

/tools      skipped
/features   skipped
/templates  skipped
/og/pricing skipped
/robots.txt skipped
/campaigns  proxy runs
/login      proxy runs
/nope       proxy runs
/guidesx    proxy runs

/login running the check is deliberate: the sign-in flow is public but needs the database. /nope and /guidesx run it because they match nothing, which is how an unknown path reaches the 404 rather than the login page.

That block is going in the test file next to the allowlist test. It closes the last of the eight places that said nothing, and it took four lines, which is roughly the amount of effort I had been implicitly claiming was impossible for the last two months.

What the ratio actually was

Ten places. Two compile errors, two covered by one existing test, one a 404 you cannot miss, and five that stay silent: the entries spread, the hub's sitemap entry, two navigation lists and the matcher. Of those five, the matcher is now checkable and the rest are checkable the same way: assert that every Kind has at least one entry, a hub route that answers 200, and a STATIC_PAGES row if KINDS says it has a hub.

The lesson I keep relearning is that Record<Kind, T> is the cheapest safety in the codebase and it only covers the things that are naturally keyed by the union. Everything that is a list, a string, a folder name or a regex is outside it, and for those you get to choose between a test and a memory. I had been choosing memory for five of the ten.

If you want to see what the two new families came out like: features is five pages, one per part of the product, and templates is six pages of messages you can copy, both in the same shape as the guides that were already there.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.