|
| 1 | +--- |
| 2 | +id: m4x7pq2n |
| 3 | +title: Preview URL Modifier |
| 4 | +description: Learn how to inject custom query parameters into Website Builder's live preview URLs. |
| 5 | +--- |
| 6 | + |
| 7 | +import { Alert } from "@/components/Alert"; |
| 8 | + |
| 9 | +<Alert type="success" title="WHAT YOU'LL LEARN"> |
| 10 | + |
| 11 | +- what the PreviewUrlModifier extension point is and where it applies |
| 12 | +- how to implement and register a custom preview URL modifier |
| 13 | +- how to use async operations (e.g. fetching a signed token) inside a modifier |
| 14 | + |
| 15 | +</Alert> |
| 16 | + |
| 17 | +## Overview |
| 18 | + |
| 19 | +Every live preview URL that Website Builder generates — in the page editor iframe, the address bar's copy link button, and the pages list — can be customized via the `PreviewUrlModifier` extension point. |
| 20 | + |
| 21 | +When registered, the modifier receives the fully-constructed `URL` object just before it is used. You can add, remove, or change any query parameter, including ones fetched asynchronously from a remote API. |
| 22 | + |
| 23 | +<Alert type="info"> |
| 24 | + |
| 25 | +Only one `PreviewUrlModifier` can be registered per project. Avoid setting parameters that start with `wb.` — those are reserved for internal Website Builder use. |
| 26 | + |
| 27 | +</Alert> |
| 28 | + |
| 29 | +## Implement the Modifier |
| 30 | + |
| 31 | +Create a class that implements `PreviewUrlModifier.Interface`. The single required method is `modify(url: URL): Promise<void>`. Mutate the `url` object in place — the return value is ignored. |
| 32 | + |
| 33 | +```typescript extensions/previewUrlModifier/MyPreviewUrlModifier.ts |
| 34 | +import { PreviewUrlModifier } from "webiny/admin/website-builder"; |
| 35 | + |
| 36 | +class MyPreviewUrlModifier implements PreviewUrlModifier.Interface { |
| 37 | + async modify(url: URL) { |
| 38 | + url.searchParams.set("my-param", "my-value"); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +export default PreviewUrlModifier.createImplementation({ |
| 43 | + implementation: MyPreviewUrlModifier, |
| 44 | + dependencies: [] |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +### Async Example — Fetching a Signed Token |
| 49 | + |
| 50 | +Mark the method `async` to perform any async work before the URL is handed back: |
| 51 | + |
| 52 | +```typescript extensions/previewUrlModifier/MyPreviewUrlModifier.ts |
| 53 | +import { PreviewUrlModifier } from "webiny/admin/website-builder"; |
| 54 | + |
| 55 | +class MyPreviewUrlModifier implements PreviewUrlModifier.Interface { |
| 56 | + async modify(url: URL) { |
| 57 | + const token = await fetch("/api/preview-token").then(r => r.text()); |
| 58 | + url.searchParams.set("token", token); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export default PreviewUrlModifier.createImplementation({ |
| 63 | + implementation: MyPreviewUrlModifier, |
| 64 | + dependencies: [] |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +## Register the Feature |
| 69 | + |
| 70 | +Wire the implementation into the DI container using `createFeature` and `RegisterFeature`: |
| 71 | + |
| 72 | +```tsx extensions/previewUrlModifier/index.tsx |
| 73 | +import React from "react"; |
| 74 | +import { createFeature, RegisterFeature } from "webiny/admin"; |
| 75 | +import MyPreviewUrlModifier from "./MyPreviewUrlModifier.js"; |
| 76 | + |
| 77 | +const PreviewUrlModifierFeature = createFeature({ |
| 78 | + name: "MyApp/PreviewUrlModifier", |
| 79 | + register(container) { |
| 80 | + container.register(MyPreviewUrlModifier); |
| 81 | + } |
| 82 | +}); |
| 83 | + |
| 84 | +export default () => <RegisterFeature feature={PreviewUrlModifierFeature} />; |
| 85 | +``` |
| 86 | + |
| 87 | +Then register the extension in `webiny.config.tsx`: |
| 88 | + |
| 89 | +```tsx webiny.config.tsx |
| 90 | +import React from "react"; |
| 91 | +import { Admin } from "webiny/extensions"; |
| 92 | + |
| 93 | +export const Extensions = () => { |
| 94 | + return ( |
| 95 | + <> |
| 96 | + {/* ... other extensions */} |
| 97 | + <Admin.Extension src={"@/extensions/previewUrlModifier/index.tsx"} /> |
| 98 | + </> |
| 99 | + ); |
| 100 | +}; |
| 101 | +``` |
| 102 | + |
| 103 | +## Where the Modifier Applies |
| 104 | + |
| 105 | +| Surface | Description | |
| 106 | +|---|---| |
| 107 | +| Editor iframe | The live-editing iframe in the page editor | |
| 108 | +| Address bar link | The "copy preview link" button in the editor toolbar | |
| 109 | +| Pages list | Preview icon links in the pages list table | |
0 commit comments