@realadvisor-sites/kit-astro 0.5.1 → 0.5.3

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/lib/ra.ts CHANGED
@@ -6,7 +6,9 @@
6
6
  * - Production and published previews: SITES_API_URL (var, the rest-api
7
7
  * origin) + SITES_API_KEY (Worker secret, the site's api_site key), both set
8
8
  * at publish. The Worker calls rest-api /v3/public directly; rest-api caches
9
- * reads for 60 s. The listings are those of the site's own portal.
9
+ * reads for 60 s. With SITE_MODE=production the SDK also keeps them in the
10
+ * colo's Cache API (fresh 60 s, stale up to 24 h while it refreshes);
11
+ * previews read live. The listings are those of the site's own portal.
10
12
  * - Sandbox preview: the same two, set by the runtime in .dev.vars. The key is
11
13
  * a placeholder the sandbox firewall fills in. Real data; enquiries are
12
14
  * answered by the firewall, never sent.
@@ -30,7 +32,7 @@ import { createClient, createFixtureClient } from '@realadvisor/sites-sdk';
30
32
  import type { DevelopmentWithUnits, SitesClient } from '@realadvisor/sites-sdk/types';
31
33
  import { SITES_API_KEY, SITES_API_URL, SITES_FIXTURE } from 'astro:env/server';
32
34
  import fixtures from 'virtual:sites-kit/fixtures';
33
- import { previewApi } from '@realadvisor/sites-kit-core/env';
35
+ import { dataCache, previewApi } from '@realadvisor/sites-kit-core/env';
34
36
  import { agencyOf, type Agency, type SiteFixture } from '@realadvisor/sites-kit-core/agency';
35
37
  import { withTextLocale } from '@realadvisor/sites-kit-core/page-locale';
36
38
 
@@ -84,8 +86,9 @@ let fixture: { data: SiteFixture; client: SitesClient } | undefined;
84
86
  function liveClient(): SitesClient | null {
85
87
  const a = access();
86
88
  if (!a) return null;
87
- const id = `${a.url}\0${a.key}`;
88
- if (live?.id !== id) live = { id, client: createClient({ url: a.url, key: a.key }) };
89
+ const cache = dataCache(import.meta.env.PROD);
90
+ const id = `${a.url}\0${a.key}\0${!!cache}`;
91
+ if (live?.id !== id) live = { id, client: createClient({ url: a.url, key: a.key, cache }) };
89
92
  return live.client;
90
93
  }
91
94
 
package/middleware.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { defineMiddleware } from 'astro:middleware';
2
- import { setRuntimeEnv, siteMode, type SiteEnv } from '@realadvisor/sites-kit-core/env';
2
+ import { setRuntimeEnv, siteMode, withRequestContext, type SiteEnv, type WaitUntil } from '@realadvisor/sites-kit-core/env';
3
3
  import site from 'virtual:sites-kit/site';
4
4
  import { securityHeaders } from '@realadvisor/sites-kit-core/security';
5
5
  import { notFoundPage, renderFailed } from '@realadvisor/sites-kit-core/render-error';
@@ -50,7 +50,11 @@ function redirectFor(url: URL): string | null {
50
50
  }
51
51
 
52
52
  // Every request renders with its page locale (lib/page-locale.ts): the data helpers read texts in it.
53
- export const onRequest = defineMiddleware((context, next) => withPageLocale(localeOfPath(context.url.pathname), () => handle(context, next)));
53
+ // The request's ExecutionContext: the data cache refreshes stale copies through its waitUntil.
54
+ export const onRequest = defineMiddleware((context, next) => {
55
+ const ctx = (context.locals as { runtime?: { ctx?: WaitUntil } }).runtime?.ctx;
56
+ return withRequestContext(ctx, () => withPageLocale(localeOfPath(context.url.pathname), () => handle(context, next)));
57
+ });
54
58
 
55
59
  const handle: Parameters<typeof defineMiddleware>[0] = async (context, next) => {
56
60
  const locals = context.locals as { runtime?: { env?: SiteEnv } };
@@ -76,11 +80,11 @@ const handle: Parameters<typeof defineMiddleware>[0] = async (context, next) =>
76
80
  } else if ((res.status === 404 || res.status === 410) && !res.body) {
77
81
  res = notFoundPage(path, res.status);
78
82
  } else if (res.status >= 500 && !res.body) {
79
- res = renderFailed(path, new Error(`HTTP ${res.status} with no body`));
83
+ res = renderFailed(path, new Error(`HTTP ${res.status} with no body`), context.url.host);
80
84
  }
81
85
  } catch (err) {
82
86
  const status = (err as { status?: unknown } | null)?.status;
83
- res = status === 404 || status === 410 ? notFoundPage(path, status) : renderFailed(path, err);
87
+ res = status === 404 || status === 410 ? notFoundPage(path, status) : renderFailed(path, err, context.url.host);
84
88
  }
85
89
  try {
86
90
  for (const [k, v] of Object.entries(securityHeaders(siteMode() === 'preview'))) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realadvisor-sites/kit-astro",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "The Astro 5 kit for RealAdvisor sites on Cloudflare Workers: the integration (middleware, security headers, data env, site.json), the ra data client, and the components (images, listings search and map, enquiry form, legal, consent). Built on @realadvisor/sites-kit-core.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -39,8 +39,8 @@
39
39
  "./package.json": "./package.json"
40
40
  },
41
41
  "peerDependencies": {
42
- "@realadvisor/sites-kit-core": "^0.5.1",
43
- "@realadvisor/sites-sdk": "^0.5.1",
42
+ "@realadvisor/sites-kit-core": "^0.5.3",
43
+ "@realadvisor/sites-sdk": "^0.5.3",
44
44
  "astro": "^5.18.2"
45
45
  }
46
46
  }