@cloudhal/astro-renderer 0.3.7 → 0.3.9

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.
@@ -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
- interface Env {
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
@@ -57,11 +57,12 @@ import { isCloudhalCacheTag } from "./cache-tags.js";
57
57
  import { isCommentsPath, SiteComments } from "./comments.js";
58
58
  import { SITE_CSP } from "./csp.js";
59
59
  import { EMBED_HEADER, withEmbedFrames } from "./embed-client.js";
60
+ import { hostRedirect, } from "./host-redirect.js";
60
61
  import { PREVIEW_PARAM } from "./resolver.js";
61
62
  import { SearchLimiter, searchCost } from "./search-limit.js";
62
63
  import { SiteDispatch, unknownHost, withNoindex } from "./site-dispatch.js";
63
64
 
64
-
65
+
65
66
 
66
67
 
67
68
   
@@ -533,6 +534,8 @@ export function createSiteWorker(options = {}) {
533
534
  const url = new URL(request.url);
534
535
  if (url.pathname === "/__cloudhal/purge") return purge(request, env, options.siteId);
535
536
  if (url.pathname === "/__cloudhal/features") return features(request, env);
537
+ const moved = hostRedirect(request, env);
538
+ if (moved) return moved;
536
539
  // RFC-0034 §4: the hostname picks the site on a multi-site Worker; everything the request
537
540
  // runs (Astro's render included) then sees that site.
538
541
  const choice = await siteDispatch(env, options.siteId).choose(request);
@@ -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,32 @@
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
+ },
31
+ });
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudhal/astro-renderer",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
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.7",
183
- "@cloudhal/core-ts": "0.3.7",
184
- "@cloudhal/storage": "0.3.7",
182
+ "@cloudhal/core-contracts": "0.3.9",
183
+ "@cloudhal/core-ts": "0.3.9",
184
+ "@cloudhal/storage": "0.3.9",
185
185
  "marked": "^18.1.0",
186
186
  "parse5": "8.0.1",
187
- "@cloudhal/analytics": "0.3.7",
187
+ "@cloudhal/analytics": "0.3.9",
188
188
  "vite": "^8.3.3",
189
- "@cloudhal/comments": "0.3.7"
189
+ "@cloudhal/comments": "0.3.9"
190
190
  },
191
191
  "peerDependencies": {
192
192
  "astro": "^7.3.0",