@realadvisor-sites/kit-react 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/README.md +2 -2
- package/package.json +3 -3
- package/server/data.ts +6 -4
- package/server/entry.server.tsx +20 -1
- package/server/worker.ts +17 -5
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ apply) and adds only what React needs. Reference site:
|
|
|
14
14
|
| --- | --- |
|
|
15
15
|
| `@realadvisor/sites-kit-react/vite` | `sitesKit({ inlineCss? })`: `virtual:sites-kit/site` (site.json), the site's bundled browser script, `build/client/_headers`, build settings. `inlineCss: true` (opt-in): the Worker inlines each page's built stylesheets as `<style>` (no render-blocking CSS request; style-src already allows it). |
|
|
16
16
|
| `…/worker` | `createSiteWorker(build)`: env, 301s, cross-site form check, per-request CSP nonce, full render before answering (500 never a cut 200), 404/410, security headers. |
|
|
17
|
-
| `…/entry.server` | `renderToReadableStream` with the nonce, waits for `allReady
|
|
17
|
+
| `…/entry.server` | `renderToReadableStream` with the nonce, waits for `allReady`; `handleError` logs a failed loader or render as one `site_error` line. |
|
|
18
18
|
| `…/server/data` | `siteClient({ fixture })`: one `SitesClient` (SDK interface), live with SITES_API_URL + SITES_API_KEY, else the fixture. |
|
|
19
19
|
| `…/server/kit` | `kitData()` for the root loader (mode, analytics id, Turnstile key, SITE_URL), `canonical(request)`, `pageLocale(request)`. |
|
|
20
20
|
| `…/server/enquiry` | `enquiryAction(request, sites)`: the Astro `/api/enquiry` contract, sent through the SDK's `handleEnquiry`. |
|
|
@@ -65,7 +65,7 @@ Approved for React Router sites only (2026-11-11): a per-request nonce in
|
|
|
65
65
|
```
|
|
66
66
|
site.json, wrangler.jsonc (main: workers/app.ts), vite.config.ts, react-router.config.ts
|
|
67
67
|
workers/app.ts createSiteWorker(() => import('virtual:react-router/server-build'))
|
|
68
|
-
app/entry.server.tsx export { default, streamTimeout } from '@realadvisor/sites-kit-react/entry.server'
|
|
68
|
+
app/entry.server.tsx export { default, handleError, streamTimeout } from '@realadvisor/sites-kit-react/entry.server'
|
|
69
69
|
app/root.tsx loader { kit: await kitData() }, <KitProvider>, <ConsentBanner>, <KitScripts>, ErrorBoundary -> 404/410 page
|
|
70
70
|
app/client.ts kit(); islands({...}); site behaviours
|
|
71
71
|
app/lib/data.server.ts export const sites = siteClient({ fixture })
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@realadvisor-sites/kit-react",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "The React Router (framework mode, SSR on Cloudflare Workers) version of the RealAdvisor sites kit: Worker with a per-request CSP nonce, data through the SDK interface, images, legal, consent, enquiry, SEO, listings search and map, and the deploy check. Reuses @realadvisor/sites-kit-core for every rule, string and browser script it can.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"sites-kit-react": "bin/sites-kit-react.mjs"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@realadvisor/sites-kit-core": "^0.5.
|
|
54
|
-
"@realadvisor/sites-sdk": "^0.5.
|
|
53
|
+
"@realadvisor/sites-kit-core": "^0.5.3",
|
|
54
|
+
"@realadvisor/sites-sdk": "^0.5.3",
|
|
55
55
|
"react": "^19.0.0",
|
|
56
56
|
"react-dom": "^19.0.0",
|
|
57
57
|
"react-router": "^7.18.0",
|
package/server/data.ts
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* export const sites = siteClient({ fixture });
|
|
8
8
|
*
|
|
9
9
|
* - SITES_API_URL + SITES_API_KEY set (publish, CRM preview, .dev.vars):
|
|
10
|
-
* the SDK's `createClient`, live data (the API caches 60 s
|
|
10
|
+
* the SDK's `createClient`, live data (the API caches 60 s; a production
|
|
11
|
+
* site also keeps reads in the colo's Cache API, fresh 60 s, stale 24 h).
|
|
11
12
|
* - Otherwise: the fixture (`pnpm dev`, the build, check:deploy), with the
|
|
12
13
|
* same filters as the API, so a page runs unchanged on both.
|
|
13
14
|
*
|
|
@@ -19,7 +20,7 @@
|
|
|
19
20
|
*/
|
|
20
21
|
import { createClient, createFixtureClient } from '@realadvisor/sites-sdk';
|
|
21
22
|
import type { Portals, SitesClient } from '@realadvisor/sites-sdk/types';
|
|
22
|
-
import { previewApi } from '@realadvisor/sites-kit-core/env';
|
|
23
|
+
import { dataCache, previewApi } from '@realadvisor/sites-kit-core/env';
|
|
23
24
|
import { withTextLocale } from '@realadvisor/sites-kit-core/page-locale';
|
|
24
25
|
import type { Fixture } from './fixture-client';
|
|
25
26
|
|
|
@@ -39,11 +40,12 @@ export function liveClient(portals?: Portals): SitesClient | null {
|
|
|
39
40
|
const { url, token } = previewApi();
|
|
40
41
|
if (!url || !token) return null;
|
|
41
42
|
// The SDK reads process.env; a Worker's env reaches it through setRuntimeEnv + this url/key.
|
|
42
|
-
const
|
|
43
|
+
const cache = dataCache(import.meta.env.PROD);
|
|
44
|
+
const key = `${url}\0${token}\0${JSON.stringify(portals ?? null)}\0${!!cache}`;
|
|
43
45
|
let live = clients.get(key);
|
|
44
46
|
if (!live) {
|
|
45
47
|
if (clients.size > 8) clients.clear();
|
|
46
|
-
live = createClient({ portals, url, key: token });
|
|
48
|
+
live = createClient({ portals, url, key: token, cache });
|
|
47
49
|
clients.set(key, live);
|
|
48
50
|
}
|
|
49
51
|
return live;
|
package/server/entry.server.tsx
CHANGED
|
@@ -9,14 +9,33 @@
|
|
|
9
9
|
* inline script a page adds. Always waits for the whole page (allReady):
|
|
10
10
|
* the Worker answers complete pages only (a render error is a 500, never a
|
|
11
11
|
* cut 200), crawlers and Lighthouse see the final HTML.
|
|
12
|
+
*
|
|
13
|
+
* `handleError` logs a loader, action or render error as one `site_error`
|
|
14
|
+
* line (kit-core lib/site-error.ts: a rejected site key has `code:
|
|
15
|
+
* 'site_key'`). A site re-exports it with the rest:
|
|
16
|
+
*
|
|
17
|
+
* export { default, handleError, streamTimeout } from '@realadvisor/sites-kit-react/entry.server';
|
|
12
18
|
*/
|
|
13
|
-
import type { AppLoadContext, EntryContext } from 'react-router';
|
|
19
|
+
import type { AppLoadContext, EntryContext, HandleErrorFunction } from 'react-router';
|
|
20
|
+
import { isRouteErrorResponse } from 'react-router';
|
|
21
|
+
import { logSiteError } from '@realadvisor/sites-kit-core/site-error';
|
|
14
22
|
import { ServerRouter } from 'react-router';
|
|
15
23
|
import { renderToReadableStream } from 'react-dom/server';
|
|
16
24
|
import { NonceContext } from '../components/Document';
|
|
17
25
|
|
|
18
26
|
export const streamTimeout = 8000;
|
|
19
27
|
|
|
28
|
+
export const handleError: HandleErrorFunction = (error, { request, context }) => {
|
|
29
|
+
// A cancelled request is not a failure; a thrown 404/410 is the not-found page.
|
|
30
|
+
if (request.signal.aborted) return;
|
|
31
|
+
if (isRouteErrorResponse(error) && error.status < 500) return;
|
|
32
|
+
const status = (error as { status?: unknown } | null)?.status;
|
|
33
|
+
if (status === 404 || status === 410) return;
|
|
34
|
+
const url = new URL(request.url);
|
|
35
|
+
logSiteError('page', error, { status: 500, path: url.pathname, host: url.host });
|
|
36
|
+
if (context && typeof context === 'object') (context as { errorLogged?: boolean }).errorLogged = true;
|
|
37
|
+
};
|
|
38
|
+
|
|
20
39
|
export default async function handleRequest(
|
|
21
40
|
request: Request,
|
|
22
41
|
responseStatusCode: number,
|
package/server/worker.ts
CHANGED
|
@@ -23,8 +23,9 @@
|
|
|
23
23
|
import { createRequestHandler, type ServerBuild } from 'react-router';
|
|
24
24
|
import site from 'virtual:sites-kit/site';
|
|
25
25
|
import inlineStyles from 'virtual:sites-kit-react/css';
|
|
26
|
-
import { setRuntimeEnv, siteMode, type SiteEnv } from '@realadvisor/sites-kit-core/env';
|
|
26
|
+
import { setRuntimeEnv, siteMode, withRequestContext, type SiteEnv } from '@realadvisor/sites-kit-core/env';
|
|
27
27
|
import { notFoundPage, renderFailed } from '@realadvisor/sites-kit-core/render-error';
|
|
28
|
+
import { logSiteError } from '@realadvisor/sites-kit-core/site-error';
|
|
28
29
|
import { localeOfPath } from '@realadvisor/sites-kit-core/i18n';
|
|
29
30
|
import { withPageLocale } from '@realadvisor/sites-kit-core/page-locale';
|
|
30
31
|
import { createNonce, siteHeaders } from './security';
|
|
@@ -36,6 +37,8 @@ export type SiteLoadContext = {
|
|
|
36
37
|
cloudflare: { env: SiteEnv & Record<string, unknown>; ctx: ExecutionContext };
|
|
37
38
|
/** This request's CSP nonce. */
|
|
38
39
|
nonce: string;
|
|
40
|
+
/** Set by the kit's handleError (entry.server) once it logged this request's error. */
|
|
41
|
+
errorLogged?: boolean;
|
|
39
42
|
};
|
|
40
43
|
|
|
41
44
|
declare module 'react-router' {
|
|
@@ -79,7 +82,7 @@ export function inlineCss(html: string, styles: Record<string, string> = inlineS
|
|
|
79
82
|
function exposeEnv(env: Record<string, unknown>) {
|
|
80
83
|
const proc = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process;
|
|
81
84
|
if (!proc?.env) return;
|
|
82
|
-
for (const k of ['SITE_MODE', 'SITES_API_URL', 'SITES_API_KEY', 'ANALYTICS_ID', 'SITE_URL']) {
|
|
85
|
+
for (const k of ['SITE_MODE', 'SITES_API_URL', 'SITES_API_KEY', 'ANALYTICS_ID', 'SITE_URL', 'SITE_SLUG', 'SITE_WORKER']) {
|
|
83
86
|
if (typeof env[k] === 'string' && env[k] && proc.env[k] !== env[k]) proc.env[k] = env[k] as string;
|
|
84
87
|
}
|
|
85
88
|
}
|
|
@@ -110,10 +113,12 @@ export function createSiteWorker(build: () => Promise<ServerBuild>) {
|
|
|
110
113
|
|
|
111
114
|
const nonce = createNonce();
|
|
112
115
|
const path = url.pathname;
|
|
116
|
+
const loadContext: SiteLoadContext = { cloudflare: { env, ctx }, nonce };
|
|
113
117
|
let res: Response;
|
|
114
118
|
try {
|
|
115
119
|
// The page locale (pageLocale(request)): siteClient reads texts in it.
|
|
116
|
-
|
|
120
|
+
// The request's ctx: the data cache refreshes stale copies through its waitUntil.
|
|
121
|
+
res = await withRequestContext(ctx, () => withPageLocale(localeOfPath(path), () => handle(request, loadContext)));
|
|
117
122
|
if ((res.headers.get('content-type') ?? '').includes('text/html') && res.body) {
|
|
118
123
|
const raw = await res.text(); // throws when rendering fails mid-stream
|
|
119
124
|
if (res.status === 200 && !raw.replace(/<!doctype html>/i, '').trim()) throw new Error('page rendered an empty body');
|
|
@@ -124,11 +129,18 @@ export function createSiteWorker(build: () => Promise<ServerBuild>) {
|
|
|
124
129
|
} else if ((res.status === 404 || res.status === 410) && !res.body) {
|
|
125
130
|
res = notFoundPage(path, res.status);
|
|
126
131
|
} else if (res.status >= 500 && !res.body) {
|
|
127
|
-
res = renderFailed(path, new Error(`HTTP ${res.status} with no body`));
|
|
132
|
+
res = renderFailed(path, new Error(`HTTP ${res.status} with no body`), url.host);
|
|
133
|
+
loadContext.errorLogged = true;
|
|
134
|
+
}
|
|
135
|
+
// The site's ServerError page (a loader threw, e.g. a rejected site
|
|
136
|
+
// key): logged by handleError when the site's entry.server exports
|
|
137
|
+
// the kit's; else one line here, so every 500 has its line.
|
|
138
|
+
if (res.status >= 500 && !loadContext.errorLogged) {
|
|
139
|
+
logSiteError('page', new Error(`HTTP ${res.status}`), { status: res.status, path, host: url.host });
|
|
128
140
|
}
|
|
129
141
|
} catch (err) {
|
|
130
142
|
const status = (err as { status?: unknown } | null)?.status;
|
|
131
|
-
res = status === 404 || status === 410 ? notFoundPage(path, status) : renderFailed(path, err);
|
|
143
|
+
res = status === 404 || status === 410 ? notFoundPage(path, status) : renderFailed(path, err, url.host);
|
|
132
144
|
}
|
|
133
145
|
// Only an HTML page carries inline scripts: the nonce goes in its CSP alone.
|
|
134
146
|
const html = (res.headers.get('content-type') ?? '').includes('text/html');
|