Let editors click a heading on your live site and change it from the Lynkow dashboard. The dashboard opens your site in an iframe, and
@lynkow/nextconnects the two throughpostMessage.
Reference - This guide is a tutorial. For exhaustive, always-current signatures and field lists, see the auto-generated reference:
SDK: Lynkow SDK
Prerequisites
A Next.js App Router site using
lynkowand@lynkow/next(Add Lynkow to an Existing Project).A page or global block in Lynkow whose fields you render (Pages & Site Configuration).
Your site served over HTTPS, also in development.
Step 1: Let the dashboard frame your site
The dashboard runs on https://app.lynkow.com and loads your page with a lynkow-preview query parameter. On those requests your site must send Content-Security-Policy: frame-ancestors 'self' https://app.lynkow.com and no X-Frame-Options header.
If your proxy.ts uses createLynkowProxy() (Multi-language), it already does this. Otherwise, use the preview helper:
// proxy.ts
import { lynkowPreviewMiddleware } from '@lynkow/next/proxy'
export default lynkowPreviewMiddleware()
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)'],
}To keep a proxy you already have, wrap it: export default withLynkowPreview(yourProxy). Your own Content-Security-Policy is kept. On a preview request its frame-ancestors is replaced to allow the dashboard and X-Frame-Options is removed. On other requests your frame-ancestors stays, or frame-ancestors 'self' is added when you set none.
Without a proxy, set the header in next.config.ts and import the origin rather than typing it:
// next.config.ts
import type { NextConfig } from 'next'
import { DEFAULT_CMS_ORIGIN } from 'lynkow/visual-editor'
const nextConfig: NextConfig = {
async headers() {
return [
{
source: '/(.*)',
headers: [{ key: 'Content-Security-Policy', value: `frame-ancestors 'self' ${DEFAULT_CMS_ORIGIN}` }],
},
]
},
}
export default nextConfigAlso remove any X-Frame-Options header your host or reverse proxy adds: some browsers let it override frame-ancestors.
Step 2: Mount the provider
// app/layout.tsx
import type { ReactNode } from 'react'
import { LynkowVisualEditor } from '@lynkow/next/client'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>
<LynkowVisualEditor>{children}</LynkowVisualEditor>
</body>
</html>
)
}The provider adds no element. It starts only when the page is inside an iframe and its URL carries lynkow-preview. Otherwise it registers no listener and scans nothing. Once started, it scans the page, sends the dashboard a lynkow:handshake message listing the blocks and fields it found, and accepts messages from https://app.lynkow.com only.
Pass no props. cmsOrigin exists only to develop against a dashboard other than production. If you set it, pass the same value to lynkowPreviewMiddleware({ cmsOrigin }) or withLynkowPreview(yourProxy, { cmsOrigin }). An empty string disables the editor and logs [Lynkow] cmsOrigin was passed as an empty value. Omit it to use the default.
Step 3: Mark the editable fields
Wrap the markup of a block in <EditableBlock> and each field in <Editable>. Both render from Server Components:
// app/page.tsx
import { EditableBlock, Editable } from '@lynkow/next/client'
import { lynkow } from '@/lib/lynkow'
export default async function HomePage() {
const page = await lynkow.pages.getBySlug('home')
const { title, intro } = page.data as { title: string; intro: string }
return (
<EditableBlock slug="home" as="main">
<Editable field="title" label="Title" as="h1">{title}</Editable>
<Editable field="intro" label="Introduction" type="richtext" as="div" html={intro} />
</EditableBlock>
)
}slugis the slug of the page or global block, exactly as in Lynkow.assets the element, so no extra wrapper is added.fieldis the key in the block's schema. Pass the text aschildren, or a richtext value ashtml.typedefaults to'text', which the editor edits inline on the page. The other types (richtext,url,image,number,date,select,boolean) open the field in the dashboard panel.Outside the editor, each field renders the value you passed. Inside it, the field shows what the editor types, without a reload.
Rules the editor's scanner applies:
A field is found only inside the element of its block.
<Editable>without an<EditableBlock>ancestor logs an error in development.The page is scanned once, when the editor starts. A field not in the DOM then (an empty section you hide) can still be edited from the dashboard's field list.
Only flat keys can be marked. Repeater rows (a list of links, a feature grid) stay editable from the dashboard. Do not mark their container: inline editing would flatten the list to text.
A marked field that the block's schema does not declare is ignored.
Raw attributes
<Editable> renders four data- attributes, which you can write yourself when a component does not fit, for an <img> for example:
Attribute | On | Value |
|---|---|---|
| the block's element | the block slug |
| a descendant of it | the field key |
| the field | the field type, |
| the field | the badge label, the key when absent |
Step 4: Read live values with hooks
Hooks from @lynkow/next/client return the editor's live value inside the editor and your initial value everywhere else. Call them in a Client Component rendered inside the block:
'use client'
import { useLynkowField } from '@lynkow/next/client'
export function Logo({ src }: { src: string }) {
const url = useLynkowField('home', 'logo', src)
return <img src={url} alt="" data-lynkow-field="logo" data-lynkow-type="image" data-lynkow-label="Logo" />
}useLynkowField(blockSlug, fieldKey, initialValue)returns one field.useBlockData(blockSlug, initialData)returns the whole block, with the editor's values merged overinitialData. Use it for a repeater you render yourself.useIsPreviewMode()returnstrueinside the editor, for example to hide a cookie banner while editing.
Step 5: Set the Preview URL
In the dashboard, go to Settings > General > Branding.
Set Preview URL to your site's address, for example
https://www.example.com. It must use HTTPS. When it is empty, the dashboard useshttps://plus the site's domain.Open a page under Pages and click Visual editor.
The dashboard loads the Preview URL plus the page's path, with ?lynkow-preview=true. In development, expose your dev server through an HTTPS tunnel such as ngrok http 3000 and set its URL as the Preview URL: browsers block an http:// page inside the HTTPS dashboard.
Troubleshooting
When no handshake arrives within 5 seconds, the dashboard shows Visual Editor SDK not detected. Check, in order:
Symptom | Cause and fix |
|---|---|
Blank iframe, no error | The Preview URL uses |
Blank iframe, console shows | The CSP does not allow |
Page loads, no handshake |
|
Overlay appears, nothing is editable | No |
Preview URL not configured | Set Preview URL, or the site's domain, in Settings > General > Branding. |
To read the iframe's console in Chrome, right-click the iframe and choose Inspect.
Next Steps
Draft Preview - render unpublished content on your site.
Pages & Site Configuration - read pages and global blocks.