@cloudhal/astro-renderer 0.3.8 → 0.3.10
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.
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workers Cache keys by path, not hostname, and answers before the Worker runs (#62). Every host
|
|
3
|
+
* routed to the site Worker — a custom domain, its workers.dev name, a redirect host (#49), the
|
|
4
|
+
* other sites of a multi-site Worker (RFC-0034) — would otherwise share one entry per path. `Vary:
|
|
5
|
+
* Host` makes it keep one variant per host.
|
|
6
|
+
*
|
|
7
|
+
* Worker-safe: no imports.
|
|
8
|
+
*/
|
|
9
|
+
export declare function varyByHost(response: Response): Response;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workers Cache keys by path, not hostname, and answers before the Worker runs (#62). Every host
|
|
3
|
+
* routed to the site Worker — a custom domain, its workers.dev name, a redirect host (#49), the
|
|
4
|
+
* other sites of a multi-site Worker (RFC-0034) — would otherwise share one entry per path. `Vary:
|
|
5
|
+
* Host` makes it keep one variant per host.
|
|
6
|
+
*
|
|
7
|
+
* Worker-safe: no imports.
|
|
8
|
+
*/
|
|
9
|
+
export function varyByHost(response ) {
|
|
10
|
+
const vary = response.headers.get("vary") ?? "";
|
|
11
|
+
if (vary.trim() === "*" || /(^|,)\s*host\s*(,|$)/i.test(vary)) return response;
|
|
12
|
+
// A redirect's or a WebSocket's headers can be immutable; copy them into a fresh response.
|
|
13
|
+
const headers = new Headers(response.headers);
|
|
14
|
+
headers.set("vary", vary.trim() ? `${vary}, Host` : "Host");
|
|
15
|
+
return new Response(response.body, {
|
|
16
|
+
status: response.status,
|
|
17
|
+
statusText: response.statusText,
|
|
18
|
+
headers,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
* `CLOUDHAL_COMMENTS_KEY` (or when the build has no comments module).
|
|
34
34
|
*/
|
|
35
35
|
import { type CommentsRateLimit } from "./comments.js";
|
|
36
|
-
|
|
36
|
+
import { type RedirectEnv } from "./host-redirect.js";
|
|
37
|
+
interface Env extends RedirectEnv {
|
|
37
38
|
CLOUDHAL_SITE_ID?: string;
|
|
38
39
|
/**
|
|
39
40
|
* RFC-0034 §4: the sites a multi-site Worker serves (comma-separated); the hostname picks one
|
|
@@ -54,14 +54,16 @@ import {
|
|
|
54
54
|
SiteAnalytics,
|
|
55
55
|
} from "./analytics.js";
|
|
56
56
|
import { isCloudhalCacheTag } from "./cache-tags.js";
|
|
57
|
+
import { varyByHost } from "./cache-vary.js";
|
|
57
58
|
import { isCommentsPath, SiteComments } from "./comments.js";
|
|
58
59
|
import { SITE_CSP } from "./csp.js";
|
|
59
60
|
import { EMBED_HEADER, withEmbedFrames } from "./embed-client.js";
|
|
61
|
+
import { hostRedirect, } from "./host-redirect.js";
|
|
60
62
|
import { PREVIEW_PARAM } from "./resolver.js";
|
|
61
63
|
import { SearchLimiter, searchCost } from "./search-limit.js";
|
|
62
64
|
import { SiteDispatch, unknownHost, withNoindex } from "./site-dispatch.js";
|
|
63
65
|
|
|
64
|
-
|
|
66
|
+
|
|
65
67
|
|
|
66
68
|
|
|
67
69
|
|
|
@@ -528,19 +530,25 @@ async function serveSite(
|
|
|
528
530
|
|
|
529
531
|
export function createSiteWorker(options = {}) {
|
|
530
532
|
const searchLimiter = new SearchLimiter();
|
|
533
|
+
async function route(request , env , ctx ) {
|
|
534
|
+
const url = new URL(request.url);
|
|
535
|
+
if (url.pathname === "/__cloudhal/purge") return purge(request, env, options.siteId);
|
|
536
|
+
if (url.pathname === "/__cloudhal/features") return features(request, env);
|
|
537
|
+
const moved = hostRedirect(request, env);
|
|
538
|
+
if (moved) return moved;
|
|
539
|
+
// RFC-0034 §4: the hostname picks the site on a multi-site Worker; everything the request
|
|
540
|
+
// runs (Astro's render included) then sees that site.
|
|
541
|
+
const choice = await siteDispatch(env, options.siteId).choose(request);
|
|
542
|
+
if (choice.kind === "unknown") return unknownHost();
|
|
543
|
+
const siteId = choice.siteId;
|
|
544
|
+
const run = () => serveSite({ request, env, ctx }, siteId, searchLimiter);
|
|
545
|
+
const response = siteId ? await withRequestSite(siteId, run) : await run();
|
|
546
|
+
return choice.noindex ? withNoindex(response) : response;
|
|
547
|
+
}
|
|
531
548
|
return {
|
|
532
549
|
async fetch(request , env , ctx ) {
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
if (url.pathname === "/__cloudhal/features") return features(request, env);
|
|
536
|
-
// RFC-0034 §4: the hostname picks the site on a multi-site Worker; everything the request
|
|
537
|
-
// runs (Astro's render included) then sees that site.
|
|
538
|
-
const choice = await siteDispatch(env, options.siteId).choose(request);
|
|
539
|
-
if (choice.kind === "unknown") return unknownHost();
|
|
540
|
-
const siteId = choice.siteId;
|
|
541
|
-
const run = () => serveSite({ request, env, ctx }, siteId, searchLimiter);
|
|
542
|
-
const response = siteId ? await withRequestSite(siteId, run) : await run();
|
|
543
|
-
return choice.noindex ? withNoindex(response) : response;
|
|
550
|
+
// #62: the edge cache is shared by every host this Worker answers on, unless told to vary.
|
|
551
|
+
return varyByHost(await route(request, env, ctx));
|
|
544
552
|
},
|
|
545
553
|
};
|
|
546
554
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hosts that only redirect (#49): `www.example.com` → `https://example.com`, 301, path and query
|
|
3
|
+
* kept. `cloudhal deploy` routes them to the site Worker as Custom Domains (so they get a DNS
|
|
4
|
+
* record and certificate) and names them in `CLOUDHAL_REDIRECT_HOSTS`, the target in
|
|
5
|
+
* `CLOUDHAL_REDIRECT_TO`. Runs first, before any site is picked.
|
|
6
|
+
*
|
|
7
|
+
* Worker-safe: no imports.
|
|
8
|
+
*/
|
|
9
|
+
export interface RedirectEnv {
|
|
10
|
+
/** Comma- or space-separated hostnames that answer with a redirect. */
|
|
11
|
+
readonly CLOUDHAL_REDIRECT_HOSTS?: string;
|
|
12
|
+
/** The hostname they redirect to (the site's custom domain). */
|
|
13
|
+
readonly CLOUDHAL_REDIRECT_TO?: string;
|
|
14
|
+
}
|
|
15
|
+
/** The 301 for a request on a redirect-only host, or undefined. */
|
|
16
|
+
export declare function hostRedirect(request: Request, env: RedirectEnv): Response | undefined;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hosts that only redirect (#49): `www.example.com` → `https://example.com`, 301, path and query
|
|
3
|
+
* kept. `cloudhal deploy` routes them to the site Worker as Custom Domains (so they get a DNS
|
|
4
|
+
* record and certificate) and names them in `CLOUDHAL_REDIRECT_HOSTS`, the target in
|
|
5
|
+
* `CLOUDHAL_REDIRECT_TO`. Runs first, before any site is picked.
|
|
6
|
+
*
|
|
7
|
+
* Worker-safe: no imports.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
/** The 301 for a request on a redirect-only host, or undefined. */
|
|
17
|
+
export function hostRedirect(request , env ) {
|
|
18
|
+
const to = env.CLOUDHAL_REDIRECT_TO?.trim().toLowerCase();
|
|
19
|
+
if (!to || !env.CLOUDHAL_REDIRECT_HOSTS) return undefined;
|
|
20
|
+
const url = new URL(request.url);
|
|
21
|
+
const host = url.hostname.toLowerCase().replace(/\.$/, "");
|
|
22
|
+
if (host === to) return undefined;
|
|
23
|
+
const hosts = env.CLOUDHAL_REDIRECT_HOSTS.toLowerCase().split(/[\s,]+/);
|
|
24
|
+
if (!hosts.includes(host)) return undefined;
|
|
25
|
+
return new Response(null, {
|
|
26
|
+
status: 301,
|
|
27
|
+
headers: {
|
|
28
|
+
location: `https://${to}${url.pathname}${url.search}`,
|
|
29
|
+
"cache-control": "public, max-age=86400",
|
|
30
|
+
// Never in the edge cache (#62): it is shared with the hosts this one redirects to.
|
|
31
|
+
"cloudflare-cdn-cache-control": "no-store",
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudhal/astro-renderer",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.10",
|
|
4
4
|
"description": "Render a Cloudhal Site Graph with Astro: data sources (live/snapshot), section components and the cloudhal() integration",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://cloudhal.dev",
|
|
@@ -179,14 +179,14 @@
|
|
|
179
179
|
"README.md"
|
|
180
180
|
],
|
|
181
181
|
"dependencies": {
|
|
182
|
-
"@cloudhal/core-contracts": "0.3.
|
|
183
|
-
"@cloudhal/core-ts": "0.3.
|
|
184
|
-
"@cloudhal/storage": "0.3.
|
|
182
|
+
"@cloudhal/core-contracts": "0.3.10",
|
|
183
|
+
"@cloudhal/core-ts": "0.3.10",
|
|
184
|
+
"@cloudhal/storage": "0.3.10",
|
|
185
185
|
"marked": "^18.1.0",
|
|
186
186
|
"parse5": "8.0.1",
|
|
187
|
-
"@cloudhal/analytics": "0.3.
|
|
187
|
+
"@cloudhal/analytics": "0.3.10",
|
|
188
188
|
"vite": "^8.3.3",
|
|
189
|
-
"@cloudhal/comments": "0.3.
|
|
189
|
+
"@cloudhal/comments": "0.3.10"
|
|
190
190
|
},
|
|
191
191
|
"peerDependencies": {
|
|
192
192
|
"astro": "^7.3.0",
|