How to seamlessly Embed iFrame in Prismic Headless CMS

Introduction

Prismic is a headless content management system (CMS) that leverages an API to organize and distribute content for developers and content teams. By separating the front end from the content management back end, developers can build websites and applications using any front-end technology, such as React, Next.js, Vue, and more.

While Prismic doesn’t have a built-in method for embedding iframe, you can achieve this using a Rich Text or Key Text field. In this blog, we’ll guide you through the process of embedding iframe in your Prismic-powered Next.js site, making it easy to display external content effortlessly

Why Use an iframe in Prismic and Its Common Uses?

Why Use an iframe in Prismic and Its Common Uses?Embedding iframe in your Prismic-powered Next.js site is a convenient way to display external content without hosting it yourself. This approach is beneficial for various purposes, including:

YouTube or Vimeo Videos

YouTube or Vimeo Videos By embedding videos from YouTube or Vimeo, you can deliver high-quality content without consuming your server’s storage. This ensures smooth playback, optimized performance, and access to platform features like captions and adaptive streaming.

Google Maps

Google Maps An iframe makes it easy to embed interactive Google Maps, allowing users to find locations without leaving your site. This is ideal for displaying business addresses, directions, or multiple location markers.

Google Forms and Surveys

Google Forms and Surveys Integrate forms from platforms like Google Forms or Typeform to collect user responses efficiently. This ensures secure data storage while maintaining a seamless user experience.

PDFs & Documents

PDFs & Documents Iframe enable you to embed PDFs, Word documents, or manuals directly on your site. This allows users to view, read, or download files without needing additional software.

avatar
Are you ready?

Hi, my name is Jaswinder, let's talk about your business needs.

I will do my best to find a reliable solution for you!

Embedding iframe in Prismic with a Slice

Since Prismic does not provide a built-in method for adding iframe, we will use a Slice to dynamically manage and display iframe content. In the next steps, we’ll guide you through creating and integrating an iframe Slice in Prismic, making it easy for content editors to embed iframe without touching any code.

Step 1: Create the iframe Slice in Prismic

  • Open your local Slice Machine.

  • Click on "Slices" and create a new Slice named IframeEmbed.

  • Navigate to your Custom Type (e.g., Page or Blog Post) and add the newly created Slice.

  • Add a Key Text field and name it iframe Url (this will store the iframe link).

(You can also choose the field name according to your preferences)

Step 1 Create the iframe Slice in Prismic

  • Click Save and push changes from the Review changes.

Click Save and push changes from the Review changes (2)Step 2: Add the iframe Slice in Prismic

  • Go to your Prismic Dashboard.

  • Choose the page where you want to display the Slice.

  • Add the iframe code you want to embed.
    *Below Example Of Youtube iframe

<iframe width="560" height="315" src="https://www.youtube.com/embed/RK1RRVR9A2g?si=69-zYJlVqoqVx7eT" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
  • Save the changes and publish the page.

Save the changes and publish the page (3)Step 3: Fetch and Render Slices on Your Page

  • Ensure your Next.js page (pages/index.js or pages/[uid].js) is set up to render slices:

import Head from "next/head";
  import { SliceZone } from "@prismicio/react";
  import { components } from "../../slices";
  import { createClient } from "@/prismicio";
  export default function Page({page}) {
    return (
      <>
        <Head>
          <title>{page.data.meta_title}</title>
          <meta name="description" content={page.data.meta_description} />
        </Head>
        <div>
          <SliceZone slices={page.data.slices} components={components} />
        </div>
      </>
    );
  }

  export async function getStaticProps({ previewData }) {
    const client = createClient({ previewData });
    const page = await client.getSingle("homepage");
    return {
      props: { page },
    };
  }
  • Then navigate to your "Slices" folder and locate the IframeEmbed/index.js file

  • Update it to render the iframe

// slice/IframeEmbed/index.js
  import React from "react";
  const IframeEmbed = ({ slice }) => {
    const iframeUrl = slice.primary.Iframe_url; // Extracting the iframe URL from the slice
    return (
      <section
        data-slice-type={slice.slice_type}
        data-slice-variation={slice.variation}
      >
        <div dangerouslySetInnerHTML={{ __html: iframeUrl }} /> // Rendering the iframe URL as HTML
      </section> /
    );
  };
  export default IframeEmbed;
  • If you have a direct link for the src attribute, such as:

"https://www.youtube.com/embed/RK1RRVR9A2g?si=69-zYJlVqoqVx7eT"
*You can use the following code:

import React from "react";
  const IframeEmbed = ({ slice }) => {
    const iframeUrl = slice.primary.Iframe_url;
    return (
      <section
        data-slice-type={slice.slice_type}
        data-slice-variation={slice.variation}
      >
        <iframe
          width="560"
          height="315"
          src={iframeUrl}
          title="YouTube video player"
          frameborder="0"
          allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
          referrerpolicy="strict-origin-when-cross-origin"
          allowfullscreen
        ></iframe>
      </section>
    );
  };

  export default IframeEmbed;
  • Open your Next.js site and check if the iframe is embedded correctly.

Open your Nextjs site and check if the iframe is embedded correctly (4)Seamlessly Manage Embedded Content in Prismic

Embedding iframe content in Prismic-powered Next.js websites enhances functionality and user experience without requiring complex backend modifications. Whether you're integrating videos, maps, forms, or external web pages, using Slices allows content editors to manage embeds dynamically. This approach not only simplifies content updates but also ensures a clean and structured implementation, keeping your website flexible and scalable.
Seamlessly Manage Embedded Content in Prismic

How We Can Help with Your Prismic Development

At RW Infotech, we specialize in Prismic and Next.js development, helping businesses create seamless, dynamic, and scalable web experiences. Whether you need custom slices, efficient content workflows, or optimized iframe embedding, our expert team ensures smooth integration tailored to your needs. Let us handle the technical complexities so you can focus on delivering exceptional content. Get in touch with RW Infotech today and elevate your Prismic-powered website!

Faq's

Frequently Asked Questions

Find answers to the most common questions about Embed iFrame in Prismic