Skip to content

Commit 63977a2

Browse files
gregbergeclaude
andcommitted
Document page and visitor context in webframes
Add the site:page:context scope (and the previously undocumented site:visitor:claims) and document that webframes receive state.page ({ id, path, title }) and state.visitor through the postMessage state, in the interactivity guides and the ContentKit component reference. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 11be552 commit 63977a2

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

docs/integrations/configurations.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ The scopes your integration has permissions for.
9898
- site:script:inject ## Internal scope - see note below
9999
- site:script:cookies ## Internal scope - see note below
100100
- site:visitor:auth ## Enable workflows related to authenticated access
101+
- site:visitor:claims ## Expose visitor claims to webframes
102+
- site:page:context ## Expose the current page (id, path, title) to webframes
101103
- site:adaptive:read ## Read claims available from Adaptive Content
102104
- site:adaptive:write ## Write claims avaiable to Adaptive Content
103105
# OpenAPI

docs/integrations/contentkit/interactivity.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ window.addEventListener("message", (event) => {
9191
});
9292
```
9393

94+
## Page and visitor context
95+
96+
In addition to the values you bind through the `data` prop, GitBook injects contextual information about the current site into the webframe `state`, as long as your integration requests the matching [scope](../configurations.md#scopes):
97+
98+
- `state.page` — the current page as `{ id, path, title }`, when the integration has the `site:page:context` scope.
99+
- `state.visitor` — the visitor claims, when the integration has the `site:visitor:claims` scope.
100+
101+
This context is delivered client-side through the same `message` event as your bound `data`, so requesting it does not change how the integration block is cached:
102+
103+
```js
104+
window.addEventListener("message", (event) => {
105+
const state = event.data?.state;
106+
if (!state) return;
107+
108+
// Current page — only present when the integration has the `site:page:context` scope.
109+
if (state.page) {
110+
const { id, path, title } = state.page;
111+
// e.g. build a link to a sibling page from `path`
112+
}
113+
});
114+
```
115+
94116
## Webframes and actions
95117
96118
Webframes are powerful elements to integrate in GitBook external applications or complete UI. Passing data to the webframe can be done using the `data` prop. But the webframe also needs to be able to communicate data back to the top component. It can be achieved using the `window.postMessage`:

docs/integrations/development/contentkit/reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ If you need a copy button, only add it when your integration has a supported way
264264
| `buttons` | `Array<Button>` | Overlay buttons |
265265
| `data` | `Record<string, string>` | State dependencies |
266266

267+
The `data` values, together with GitBook-provided context, reach the frame through the `message` event as `event.data.state`. GitBook reserves two keys in `state`: `page` (`{ id, path, title }` of the current page, with the `site:page:context` scope) and `visitor` (visitor claims, with the `site:visitor:claims` scope). See [Interactivity](../../contentkit/interactivity.md#page-and-visitor-context).
268+
267269
#### `select`
268270

269271
```tsx

docs/integrations/guides/interactivity.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ window.addEventListener("message", (event) => {
9595
});
9696
```
9797

98+
### Page and visitor context
99+
100+
In addition to the values you bind through the `data` prop, GitBook injects contextual information about the current site into the webframe `state`, as long as your integration requests the matching [scope](../configurations.md#scopes):
101+
102+
- `state.page` — the current page as `{ id, path, title }`, when the integration has the `site:page:context` scope.
103+
- `state.visitor` — the visitor claims, when the integration has the `site:visitor:claims` scope.
104+
105+
This context is delivered client-side through the same `message` event as your bound `data`, so requesting it does not change how the integration block is cached:
106+
107+
```js
108+
window.addEventListener("message", (event) => {
109+
const state = event.data?.state;
110+
if (!state) return;
111+
112+
// Current page — only present when the integration has the `site:page:context` scope.
113+
if (state.page) {
114+
const { id, path, title } = state.page;
115+
// e.g. build a link to a sibling page from `path`
116+
}
117+
});
118+
```
119+
98120
### Editable blocks
99121
100122
Some blocks might be static or only generated from link unfurling, but most blocks are designed to be editable by the user. Editable means that the user can interact with the blocks to change its properties.

0 commit comments

Comments
 (0)