@hubpav/hwio-web-worker 0.6.6 → 0.7.1
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/dist/headers.d.ts +6 -0
- package/dist/headers.js +2 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +12 -8
- package/package.json +1 -1
package/dist/headers.d.ts
CHANGED
|
@@ -28,6 +28,12 @@ export interface HWioHeadersOptions {
|
|
|
28
28
|
cache?: Record<string, string>;
|
|
29
29
|
/** Extra headers on every response (e.g. X-Robots-Tag: noindex). */
|
|
30
30
|
extra?: Record<string, string>;
|
|
31
|
+
/**
|
|
32
|
+
* Add a host-scoped `_headers` rule that marks preview deployments (`*.workers.dev`) noindex.
|
|
33
|
+
* Needed on sites whose Worker does not run for HTML (`run_worker_first: ["/csp-report"]`),
|
|
34
|
+
* where the handler's own preview header never applies.
|
|
35
|
+
*/
|
|
36
|
+
previewNoindex?: boolean;
|
|
31
37
|
}
|
|
32
38
|
export declare function hwioBuildCsp(o: HWioHeadersOptions): string;
|
|
33
39
|
export declare function hwioSecurityHeaders(o: HWioHeadersOptions): Record<string, string>;
|
package/dist/headers.js
CHANGED
|
@@ -70,5 +70,7 @@ export function hwioRenderHeadersFile(o) {
|
|
|
70
70
|
const security = Object.entries(hwioSecurityHeaders(o)).map(([k, v]) => ` ${k}: ${v}`);
|
|
71
71
|
const hints = (o.earlyHints ?? []).length ? hwioEarlyHintLinks(o.earlyHints).map((l) => ` Link: ${l}`) : [];
|
|
72
72
|
blocks.push(`/*\n${[...security, ...hints].join('\n')}`);
|
|
73
|
+
if (o.previewNoindex)
|
|
74
|
+
blocks.push('https://:preview.:account.workers.dev/*\n X-Robots-Tag: noindex, nofollow');
|
|
73
75
|
return `# Generated by @hubpav/hwio-web-worker (hwioRenderHeadersFile). Edit the site's hwioUi() options, not this file.\n${blocks.join('\n\n')}\n`;
|
|
74
76
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ export interface HWioWorkerOptions {
|
|
|
8
8
|
earlyHints?: string[];
|
|
9
9
|
/** Name of the static assets binding. */
|
|
10
10
|
assetsBinding?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Hostname suffixes treated as preview deployments (Workers Builds non-production branches,
|
|
13
|
+
* `wrangler versions upload --preview-alias`): the canonical redirect is skipped there and
|
|
14
|
+
* every response carries `X-Robots-Tag: noindex, nofollow`. Default: ['.workers.dev'].
|
|
15
|
+
*/
|
|
16
|
+
previewHosts?: string[];
|
|
11
17
|
}
|
|
12
18
|
/**
|
|
13
19
|
* Fetch handler for an Astro static site on Cloudflare Workers with static assets.
|
package/dist/index.js
CHANGED
|
@@ -6,10 +6,11 @@ export * from './headers.js';
|
|
|
6
6
|
* `run_worker_first: true` also get the canonical redirect and Early Hints.
|
|
7
7
|
*/
|
|
8
8
|
export function hwioWorker(options = {}) {
|
|
9
|
-
const { canonicalHost = null, cspReport = true, earlyHints = [], assetsBinding = 'ASSETS' } = options;
|
|
9
|
+
const { canonicalHost = null, cspReport = true, earlyHints = [], assetsBinding = 'ASSETS', previewHosts = ['.workers.dev'] } = options;
|
|
10
10
|
return {
|
|
11
11
|
async fetch(request, env) {
|
|
12
12
|
const url = new URL(request.url);
|
|
13
|
+
const preview = previewHosts.some((suffix) => url.hostname.endsWith(suffix));
|
|
13
14
|
if (cspReport && url.pathname === '/csp-report') {
|
|
14
15
|
if (request.method === 'POST') {
|
|
15
16
|
try {
|
|
@@ -22,7 +23,7 @@ export function hwioWorker(options = {}) {
|
|
|
22
23
|
}
|
|
23
24
|
return new Response(null, { status: 204 });
|
|
24
25
|
}
|
|
25
|
-
if (canonicalHost && (url.protocol !== 'https:' || url.hostname !== canonicalHost)) {
|
|
26
|
+
if (canonicalHost && !preview && (url.protocol !== 'https:' || url.hostname !== canonicalHost)) {
|
|
26
27
|
url.protocol = 'https:';
|
|
27
28
|
url.hostname = canonicalHost;
|
|
28
29
|
return Response.redirect(url.toString(), 301);
|
|
@@ -31,12 +32,15 @@ export function hwioWorker(options = {}) {
|
|
|
31
32
|
if (!assets)
|
|
32
33
|
return new Response('assets binding missing', { status: 500 });
|
|
33
34
|
const res = await assets.fetch(request);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
const html = res.status === 200 && (res.headers.get('content-type') ?? '').includes('text/html');
|
|
36
|
+
if (!preview && !(earlyHints.length && html))
|
|
37
|
+
return res;
|
|
38
|
+
const out = new Response(res.body, res);
|
|
39
|
+
if (earlyHints.length && html)
|
|
40
|
+
hwioEarlyHintLinks(earlyHints).forEach((l) => out.headers.append('Link', l));
|
|
41
|
+
if (preview)
|
|
42
|
+
out.headers.set('X-Robots-Tag', 'noindex, nofollow');
|
|
43
|
+
return out;
|
|
40
44
|
},
|
|
41
45
|
};
|
|
42
46
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubpav/hwio-web-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Shared Cloudflare Worker handler and security-header builder for the HARDWARIO static sites: canonical-host redirect, CSP report collector, Early Hints, _headers generation.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|