@gusnips/vite 0.4.5 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +24 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +89 -1
- package/dist/node.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/node.test.ts +77 -3
- package/src/node.ts +106 -1
package/README.md
CHANGED
|
@@ -111,6 +111,19 @@ text-fitting maths. When copy does not fit, it **records the overflow and refuse
|
|
|
111
111
|
rather than appending an ellipsis — an ellipsis makes every string "fit", so copy that outgrew
|
|
112
112
|
its column has no failing case and ships.
|
|
113
113
|
|
|
114
|
+
`assertOgImages("dist", origin)` reads every page the build wrote and checks that each share card
|
|
115
|
+
it advertises is a file that exists. Run it last:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
await assertOgImages("dist", "https://example.com");
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Three codebases shipped a `404.html` whose `og:image` named a card nothing had ever rendered —
|
|
122
|
+
the card generator walks the page registry, and a not-found shell is not in it. Every share of a
|
|
123
|
+
dead link unfurled broken, and nothing in a browser shows it. `bakeHead` cannot catch this: it
|
|
124
|
+
writes the image it is handed, and a shell carrying the brand card is correct. The build can,
|
|
125
|
+
because by then the card is on disk or it is not. Cards on another host are skipped.
|
|
126
|
+
|
|
114
127
|
`loadTemplate` refuses a `dist/index.html` that is already a rendered page. `dist/index.html` is
|
|
115
128
|
both the file every page is baked _from_ and the home page's own output, so a second pass over a
|
|
116
129
|
written `dist/` reads a finished page as its blank and nests one render inside another. `vite
|
package/dist/index.d.ts
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
export { resolveKey } from "./catalog.ts";
|
|
19
19
|
export { assertRendered, bakeHead, EMPTY_ROOT, ogLocale, type Alternate, type HeadTags, type RenderedChecks, } from "./head.ts";
|
|
20
|
-
export { loadRenderer, loadTemplate, writeDist, writeOgCards, type OgCard, type WriteOgCardsOptions, } from "./node.ts";
|
|
20
|
+
export { assertOgImages, loadRenderer, loadTemplate, writeDist, writeOgCards, type OgCard, type WriteOgCardsOptions, } from "./node.ts";
|
|
21
21
|
export { describeOverflow, fitText, OG_CANVAS, type FitOptions, type FitResult, type OgOverflow, } from "./og.ts";
|
|
22
22
|
export type { PageRenderer } from "./render.ts";
|
|
23
23
|
export { ogImagePath, pageFile, pageSlug, robotsTxt, siteOrigin, sitemapFor, sitemapXml, type PublicPage, type RobotsOptions, type SitemapEntry, } from "./sitemap.ts";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EACL,cAAc,EACd,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,cAAc,GACpB,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,YAAY,EACZ,KAAK,MAAM,EACX,KAAK,mBAAmB,GACzB,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,gBAAgB,EAChB,OAAO,EACP,SAAS,EACT,KAAK,UAAU,EACf,KAAK,SAAS,EACd,KAAK,UAAU,GAChB,MAAM,SAAS,CAAC;AAKjB,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EACL,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,UAAU,EACV,UAAU,EACV,UAAU,EACV,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,YAAY,GAClB,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EACL,cAAc,EACd,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,cAAc,GACpB,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,YAAY,EACZ,KAAK,MAAM,EACX,KAAK,mBAAmB,GACzB,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,gBAAgB,EAChB,OAAO,EACP,SAAS,EACT,KAAK,UAAU,EACf,KAAK,SAAS,EACd,KAAK,UAAU,GAChB,MAAM,SAAS,CAAC;AAKjB,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EACL,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,UAAU,EACV,UAAU,EACV,UAAU,EACV,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,YAAY,GAClB,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
export { resolveKey } from "./catalog.js";
|
|
19
19
|
export { assertRendered, bakeHead, EMPTY_ROOT, ogLocale, } from "./head.js";
|
|
20
|
-
export { loadRenderer, loadTemplate, writeDist, writeOgCards, } from "./node.js";
|
|
20
|
+
export { assertOgImages, loadRenderer, loadTemplate, writeDist, writeOgCards, } from "./node.js";
|
|
21
21
|
export { describeOverflow, fitText, OG_CANVAS, } from "./og.js";
|
|
22
22
|
export { ogImagePath, pageFile, pageSlug, robotsTxt, siteOrigin, sitemapFor, sitemapXml, } from "./sitemap.js";
|
|
23
23
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EACL,cAAc,EACd,QAAQ,EACR,UAAU,EACV,QAAQ,GAIT,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,YAAY,GAGb,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,gBAAgB,EAChB,OAAO,EACP,SAAS,GAIV,MAAM,SAAS,CAAC;AAMjB,OAAO,EACL,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,UAAU,EACV,UAAU,EACV,UAAU,GAIX,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EACL,cAAc,EACd,QAAQ,EACR,UAAU,EACV,QAAQ,GAIT,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,YAAY,GAGb,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,gBAAgB,EAChB,OAAO,EACP,SAAS,GAIV,MAAM,SAAS,CAAC;AAMjB,OAAO,EACL,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,UAAU,EACV,UAAU,EACV,UAAU,GAIX,MAAM,cAAc,CAAC"}
|
package/dist/node.d.ts
CHANGED
|
@@ -54,4 +54,28 @@ export interface WriteOgCardsOptions<Page> {
|
|
|
54
54
|
* operator round the loop six times.
|
|
55
55
|
*/
|
|
56
56
|
export declare function writeOgCards<Page>({ pages, outDir, file, card, check, }: WriteOgCardsOptions<Page>): Promise<string[]>;
|
|
57
|
+
/**
|
|
58
|
+
* Every share card a built page advertises is a file that exists.
|
|
59
|
+
*
|
|
60
|
+
* Three repos shipped a page whose `og:image` named a card nothing had ever rendered. All three
|
|
61
|
+
* were `404.html`, all three for the same reason: a card generator walks the page registry, and
|
|
62
|
+
* a not-found shell is not in the registry, so a head built from the same template inherits a
|
|
63
|
+
* card address with no card behind it. Every share of a dead link unfurled broken, in one case
|
|
64
|
+
* in three languages, and **nothing in a browser shows it** — the page looks perfect, and the
|
|
65
|
+
* only reader who finds out is whoever the link was sent to.
|
|
66
|
+
*
|
|
67
|
+
* `bakeHead` cannot catch it: it is handed an `image` and writes it, and a shell carrying the
|
|
68
|
+
* brand card is correct, so it has no way to tell a right image from a wrong one (invariant 12).
|
|
69
|
+
* The build can, because by then the cards are either on disk or they are not. Run it last,
|
|
70
|
+
* after the pages and the cards are written:
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* await assertOgImages("dist", "https://example.com");
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* Cards on another host are somebody else's to serve and are skipped — but a build whose pages
|
|
77
|
+
* all advertise cards and whose origin matches NONE of them has been given the wrong origin,
|
|
78
|
+
* and a check that silently passes is worse than no check, so that is an error too.
|
|
79
|
+
*/
|
|
80
|
+
export declare function assertOgImages(distDir: string, origin: string): Promise<number>;
|
|
57
81
|
//# sourceMappingURL=node.d.ts.map
|
package/dist/node.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,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,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAiCrF"}
|
package/dist/node.js
CHANGED
|
@@ -19,11 +19,12 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
19
19
|
* policy the product owns. What is identical in every one of them is these four functions and
|
|
20
20
|
* the gate in `assertRendered`, so those ship and the ~40-line loop stays in the app.
|
|
21
21
|
*/
|
|
22
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
22
|
+
import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
23
23
|
import { dirname, join } from "node:path";
|
|
24
24
|
import { pathToFileURL } from "node:url";
|
|
25
25
|
import { EMPTY_ROOT } from "./head.js";
|
|
26
26
|
import { describeOverflow } from "./og.js";
|
|
27
|
+
import { siteOrigin } from "./sitemap.js";
|
|
27
28
|
/**
|
|
28
29
|
* The built `index.html`, and a refusal to bake one twice.
|
|
29
30
|
*
|
|
@@ -101,4 +102,91 @@ export async function writeOgCards({ pages, outDir, file, card, check = false, }
|
|
|
101
102
|
await writeFile(join(outDir, entry.file), entry.png);
|
|
102
103
|
return laid.map((entry) => entry.file);
|
|
103
104
|
}
|
|
105
|
+
const CARD_META = /<meta[^>]+(?:property|name)="(?:og:image|twitter:image)"[^>]+content="([^"]*)"[^>]*>/gi;
|
|
106
|
+
function advertisedCards(page, html) {
|
|
107
|
+
const seen = new Set();
|
|
108
|
+
const cards = [];
|
|
109
|
+
for (const match of html.matchAll(CARD_META)) {
|
|
110
|
+
const url = match[1];
|
|
111
|
+
if (url && !seen.has(url)) {
|
|
112
|
+
seen.add(url);
|
|
113
|
+
cards.push({ page, url });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return cards;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The path a card URL points at inside `dist`, or null when the card is not this build's to
|
|
120
|
+
* serve. A `?v=2` or a `#` is addressing, not a file name, so both come off.
|
|
121
|
+
*/
|
|
122
|
+
function distPathOf(url, origin) {
|
|
123
|
+
if (url.startsWith("/"))
|
|
124
|
+
return decodeURIComponent(url.split(/[?#]/)[0] ?? "");
|
|
125
|
+
try {
|
|
126
|
+
const parsed = new URL(url);
|
|
127
|
+
return parsed.origin === origin ? decodeURIComponent(parsed.pathname) : null;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
// Not an address at all. A card URL relative to the page's own folder cannot be resolved
|
|
131
|
+
// without knowing where that page sits, and Open Graph asks for an absolute URL anyway.
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async function isFile(path) {
|
|
136
|
+
try {
|
|
137
|
+
return (await stat(path)).isFile();
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Every share card a built page advertises is a file that exists.
|
|
145
|
+
*
|
|
146
|
+
* Three repos shipped a page whose `og:image` named a card nothing had ever rendered. All three
|
|
147
|
+
* were `404.html`, all three for the same reason: a card generator walks the page registry, and
|
|
148
|
+
* a not-found shell is not in the registry, so a head built from the same template inherits a
|
|
149
|
+
* card address with no card behind it. Every share of a dead link unfurled broken, in one case
|
|
150
|
+
* in three languages, and **nothing in a browser shows it** — the page looks perfect, and the
|
|
151
|
+
* only reader who finds out is whoever the link was sent to.
|
|
152
|
+
*
|
|
153
|
+
* `bakeHead` cannot catch it: it is handed an `image` and writes it, and a shell carrying the
|
|
154
|
+
* brand card is correct, so it has no way to tell a right image from a wrong one (invariant 12).
|
|
155
|
+
* The build can, because by then the cards are either on disk or they are not. Run it last,
|
|
156
|
+
* after the pages and the cards are written:
|
|
157
|
+
*
|
|
158
|
+
* ```ts
|
|
159
|
+
* await assertOgImages("dist", "https://example.com");
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* Cards on another host are somebody else's to serve and are skipped — but a build whose pages
|
|
163
|
+
* all advertise cards and whose origin matches NONE of them has been given the wrong origin,
|
|
164
|
+
* and a check that silently passes is worse than no check, so that is an error too.
|
|
165
|
+
*/
|
|
166
|
+
export async function assertOgImages(distDir, origin) {
|
|
167
|
+
const root = siteOrigin(origin);
|
|
168
|
+
const entries = await readdir(distDir, { recursive: true, withFileTypes: true });
|
|
169
|
+
const pages = entries
|
|
170
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith(".html"))
|
|
171
|
+
.map((entry) => join(entry.parentPath, entry.name));
|
|
172
|
+
const advertised = [];
|
|
173
|
+
for (const page of pages)
|
|
174
|
+
advertised.push(...advertisedCards(page, await readFile(page, "utf8")));
|
|
175
|
+
const mine = advertised
|
|
176
|
+
.map((card) => ({ ...card, file: distPathOf(card.url, root) }))
|
|
177
|
+
.filter((card) => card.file !== null);
|
|
178
|
+
if (advertised.length > 0 && mine.length === 0)
|
|
179
|
+
throw new Error(`og: ${String(advertised.length)} page(s) advertise a share card and none is under ` +
|
|
180
|
+
`${root}, so nothing was checked. Pass the origin this build renders for.`);
|
|
181
|
+
const missing = [];
|
|
182
|
+
for (const card of mine) {
|
|
183
|
+
if (!(await isFile(join(distDir, card.file))))
|
|
184
|
+
missing.push(card);
|
|
185
|
+
}
|
|
186
|
+
if (missing.length > 0)
|
|
187
|
+
throw new Error(`og: ${String(missing.length)} page(s) advertise a share card that is not in ${distDir} —\n\n` +
|
|
188
|
+
missing.map((card) => ` · ${card.page} → ${card.url}`).join("\n") +
|
|
189
|
+
"\n\n Either render the card, or leave `image` out of the head for that page.");
|
|
190
|
+
return mine.length;
|
|
191
|
+
}
|
|
104
192
|
//# sourceMappingURL=node.js.map
|
package/dist/node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":";;;;;;;;AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
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|
|
2
2
|
"name": "@gusnips/vite",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.6",
|
|
4
4
|
"description": "The build-time half of a prerendered Vite SPA: head baking, sitemap, robots, the prerender driver, and a shared vite preset.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"release:major": "bun pm version major && bun publish --access public"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@gusnips/react": "0.4.
|
|
62
|
+
"@gusnips/react": "0.4.6"
|
|
63
63
|
},
|
|
64
64
|
"peerDependencies": {
|
|
65
65
|
"@tailwindcss/vite": "^4",
|
package/src/index.ts
CHANGED
package/src/node.test.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
|
|
1
|
+
import { mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
4
|
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
-
import { loadTemplate, writeOgCards } from "./node.ts";
|
|
5
|
+
import { assertOgImages, loadTemplate, writeOgCards } from "./node.ts";
|
|
6
6
|
|
|
7
7
|
const dirs: string[] = [];
|
|
8
8
|
|
|
@@ -100,3 +100,77 @@ describe("writeOgCards", () => {
|
|
|
100
100
|
await expect(run).rejects.toThrow(/2 card\(s\)/);
|
|
101
101
|
});
|
|
102
102
|
});
|
|
103
|
+
|
|
104
|
+
describe("assertOgImages", () => {
|
|
105
|
+
const page = (image: string) =>
|
|
106
|
+
`<html><head><meta property="og:image" content="${image}" />` +
|
|
107
|
+
`<meta property="twitter:image" content="${image}" /></head></html>`;
|
|
108
|
+
|
|
109
|
+
async function distWith(files: Record<string, string>): Promise<string> {
|
|
110
|
+
const dir = await mkdtemp(join(tmpdir(), "frontkit-og-"));
|
|
111
|
+
dirs.push(dir);
|
|
112
|
+
for (const [file, contents] of Object.entries(files)) {
|
|
113
|
+
await mkdir(join(dir, dirname(file)), { recursive: true });
|
|
114
|
+
await writeFile(join(dir, file), contents, "utf8");
|
|
115
|
+
}
|
|
116
|
+
return dir;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
it("passes when every advertised card is on disk", async () => {
|
|
120
|
+
const dist = await distWith({
|
|
121
|
+
"index.html": page("https://example.com/og/home.png"),
|
|
122
|
+
"og/home.png": "png",
|
|
123
|
+
});
|
|
124
|
+
await expect(assertOgImages(dist, "https://example.com")).resolves.toBe(1);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The bug itself, three repos over: the card generator walks the page registry, the 404 shell
|
|
129
|
+
* is not in the registry, and the head it inherits advertises a card that has never been
|
|
130
|
+
* rendered. Nothing in a browser shows it — only the person the link was sent to.
|
|
131
|
+
*/
|
|
132
|
+
it("names the page whose card was never rendered", async () => {
|
|
133
|
+
const dist = await distWith({
|
|
134
|
+
"index.html": page("https://example.com/og/home.png"),
|
|
135
|
+
"404.html": page("https://example.com/og/__not-found__.png"),
|
|
136
|
+
"og/home.png": "png",
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
await expect(assertOgImages(dist, "https://example.com")).rejects.toThrow(
|
|
140
|
+
/404\.html → https:\/\/example\.com\/og\/__not-found__\.png/,
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("finds a page nested under a directory, and reads a root-relative card", async () => {
|
|
145
|
+
const dist = await distWith({
|
|
146
|
+
"precos/index.html": page("/og/precos.png"),
|
|
147
|
+
});
|
|
148
|
+
await expect(assertOgImages(dist, "https://example.com")).rejects.toThrow(/og\/precos\.png/);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("skips a card served by somebody else", async () => {
|
|
152
|
+
const dist = await distWith({
|
|
153
|
+
"index.html": page("https://cdn.example.net/card.png"),
|
|
154
|
+
"about.html": page("https://example.com/og/about.png"),
|
|
155
|
+
"og/about.png": "png",
|
|
156
|
+
});
|
|
157
|
+
await expect(assertOgImages(dist, "https://example.com")).resolves.toBe(1);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// A check that silently passes is worse than no check: with the origin wrong, every card on
|
|
161
|
+
// the page looks like somebody else's and nothing gets looked at.
|
|
162
|
+
it("refuses an origin that matches none of the cards it found", async () => {
|
|
163
|
+
const dist = await distWith({ "index.html": page("https://example.com/og/home.png") });
|
|
164
|
+
await expect(assertOgImages(dist, "https://wrong.example")).rejects.toThrow(
|
|
165
|
+
/none is under https:\/\/wrong\.example/,
|
|
166
|
+
);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("ignores a cache-busting query when looking for the file", async () => {
|
|
170
|
+
const dist = await distWith({
|
|
171
|
+
"index.html": page("https://example.com/og/home.png?v=2"),
|
|
172
|
+
"og/home.png": "png",
|
|
173
|
+
});
|
|
174
|
+
await expect(assertOgImages(dist, "https://example.com")).resolves.toBe(1);
|
|
175
|
+
});
|
|
176
|
+
});
|
package/src/node.ts
CHANGED
|
@@ -11,13 +11,14 @@
|
|
|
11
11
|
* policy the product owns. What is identical in every one of them is these four functions and
|
|
12
12
|
* the gate in `assertRendered`, so those ship and the ~40-line loop stays in the app.
|
|
13
13
|
*/
|
|
14
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
14
|
+
import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
15
15
|
import { dirname, join } from "node:path";
|
|
16
16
|
import { pathToFileURL } from "node:url";
|
|
17
17
|
import { EMPTY_ROOT } from "./head.ts";
|
|
18
18
|
import type { OgOverflow } from "./og.ts";
|
|
19
19
|
import { describeOverflow } from "./og.ts";
|
|
20
20
|
import type { PageRenderer } from "./render.ts";
|
|
21
|
+
import { siteOrigin } from "./sitemap.ts";
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* The built `index.html`, and a refusal to bake one twice.
|
|
@@ -138,3 +139,107 @@ export async function writeOgCards<Page>({
|
|
|
138
139
|
for (const entry of laid) await writeFile(join(outDir, entry.file), entry.png);
|
|
139
140
|
return laid.map((entry) => entry.file);
|
|
140
141
|
}
|
|
142
|
+
|
|
143
|
+
/** Where a page says its share card is, and the page that says it. */
|
|
144
|
+
interface AdvertisedCard {
|
|
145
|
+
page: string;
|
|
146
|
+
url: string;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const CARD_META =
|
|
150
|
+
/<meta[^>]+(?:property|name)="(?:og:image|twitter:image)"[^>]+content="([^"]*)"[^>]*>/gi;
|
|
151
|
+
|
|
152
|
+
function advertisedCards(page: string, html: string): AdvertisedCard[] {
|
|
153
|
+
const seen = new Set<string>();
|
|
154
|
+
const cards: AdvertisedCard[] = [];
|
|
155
|
+
for (const match of html.matchAll(CARD_META)) {
|
|
156
|
+
const url = match[1];
|
|
157
|
+
if (url && !seen.has(url)) {
|
|
158
|
+
seen.add(url);
|
|
159
|
+
cards.push({ page, url });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return cards;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The path a card URL points at inside `dist`, or null when the card is not this build's to
|
|
167
|
+
* serve. A `?v=2` or a `#` is addressing, not a file name, so both come off.
|
|
168
|
+
*/
|
|
169
|
+
function distPathOf(url: string, origin: string): string | null {
|
|
170
|
+
if (url.startsWith("/")) return decodeURIComponent(url.split(/[?#]/)[0] ?? "");
|
|
171
|
+
try {
|
|
172
|
+
const parsed = new URL(url);
|
|
173
|
+
return parsed.origin === origin ? decodeURIComponent(parsed.pathname) : null;
|
|
174
|
+
} catch {
|
|
175
|
+
// Not an address at all. A card URL relative to the page's own folder cannot be resolved
|
|
176
|
+
// without knowing where that page sits, and Open Graph asks for an absolute URL anyway.
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function isFile(path: string): Promise<boolean> {
|
|
182
|
+
try {
|
|
183
|
+
return (await stat(path)).isFile();
|
|
184
|
+
} catch {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Every share card a built page advertises is a file that exists.
|
|
191
|
+
*
|
|
192
|
+
* Three repos shipped a page whose `og:image` named a card nothing had ever rendered. All three
|
|
193
|
+
* were `404.html`, all three for the same reason: a card generator walks the page registry, and
|
|
194
|
+
* a not-found shell is not in the registry, so a head built from the same template inherits a
|
|
195
|
+
* card address with no card behind it. Every share of a dead link unfurled broken, in one case
|
|
196
|
+
* in three languages, and **nothing in a browser shows it** — the page looks perfect, and the
|
|
197
|
+
* only reader who finds out is whoever the link was sent to.
|
|
198
|
+
*
|
|
199
|
+
* `bakeHead` cannot catch it: it is handed an `image` and writes it, and a shell carrying the
|
|
200
|
+
* brand card is correct, so it has no way to tell a right image from a wrong one (invariant 12).
|
|
201
|
+
* The build can, because by then the cards are either on disk or they are not. Run it last,
|
|
202
|
+
* after the pages and the cards are written:
|
|
203
|
+
*
|
|
204
|
+
* ```ts
|
|
205
|
+
* await assertOgImages("dist", "https://example.com");
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* Cards on another host are somebody else's to serve and are skipped — but a build whose pages
|
|
209
|
+
* all advertise cards and whose origin matches NONE of them has been given the wrong origin,
|
|
210
|
+
* and a check that silently passes is worse than no check, so that is an error too.
|
|
211
|
+
*/
|
|
212
|
+
export async function assertOgImages(distDir: string, origin: string): Promise<number> {
|
|
213
|
+
const root = siteOrigin(origin);
|
|
214
|
+
const entries = await readdir(distDir, { recursive: true, withFileTypes: true });
|
|
215
|
+
const pages = entries
|
|
216
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith(".html"))
|
|
217
|
+
.map((entry) => join(entry.parentPath, entry.name));
|
|
218
|
+
|
|
219
|
+
const advertised: AdvertisedCard[] = [];
|
|
220
|
+
for (const page of pages) advertised.push(...advertisedCards(page, await readFile(page, "utf8")));
|
|
221
|
+
|
|
222
|
+
const mine = advertised
|
|
223
|
+
.map((card) => ({ ...card, file: distPathOf(card.url, root) }))
|
|
224
|
+
.filter((card): card is AdvertisedCard & { file: string } => card.file !== null);
|
|
225
|
+
|
|
226
|
+
if (advertised.length > 0 && mine.length === 0)
|
|
227
|
+
throw new Error(
|
|
228
|
+
`og: ${String(advertised.length)} page(s) advertise a share card and none is under ` +
|
|
229
|
+
`${root}, so nothing was checked. Pass the origin this build renders for.`,
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const missing: (AdvertisedCard & { file: string })[] = [];
|
|
233
|
+
for (const card of mine) {
|
|
234
|
+
if (!(await isFile(join(distDir, card.file)))) missing.push(card);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (missing.length > 0)
|
|
238
|
+
throw new Error(
|
|
239
|
+
`og: ${String(missing.length)} page(s) advertise a share card that is not in ${distDir} —\n\n` +
|
|
240
|
+
missing.map((card) => ` · ${card.page} → ${card.url}`).join("\n") +
|
|
241
|
+
"\n\n Either render the card, or leave `image` out of the head for that page.",
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
return mine.length;
|
|
245
|
+
}
|