@realadvisor-sites/kit-react 0.4.2 → 0.5.0

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.
@@ -75,21 +75,25 @@ export function useHydrate(): boolean {
75
75
  export function KitScripts() {
76
76
  const nonce = useNonce();
77
77
  const hydrate = useHydrate();
78
+ // Dev only (the CRM preview's bridge): empty in a build.
79
+ const dev = (client.dev ?? []).map((src) => <script key={src} type="module" src={src} nonce={nonce} />);
78
80
  if (hydrate) {
79
81
  return (
80
82
  <>
81
83
  <ScrollRestoration nonce={nonce} />
82
84
  <Scripts nonce={nonce} />
85
+ {dev}
83
86
  </>
84
87
  );
85
88
  }
86
- if (!client.src) return null;
89
+ if (!client.src) return <>{dev}</>;
87
90
  return (
88
91
  <>
89
92
  {client.preload.map((href) => (
90
93
  <link key={href} rel="modulepreload" href={href} />
91
94
  ))}
92
95
  <script type="module" src={client.src} nonce={nonce} />
96
+ {dev}
93
97
  </>
94
98
  );
95
99
  }
@@ -18,7 +18,6 @@
18
18
  * `data-*` attributes go on the <img> (as Astro's `...rest`).
19
19
  */
20
20
  import type { CSSProperties } from 'react';
21
- import { preload } from 'react-dom';
22
21
  import { imgUrl, srcsetUrl, type Crop } from '../lib/image';
23
22
 
24
23
  export type RaImageProps = {
@@ -86,14 +85,18 @@ export function RaImage(props: RaImageProps) {
86
85
  />
87
86
  );
88
87
  const usePicture = !!(mobileSet || avifSet);
89
- // Preloads (React puts them in <head>): the format the browser will pick first (AVIF when there is one).
90
- if (priority) {
91
- if (mobileSet) preload(fallback, { as: 'image', type: mobileAvif ? 'image/avif' : undefined, media: MOBILE, imageSrcSet: mobileAvif || mobileSet, imageSizes: mobileSizes, fetchPriority: 'high' });
92
- if (set) preload(fallback, { as: 'image', type: avifSet ? 'image/avif' : undefined, media: mobileSet ? DESKTOP : undefined, imageSrcSet: avifSet || set, imageSizes: sizes, fetchPriority: 'high' });
93
- }
94
- if (!usePicture) return image;
88
+ // Preloads (React 19 hoists a <link> into <head>): the format the browser will pick first (AVIF when there is one).
89
+ // A <link>, not react-dom's preload(): kit components import nothing CommonJS, which the dev Worker would load raw.
90
+ const preloads = priority ? (
91
+ <>
92
+ {mobileSet && <link rel="preload" as="image" href={fallback} type={mobileAvif ? 'image/avif' : undefined} media={MOBILE} imageSrcSet={mobileAvif || mobileSet} imageSizes={mobileSizes} fetchPriority="high" />}
93
+ {set && <link rel="preload" as="image" href={fallback} type={avifSet ? 'image/avif' : undefined} media={mobileSet ? DESKTOP : undefined} imageSrcSet={avifSet || set} imageSizes={sizes} fetchPriority="high" />}
94
+ </>
95
+ ) : null;
96
+ if (!usePicture) return <>{preloads}{image}</>;
95
97
  return (
96
98
  <>
99
+ {preloads}
97
100
  <picture>
98
101
  {mobileAvif && <source media={MOBILE} type="image/avif" srcSet={mobileAvif} sizes={mobileSizes} width={mobile!.width} height={mobile!.height} />}
99
102
  {mobileSet && <source media={MOBILE} srcSet={mobileSet} sizes={mobileSizes} width={mobile!.width} height={mobile!.height} />}
package/env.d.ts CHANGED
@@ -8,7 +8,12 @@ declare module 'virtual:sites-kit/site' {
8
8
  }
9
9
  declare module 'virtual:sites-kit-react/client' {
10
10
  /** The site's bundled browser script and its static imports. */
11
- const client: { src: string; preload: string[] };
11
+ const client: {
12
+ src: string;
13
+ preload: string[];
14
+ /** Dev only: scripts the CRM preview adds (SITES_PREVIEW_BRIDGE). Empty in a build. */
15
+ dev?: string[];
16
+ };
12
17
  export default client;
13
18
  }
14
19
  declare module 'virtual:sites-kit-react/css' {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realadvisor-sites/kit-react",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
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.4.2",
54
- "@realadvisor/sites-sdk": "^0.4.2",
53
+ "@realadvisor/sites-kit-core": "^0.5.0",
54
+ "@realadvisor/sites-sdk": "^0.5.0",
55
55
  "react": "^19.0.0",
56
56
  "react-dom": "^19.0.0",
57
57
  "react-router": "^7.18.0",
package/vite.mjs CHANGED
@@ -29,7 +29,7 @@
29
29
  *
30
30
  * Plain JS on purpose: vite.config loads it natively.
31
31
  */
32
- import { existsSync, writeFileSync } from 'node:fs';
32
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
33
33
  import { join, relative, resolve } from 'node:path';
34
34
  import { fileURLToPath } from 'node:url';
35
35
  import { readSiteConfig, SITE_CONFIG_FILE } from '@realadvisor/sites-kit-core/site-config';
@@ -38,6 +38,14 @@ import { securityHeaders } from '@realadvisor/sites-kit-core/security';
38
38
  const SITE_ID = 'virtual:sites-kit/site';
39
39
  const CLIENT_ID = 'virtual:sites-kit-react/client';
40
40
  const CSS_ID = 'virtual:sites-kit-react/css';
41
+ /**
42
+ * Dev only: a browser script the CRM preview adds to every page (its
43
+ * bridge: path reporting, element picking). SITES_PREVIEW_BRIDGE is the
44
+ * path of that file, outside the site; `<KitScripts>` loads it with the
45
+ * request's nonce. Never in a build.
46
+ */
47
+ const BRIDGE_ID = 'virtual:sites-kit-react/preview-bridge';
48
+ const BRIDGE_ENV = 'SITES_PREVIEW_BRIDGE';
41
49
  const resolved = (id) => `\0${id}`;
42
50
  const KIT_PACKAGES = ['@realadvisor/sites-kit-core', '@realadvisor/sites-kit-react', '@realadvisor/sites-sdk'];
43
51
 
@@ -134,6 +142,7 @@ export default function sitesKit(opts = {}) {
134
142
  },
135
143
  resolveId(id, importer) {
136
144
  if (id === SITE_ID || id === CLIENT_ID || id === CSS_ID) return resolved(id);
145
+ if (id === BRIDGE_ID && command === 'serve') return resolved(id);
137
146
  // From JS only: an @import inside a stylesheet stays plain CSS.
138
147
  if (importer && !/\.(css|scss|sass|less)(\?|$)/.test(importer) && isKitCss(id.split('?')[0])) return KIT_CSS_ID;
139
148
  return undefined;
@@ -151,9 +160,16 @@ export default function sitesKit(opts = {}) {
151
160
  if (command === 'serve') {
152
161
  const file = resolve(root, clientEntry);
153
162
  const src = existsSync(file) ? `${base}${relative(root, file).split('\\').join('/')}` : '';
154
- return `export default ${JSON.stringify({ src, preload: [] })};`;
163
+ const bridge = process.env[BRIDGE_ENV];
164
+ // Vite's URL of a virtual module (\0 becomes __x00__).
165
+ const dev = bridge && existsSync(bridge) ? [`${base}@id/__x00__${BRIDGE_ID}`] : [];
166
+ return `export default ${JSON.stringify({ src, preload: [], dev })};`;
155
167
  }
156
- return `export default ${JSON.stringify(built)};`;
168
+ return `export default ${JSON.stringify({ ...built, dev: [] })};`;
169
+ }
170
+ if (id === resolved(BRIDGE_ID)) {
171
+ const bridge = process.env[BRIDGE_ENV];
172
+ return bridge && existsSync(bridge) ? readFileSync(bridge, 'utf8') : '';
157
173
  }
158
174
  return undefined;
159
175
  },