webim-adapter 0.1.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.
- package/README.md +126 -0
- package/build/webim-adapter.js +1770 -0
- package/build/webim-embed.js +12 -0
- package/build/webim-ssr.js +1766 -0
- package/package.json +46 -0
- package/ssr.d.ts +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# webim-adapter
|
|
2
|
+
|
|
3
|
+
Render pages built with the WebIM page builder inside any React app.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install webim-adapter
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```jsx
|
|
10
|
+
import { WebimPage } from 'webim-adapter';
|
|
11
|
+
|
|
12
|
+
<WebimPage
|
|
13
|
+
apiUrl="https://api.example.com/api" // WebIM gateway base
|
|
14
|
+
tenant="acme" // tenant slug (X-Tenant header)
|
|
15
|
+
slug="landing" // published page slug
|
|
16
|
+
locale="tr" // optional content locale
|
|
17
|
+
mode="auto" // auto | light | dark
|
|
18
|
+
fallback={<Spinner />} // while loading
|
|
19
|
+
errorFallback={<NotFound />} // on 404 / network error
|
|
20
|
+
onLoad={(bundle) => (document.title = bundle.seo?.title ?? bundle.title)}
|
|
21
|
+
/>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Only published pages render — drafts 404. The component fetches the page
|
|
25
|
+
(`GET /render/:slug`) and the tenant's public theme/component libraries
|
|
26
|
+
(`GET /settings/public`) in parallel, then renders the block tree with the
|
|
27
|
+
same renderer the builder uses. Embedded WebIM forms submit to the public
|
|
28
|
+
forms engine automatically.
|
|
29
|
+
|
|
30
|
+
## SSR / prefetching
|
|
31
|
+
|
|
32
|
+
`WebimPage` fetches on mount. For server rendering, fetch the bundle
|
|
33
|
+
yourself and render the pure component:
|
|
34
|
+
|
|
35
|
+
```jsx
|
|
36
|
+
import { fetchPage, WebimContent } from 'webim-adapter';
|
|
37
|
+
|
|
38
|
+
// in a server loader
|
|
39
|
+
const bundle = await fetchPage({ apiUrl, tenant: 'acme', slug: 'landing' });
|
|
40
|
+
|
|
41
|
+
// in the component tree
|
|
42
|
+
<WebimContent bundle={bundle} mode="light" />
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Script-tag embed (no React required)
|
|
46
|
+
|
|
47
|
+
For plain HTML / WordPress-style sites, `build/webim-embed.js` is a
|
|
48
|
+
self-contained bundle (React included). Either drop containers and one tag:
|
|
49
|
+
|
|
50
|
+
```html
|
|
51
|
+
<div data-webim-page data-api="https://api.example.com/api"
|
|
52
|
+
data-tenant="acme" data-slug="landing" data-mode="auto"></div>
|
|
53
|
+
<script src="https://unpkg.com/webim-adapter/build/webim-embed.js"></script>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
…or let the script tag place the page right where it sits:
|
|
57
|
+
|
|
58
|
+
```html
|
|
59
|
+
<script src=".../webim-embed.js" data-api="…" data-tenant="acme" data-slug="landing"></script>
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`data-locale` and `data-mode` (`auto|light|dark`) work on both. Containers
|
|
63
|
+
added after load: `window.WebimEmbed.scan()`, or
|
|
64
|
+
`window.WebimEmbed.mount(el, { apiUrl, tenant, slug, locale, mode })`.
|
|
65
|
+
|
|
66
|
+
## Server-rendered HTML (zero-JS consumers / SEO)
|
|
67
|
+
|
|
68
|
+
The WebIM gateway serves a complete HTML document per published page:
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
GET /api/render/:slug/html?tenant=acme&locale=tr&mode=light
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Browser-navigable (tenant rides the query string), with the page's SEO meta
|
|
75
|
+
in the head and custom JS inlined. Static markup: theme, blocks, custom
|
|
76
|
+
CSS/JS and iframes all work; React-stateful pieces (engine-wired form
|
|
77
|
+
submits, sliders/tabs, Embed-block markup) stay inert — use the script
|
|
78
|
+
embed when those matter. The endpoint allows iframing, so
|
|
79
|
+
`<iframe src=".../render/landing/html?tenant=acme">` is also a valid embed.
|
|
80
|
+
|
|
81
|
+
Node servers can render themselves via `webim-adapter/ssr`:
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
import { fetchPage, renderPageDocument, renderBundleHtml } from 'webim-adapter/ssr';
|
|
85
|
+
|
|
86
|
+
const bundle = await fetchPage({ apiUrl, tenant, slug });
|
|
87
|
+
const document = renderPageDocument(bundle, { mode: 'light', lang: 'en' });
|
|
88
|
+
const bodyOnly = renderBundleHtml(bundle); // splice into your own layout
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Draft previews
|
|
92
|
+
|
|
93
|
+
Only published pages render publicly. To preview a draft, an authenticated
|
|
94
|
+
editor mints a signed 30-minute token:
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
POST /api/pages/:id/preview-token (Bearer auth)
|
|
98
|
+
→ { token, expires_in, slug }
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
The token unlocks that one page's current state everywhere:
|
|
102
|
+
|
|
103
|
+
```jsx
|
|
104
|
+
<WebimPage … preview={token} />
|
|
105
|
+
await fetchPage({ …, preview: token });
|
|
106
|
+
GET /api/render/:slug/html?tenant=acme&preview=<token>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The Pages list in the dashboard has a "Copy preview link" action that mints
|
|
110
|
+
a token and copies the ready-to-share HTML URL.
|
|
111
|
+
|
|
112
|
+
## Lower-level exports
|
|
113
|
+
|
|
114
|
+
- `PageRenderer` — render a raw `blocks` tree you already have.
|
|
115
|
+
- `renderRichContent(value)` — render a rich-text field value from a
|
|
116
|
+
collection entry.
|
|
117
|
+
- `resolveTheme(meta, themes)` — pick the theme tokens for a page.
|
|
118
|
+
|
|
119
|
+
## Notes
|
|
120
|
+
|
|
121
|
+
- Peer deps: `react` / `react-dom` 19+ (Google Font links rely on React 19
|
|
122
|
+
head hoisting).
|
|
123
|
+
- A page's custom code (`meta.custom.js` and Embed blocks) executes in the
|
|
124
|
+
host page — embed content only from tenants you trust, exactly like a tag
|
|
125
|
+
manager snippet.
|
|
126
|
+
- The WebIM gateway must allow CORS from the host origin.
|