@lownoise-studio/rendershield 0.3.0 → 1.0.0
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/CHANGELOG.md +58 -0
- package/CONTRIBUTING.md +41 -0
- package/README.md +209 -138
- package/SECURITY.md +25 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +43 -21
- package/dist/cli.js.map +1 -1
- package/dist/commands/build.d.ts +2 -0
- package/dist/commands/build.d.ts.map +1 -0
- package/dist/commands/build.js +10 -9
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/init.d.ts +2 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/init.js +7 -7
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/verify.d.ts +19 -0
- package/dist/commands/verify.d.ts.map +1 -0
- package/dist/commands/verify.js +60 -64
- package/dist/commands/verify.js.map +1 -1
- package/dist/core/generateRobots.d.ts +3 -0
- package/dist/core/generateRobots.d.ts.map +1 -0
- package/dist/core/generateSitemap.d.ts +3 -0
- package/dist/core/generateSitemap.d.ts.map +1 -0
- package/dist/core/generateWorker.d.ts +3 -0
- package/dist/core/generateWorker.d.ts.map +1 -0
- package/dist/core/generateWorker.js +85 -75
- package/dist/core/generateWorker.js.map +1 -1
- package/dist/core/loadConfig.d.ts +3 -0
- package/dist/core/loadConfig.d.ts.map +1 -0
- package/dist/core/loadConfig.js +99 -40
- package/dist/core/loadConfig.js.map +1 -1
- package/dist/core/loadMarkdown.d.ts +3 -0
- package/dist/core/loadMarkdown.d.ts.map +1 -0
- package/dist/core/loadMarkdown.js +4 -3
- package/dist/core/loadMarkdown.js.map +1 -1
- package/dist/core/renderHtml.d.ts +3 -0
- package/dist/core/renderHtml.d.ts.map +1 -0
- package/dist/core/renderHtml.js +30 -11
- package/dist/core/renderHtml.js.map +1 -1
- package/dist/core/validateOutput.d.ts +28 -0
- package/dist/core/validateOutput.d.ts.map +1 -0
- package/dist/core/validateOutput.js +9 -3
- package/dist/core/validateOutput.js.map +1 -1
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +24 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/docs/deploy-cloudflare.md +40 -14
- package/package.json +30 -4
- package/src/cli.ts +86 -60
- package/src/commands/build.ts +199 -185
- package/src/commands/init.ts +7 -7
- package/src/commands/verify.ts +266 -233
- package/src/core/generateWorker.ts +97 -87
- package/src/core/loadConfig.ts +261 -142
- package/src/core/loadMarkdown.ts +11 -5
- package/src/core/renderHtml.ts +41 -13
- package/src/core/validateOutput.ts +335 -325
- package/src/errors.ts +48 -0
- package/src/index.ts +40 -0
- package/src/types.ts +4 -1
package/src/commands/verify.ts
CHANGED
|
@@ -1,233 +1,266 @@
|
|
|
1
|
-
import fs from "fs-extra";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { loadConfig } from "../core/loadConfig.js";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
"
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
`
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
);
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
//
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
`)
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { loadConfig } from "../core/loadConfig.js";
|
|
4
|
+
import {
|
|
5
|
+
checkPrerenderContract,
|
|
6
|
+
type ContractCheckResult,
|
|
7
|
+
} from "../core/validateOutput.js";
|
|
8
|
+
import { renderShieldError } from "../errors.js";
|
|
9
|
+
|
|
10
|
+
function joinUrl(base: string, routePath: string): string {
|
|
11
|
+
const b = base.endsWith("/") ? base.slice(0, -1) : base;
|
|
12
|
+
const p = routePath.startsWith("/") ? routePath : `/${routePath}`;
|
|
13
|
+
return b + p;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function findFirstIndexHtml(outDirAbs: string): Promise<string | null> {
|
|
17
|
+
const stack: string[] = [outDirAbs];
|
|
18
|
+
|
|
19
|
+
while (stack.length > 0) {
|
|
20
|
+
const current = stack.pop() as string;
|
|
21
|
+
|
|
22
|
+
let entries: fs.Dirent[];
|
|
23
|
+
try {
|
|
24
|
+
entries = await fs.readdir(current, { withFileTypes: true });
|
|
25
|
+
} catch {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
30
|
+
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
const full = path.join(current, entry.name);
|
|
33
|
+
|
|
34
|
+
if (entry.isDirectory()) {
|
|
35
|
+
stack.push(full);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (entry.isFile() && entry.name.toLowerCase() === "index.html") {
|
|
40
|
+
// Ignore index.html at the output root; prefer a routed page like
|
|
41
|
+
// <section>/<slug>/index.html (or deeper).
|
|
42
|
+
const rel = path.relative(outDirAbs, full);
|
|
43
|
+
const parts = rel.split(path.sep).filter(Boolean);
|
|
44
|
+
if (parts.length >= 2) return full;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function indexHtmlPathToRoute(outDirAbs: string, indexPathAbs: string): string {
|
|
53
|
+
const rel = path.relative(outDirAbs, indexPathAbs);
|
|
54
|
+
// rel: <section>/<slug>/index.html
|
|
55
|
+
const noFile = rel.replace(/index\.html$/i, "");
|
|
56
|
+
const normalized = noFile.split(path.sep).join("/").replace(/\/+$/, "");
|
|
57
|
+
return "/" + normalized.replace(/^\/+/, "");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const BOT_UA =
|
|
61
|
+
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
|
|
62
|
+
const HUMAN_UA =
|
|
63
|
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
|
|
64
|
+
|
|
65
|
+
/** Heuristic: likely SPA shell if body has almost no visible content and no article. */
|
|
66
|
+
function looksLikeSpaShell(html: string): { likely: boolean; reason?: string } {
|
|
67
|
+
const bodyMatch = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
|
|
68
|
+
const bodyHtml = bodyMatch ? bodyMatch[1] : html;
|
|
69
|
+
const noScript = bodyHtml
|
|
70
|
+
.replace(/<script[\s\S]*?<\/script>/gi, " ")
|
|
71
|
+
.replace(/<style[\s\S]*?<\/style>/gi, " ")
|
|
72
|
+
.replace(/<noscript[\s\S]*?<\/noscript>/gi, " ");
|
|
73
|
+
const text = noScript.replace(/<\/?[^>]+>/g, " ").replace(/\s+/g, " ").trim();
|
|
74
|
+
if (text.length < 150) {
|
|
75
|
+
return { likely: true, reason: `Body text very short (${text.length} chars); likely app shell.` };
|
|
76
|
+
}
|
|
77
|
+
if (!/<article\b/i.test(html)) {
|
|
78
|
+
return { likely: true, reason: "No <article> present; may be SPA shell." };
|
|
79
|
+
}
|
|
80
|
+
const rootOnly = /<body[^>]*>\s*<div[^>]*id=["'](?:root|app|__next)["'][^>]*>\s*<\/div>\s*<\/body>/i.test(
|
|
81
|
+
html.replace(/\s+/g, " ")
|
|
82
|
+
);
|
|
83
|
+
if (rootOnly) {
|
|
84
|
+
return { likely: true, reason: "Single root div (e.g. #root, #app) with no content." };
|
|
85
|
+
}
|
|
86
|
+
return { likely: false };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type VerifyProdOptions = { prodUrl: string };
|
|
90
|
+
|
|
91
|
+
export type VerifyLocalResult = {
|
|
92
|
+
mode: "local";
|
|
93
|
+
canonicalBase: string;
|
|
94
|
+
routePath: string;
|
|
95
|
+
outputFile: string;
|
|
96
|
+
url: string;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export type VerifyProdResult = {
|
|
100
|
+
mode: "prod";
|
|
101
|
+
url: string;
|
|
102
|
+
contract: ContractCheckResult;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export type VerifyResult = VerifyLocalResult | VerifyProdResult;
|
|
106
|
+
|
|
107
|
+
export async function cmdVerify(
|
|
108
|
+
cwd = process.cwd(),
|
|
109
|
+
options?: VerifyProdOptions
|
|
110
|
+
): Promise<VerifyResult> {
|
|
111
|
+
if (options?.prodUrl) {
|
|
112
|
+
return runVerifyProd(options.prodUrl);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const cfg = await loadConfig(cwd);
|
|
116
|
+
|
|
117
|
+
const outDirAbs = path.join(cwd, cfg.output.outDir);
|
|
118
|
+
const exists = await fs.pathExists(outDirAbs);
|
|
119
|
+
|
|
120
|
+
if (!exists) {
|
|
121
|
+
throw renderShieldError(
|
|
122
|
+
"VERIFY_FAILED",
|
|
123
|
+
`No prerender output directory found: ${cfg.output.outDir}/. Run: rendershield build`,
|
|
124
|
+
{ outDir: cfg.output.outDir }
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const firstIndex = await findFirstIndexHtml(outDirAbs);
|
|
129
|
+
|
|
130
|
+
if (!firstIndex) {
|
|
131
|
+
throw renderShieldError(
|
|
132
|
+
"VERIFY_FAILED",
|
|
133
|
+
`No prerendered pages found inside: ${cfg.output.outDir}/. Run: rendershield build`,
|
|
134
|
+
{ outDir: cfg.output.outDir }
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const routePath = indexHtmlPathToRoute(outDirAbs, firstIndex);
|
|
139
|
+
const url = joinUrl(cfg.site.canonicalBase, routePath);
|
|
140
|
+
const outputFile = path.relative(cwd, firstIndex);
|
|
141
|
+
|
|
142
|
+
console.log(`
|
|
143
|
+
RenderShield verify
|
|
144
|
+
|
|
145
|
+
Using:
|
|
146
|
+
canonicalBase: ${cfg.site.canonicalBase}
|
|
147
|
+
routePath: ${routePath}
|
|
148
|
+
output file: ${outputFile}
|
|
149
|
+
|
|
150
|
+
Smoke tests:
|
|
151
|
+
|
|
152
|
+
1) Human (usually SPA shell):
|
|
153
|
+
curl -s ${url} | grep -i "<title>"
|
|
154
|
+
|
|
155
|
+
2) Bot (should see prerendered, route-specific title):
|
|
156
|
+
curl -s -H "User-Agent: Googlebot" ${url} | grep -i "<title>"
|
|
157
|
+
|
|
158
|
+
3) Debug headers (Worker must be routed + proxy ON):
|
|
159
|
+
curl -I -H "User-Agent: GPTBot" ${url}
|
|
160
|
+
|
|
161
|
+
Expected: x-rendershield: bot-hit (proves Worker served prerender to bot).
|
|
162
|
+
If debugHeaders enabled: X-Bot-Detected, X-Prerender, X-Final-Path.
|
|
163
|
+
`);
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
mode: "local",
|
|
167
|
+
canonicalBase: cfg.site.canonicalBase,
|
|
168
|
+
routePath,
|
|
169
|
+
outputFile,
|
|
170
|
+
url,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function runVerifyProd(url: string): Promise<VerifyProdResult> {
|
|
175
|
+
const normalizedUrl = url.startsWith("http") ? url : `https://${url}`;
|
|
176
|
+
|
|
177
|
+
let botHtml: string;
|
|
178
|
+
let humanHtml: string;
|
|
179
|
+
let botStatus: number;
|
|
180
|
+
let humanStatus: number;
|
|
181
|
+
let xRenderShield: string | null;
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
const [botRes, humanRes] = await Promise.all([
|
|
185
|
+
fetch(normalizedUrl, {
|
|
186
|
+
headers: { "User-Agent": BOT_UA },
|
|
187
|
+
redirect: "follow",
|
|
188
|
+
}),
|
|
189
|
+
fetch(normalizedUrl, {
|
|
190
|
+
headers: { "User-Agent": HUMAN_UA },
|
|
191
|
+
redirect: "follow",
|
|
192
|
+
}),
|
|
193
|
+
]);
|
|
194
|
+
|
|
195
|
+
botStatus = botRes.status;
|
|
196
|
+
humanStatus = humanRes.status;
|
|
197
|
+
xRenderShield = botRes.headers.get("x-rendershield");
|
|
198
|
+
botHtml = await botRes.text();
|
|
199
|
+
humanHtml = await humanRes.text();
|
|
200
|
+
} catch (err: unknown) {
|
|
201
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
202
|
+
throw renderShieldError(
|
|
203
|
+
"VERIFY_FAILED",
|
|
204
|
+
`verify --prod: failed to fetch ${normalizedUrl}. ${msg}`,
|
|
205
|
+
{ url: normalizedUrl }
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Prove routing: Worker must set x-rendershield: bot-hit for bot requests. No inference.
|
|
210
|
+
const routingOk = xRenderShield === "bot-hit";
|
|
211
|
+
if (xRenderShield === "bot-fallback") {
|
|
212
|
+
throw renderShieldError(
|
|
213
|
+
"VERIFY_FAILED",
|
|
214
|
+
`verify --prod: bot request received x-rendershield: bot-fallback. ` +
|
|
215
|
+
`Prerender origin returned non-200; Worker fell back to SPA. Fix deployment or origin so bots get prerendered HTML.`,
|
|
216
|
+
{ url: normalizedUrl, xRenderShield }
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
if (!routingOk) {
|
|
220
|
+
const hint = xRenderShield == null
|
|
221
|
+
? " If no Worker is deployed, use verify without --prod to check local/build output."
|
|
222
|
+
: "";
|
|
223
|
+
throw renderShieldError(
|
|
224
|
+
"VERIFY_FAILED",
|
|
225
|
+
`verify --prod: expected x-rendershield: bot-hit (proving Worker routed bot to prerender). ` +
|
|
226
|
+
`Got: ${xRenderShield ?? "(missing)"}. Ensure the Worker is deployed and bound to this route.${hint}`,
|
|
227
|
+
{ url: normalizedUrl, xRenderShield }
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const contract = checkPrerenderContract(botHtml, {
|
|
232
|
+
routePath: normalizedUrl,
|
|
233
|
+
outFile: normalizedUrl,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const humanSpa = looksLikeSpaShell(humanHtml);
|
|
237
|
+
|
|
238
|
+
// Report
|
|
239
|
+
console.log(`
|
|
240
|
+
RenderShield verify --prod
|
|
241
|
+
URL: ${normalizedUrl}
|
|
242
|
+
|
|
243
|
+
Fetch:
|
|
244
|
+
Bot (Googlebot): ${botStatus} (${botHtml.length} bytes) x-rendershield: ${xRenderShield ?? "(none)"}
|
|
245
|
+
Human (Chrome): ${humanStatus} (${humanHtml.length} bytes)
|
|
246
|
+
|
|
247
|
+
Routing: x-rendershield: bot-hit (Worker served prerendered HTML to bot)
|
|
248
|
+
|
|
249
|
+
Bot contract (title, meta, canonical, OG, JSON-LD, article):
|
|
250
|
+
${contract.ok ? "PASS — all required fields present." : "FAIL — missing or invalid:"}
|
|
251
|
+
${contract.missing.length > 0 ? contract.missing.map((m) => ` - ${m}`).join("\n") : ""}
|
|
252
|
+
|
|
253
|
+
Human response:
|
|
254
|
+
${humanSpa.likely ? `Likely SPA shell: ${humanSpa.reason ?? "unknown"}` : "Has substantial content (not a minimal SPA shell)."}
|
|
255
|
+
`);
|
|
256
|
+
|
|
257
|
+
if (!contract.ok) {
|
|
258
|
+
throw renderShieldError(
|
|
259
|
+
"VERIFY_FAILED",
|
|
260
|
+
`Production URL did not satisfy bot contract. Missing: ${contract.missing.join("; ")}`,
|
|
261
|
+
{ url: normalizedUrl, missing: contract.missing }
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return { mode: "prod", url: normalizedUrl, contract };
|
|
266
|
+
}
|