Adjust your app for Web Spotlight
What does your web app need to do to display interactive pages in Web Spotlight? Review a short checklist with code samples to see if your app is ready.
Check 1. Content preview
Use Delivery SDK in your app to preview content. We recommend using environment variables to avoid storing sensitive information directly in your code, such as your environment ID and API key. For example, this is how you can set up a client for Delivery API to preview content.import { createDeliveryClient } from "@kontent-ai/delivery-sdk";
export const deliveryClient = createDeliveryClient({
environmentId: process.env.KONTENT_AI_ENVIRONMENT_ID || '',
previewApiKey: process.env.KONTENT_AI_PREVIEW_API_KEY,
defaultQueryConfig: {
usePreviewMode: true
}
})
// This example uses generated models. See https://kontent.ai/learn/strongly-typed-models
const [landingPage, setLandingPage] = useState<LandingPage | null>(null);
// Retrieves the Website root item and saves the linked Landing page
useEffect(() => {
deliveryClient.item<WebSpotlightRoot>('website_root')
.toPromise()
.then(
response => (
// Website root uses the 'Content' element to link the Landing page
setLandingPage(response.data.item.elements.content.linkedItems[0])
)
)
}, [])
Check 2: Displaying a content item
Once your app can retrieve content items, it’s time to display their content. For example, this is how you can map your landing page elements to HTML elements.<img
src={landingPage.elements.image.value[0]?.url}
// If the asset doesn't have an alt text description, use its file name
alt={landingPage.elements.image.value[0]?.description ||
landingPage.elements.image.value[0]?.name}
height={landingPage.elements.image.value[0]?.height || 200}
width={landingPage.elements.image.value[0]?.width || 300} />
<h1>{landingPage.elements.title.value}</h1>
<div dangerouslySetInnerHTML={{ __html: landingPage.elements.body.value }}></div>
Check 3: Smart Link SDK
To enable in-context editing in Web Spotlight, use the Kontent.ai Smart Link SDK in your app. Make sure to initialize the SDK with basic information about your project’s environment.import KontentSmartLink from '@kontent-ai/smart-link';
const kontentSmartLink = KontentSmartLink.initialize({
defaultDataAttributes: {
projectId: process.env.KONTENT_AI_ENVIRONMENT_ID,
languageCodename: "default"
},
queryParam: "preview"
});
data-kontent-item-id
and data-kontent-element-codename
data attributes to add smart links for specific content elements.
// Specify content item ID
<div className="App-header" data-kontent-item-id={landingPage.system.id}>
<img
// Specify element codename using a strongly typed model
data-kontent-element-codename={contentTypes.landing_page.elements.image.codename}
className="App-logo"
src={landingPage.elements.image.value[0]?.url}
// If asset doesn't have alt text description, use its file name
alt={landingPage.elements.image.value[0]?.description ||
landingPage.elements.image.value[0]?.name}
height={landingPage.elements.image.value[0]?.height || 200}
width={landingPage.elements.image.value[0]?.width || 300}
/>
<h1
data-kontent-element-codename={contentTypes.landing_page.elements.title.codename}>
{landingPage.elements.title.value}
</h1>
<div
data-kontent-element-codename={contentTypes.landing_page.elements.body.codename}
dangerouslySetInnerHTML={{ __html: landingPage.elements.body.value }}>
</div>
</div>
Check 4: App deployment
Sign in with your Kontent.ai credentials or sign up for free to unlock the full lesson, track your progress, and access exclusive expert insights and tips!