Preview mode
Composable architectures need a layer where the systems come together and an experience is built. In Uniform, users can leverage data from integrated systems to create "compositions." These compositions can be created, edited, and published from our contextual editor, so that authors can see what the end user will see.
These instructions assume you already have Canvas activated in your front-end application. For instructions, see activation.
Activate front-end
The activation process depends on the technology used to build the front-end. In general, however, the process involves changing the code that reads the composition from Uniform so the most recent configuration (i.e. the draft version) from Uniform is used.
Next.js
In order to make these instructions easier to follow, a specific example is used. The example activates preview mode for the following page:
Sample page used in this example
import Head from 'next/head'
import {
CanvasClient,
} from '@uniformdev/canvas'
import {
UniformComposition,
UniformSlot,
} from '@uniformdev/canvas-react'
export async function getStaticProps() {
const client = new CanvasClient({
apiKey: process.env.UNIFORM_API_KEY,
projectId: process.env.UNIFORM_PROJECT_ID,
})
const { composition } = await client.getCompositionBySlug({
slug: "/",
})
return {
props: {
title: 'Sample app',
composition,
}
}
}
function UnknownComponent({ component }) {
return <div>[unknown component: {component.type}]</div>
}
function resolveRenderer({ type }) {
//TODO: Add your components.
return UnknownComponent
}
export default function Home({ title, composition }) {
return (
<UniformComposition data={composition} resolveRenderer={resolveRenderer}>
<div>
<Head>
<title>{title}</title>
</Head>
<main>
<UniformSlot name='body' />
</main>
</div>
</UniformComposition>
)
}
Add preview route handler
A number of new files are needed in order to support preview mode.
Add the following values to your
.env
file:Environment variable Description UNIFORM_PREVIEW_SECRET
This can be any value you want. However, this value must be included in the preview URL that you will configure in a later step. About this stepTechnically you do not need to set the preview secret in an environment variable. You could hard-code the value into your app, but using environment variables is a "best practice."
Add the secret to the
serverRuntimeConfig
in your app'snext.config.js
:next.config.jsconst nextConfig = {
[...]
serverRuntimeConfig: {
[...]
previewSecret: process.env.UNIFORM_PREVIEW_SECRET,
},
};Install dependencies
npm install @uniformdev/canvas-next
Add the following preview handler:
pages/api/preview.jsimport getConfig from 'next/config'
import { createPreviewHandler } from '@uniformdev/canvas-next'
const handler = createPreviewHandler({
secret: () => getConfig().serverRuntimeConfig.previewSecret,
})
export default handlerAbout this stepThis adds a handler to redirect preview requests to the right page. You can read more about Preview Mode in Nextjs docs.
Edit page files
Next you need to make changes to the pages that read compositions from Uniform.
- Next.js (steps)
- Next.js (complete)
This section guides you through the process of activating preview mode by explaining each step. It takes longer to go through, but it will help you understand why each line of code is needed.
In the page file, import the composition state constants:
import Head from 'next/head'
import {
CanvasClient,
CANVAS_DRAFT_STATE,
CANVAS_PUBLISHED_STATE,
} from '@uniformdev/canvas'
import {
UniformComposition,
UniformSlot,
} from '@uniformdev/canvas-react'
...Conditionally set the right state code if we are in preview mode or not:
export async function getStaticProps({ preview }) {
const client = new CanvasClient({
apiKey: process.env.UNIFORM_API_KEY,
projectId: process.env.UNIFORM_PROJECT_ID,
})
const { composition } = await client.getCompositionBySlug({
slug: '/',
state: preview ? CANVAS_DRAFT_STATE : CANVAS_PUBLISHED_STATE,
})
return {
props: {
title: 'Sample app',
composition,
}
}
}Continue with the section Configure Uniform project.
This section gets you to activating preview mode as quickly as possible. It does not explain each line of code.
In the page file, add the following code:
import Head from 'next/head'
import {
CanvasClient,
CANVAS_DRAFT_STATE,
CANVAS_PUBLISHED_STATE,
} from '@uniformdev/canvas'
import {
UniformComposition,
UniformSlot,
} from '@uniformdev/canvas-react'
export async function getStaticProps({ preview }) {
const client = new CanvasClient({
apiKey: process.env.UNIFORM_API_KEY,
projectId: process.env.UNIFORM_PROJECT_ID,
})
const { composition } = await client.getCompositionBySlug({
slug: '/',
state: preview ? CANVAS_DRAFT_STATE : CANVAS_PUBLISHED_STATE,
})
return {
props: {
title: 'Sample app',
composition,
}
}
}
function UnknownComponent({ component }) {
return <div>[unknown component: {component.type}]</div>
}
function resolveRenderer({ type }) {
//TODO: Add your components.
return UnknownComponent
}
export default function Home({ title, composition }) {
return (
<UniformComposition data={composition} resolveRenderer={resolveRenderer}>
<div>
<Head>
<title>{title}</title>
</Head>
<main>
<UniformSlot name='body' />
</main>
</div>
</UniformComposition>
)
}Continue with the section Configure Uniform project.
Nuxt 3
The Uniform module for Nuxt integrates preview mode out of the box. No front-end application modifications are needed. But you still need to configure the Uniform project in order for the preview button to work.
Configure Uniform project
Start your front-end application.
can users access the application?If you are the only person using your Uniform project, you can run the application locally. However, if other users need to use the preview feature, the front-end application must run somewhere that the other users can access it. This might be on a server on your network, or on an internet-accessible CDN like Netlify or Vercel.
https and httpYou will only get the automatic update on save when your front-end application is accessed using https:// due to standard browser security.
http:// will only work with localhost (e.g. http://localhost:3000), but this requires any user who wants to use the preview feature to have the front-end application running locally. This is only recommended for Uniform projects where the developer is the only person using the project.
In Uniform, open your project.
Navigate to Settings > Canvas Settings.
Enter the following value for Preview URL:
- Next.js
- Nuxt 3
https://[!!! APP HOST NAME !!!]/api/preview?secret=[!!! PREVIEW SECRET !!!]
Token Value [!!! APP HOST NAME !!!]
The host name where the Next.js application is running. Any user who wants to use the preview feature must be able to access this host from their browser. [!!! PREVIEW SECRET !!!]
The value you specified for the preview secret in the .env
file.https://[!!! APP HOST NAME !!!]?preview=true
Token Value [!!! APP HOST NAME !!!]
The host name where the Nuxt application is running. Any user who wants to use the preview feature must be able to access this host from their browser. tipWhen in preview mode, the⚡️emoji is prepended to the title of the page.
Click Save.
Now when you open a composition in Canvas, a button Preview is available.