This part of the library helps you to generate your own "sandbox" HTML (files) and serve them.
To allow only specific origins:
import {generateIframeHtml} from "@perspective-software/cross-origin-html-embed";
const iframeHtml = generateIframeHtml({
allowAllOrigins: false,
originWhitelist: ["https://app.myservice.com"],
});To allow all origins:
import {generateIframeHtml} from "@perspective-software/cross-origin-html-embed";
const iframeHtml = generateIframeHtml({
allowAllOrigins: true,
});| Option | Required | Description |
|---|---|---|
allowAllOrigins |
yes | If true, receiving and sending messages from/to all origins via asterisk * is activated. |
originWhitelist |
(depends on allowAllOrigins) |
A specific list of origins to receive messages from and sent messages to. |
name |
Will be added to the head title element. | |
canoncialUrl |
If set, the rel="canonical" link tag will be added to the head. |
|
favicon |
If set, this objects describes href and type for a link tag with rel="icon" to be added to the head. |
|
hideBranding |
If true, the iframe will not print the logo to the console. |
|
extendHead |
Optional HTML string which will be added before the end of the <head> tag. |
|
extendBody |
Optional HTML string which will be added before the end of the <body> tag. |
We have also prepared some example under examples/ 🔗.
Clone the repository and check them out.
There is also an example available: Express Example 🔗.
import {generateIframeHtml} from "@perspective-software/cross-origin-html-embed";
import express from "express";
const PORT = process.env.PORT || 4042;
const app = express();
app.get("/", (_, res) => {
// 🛡️ Here happens the magic
const iframeHtml = generateIframeHtml({
allowAllOrigins: false,
originWhitelist: ["https://app.myservice.com"],
});
res.send(iframeHtml);
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});There is also an example available: Next.js App Router Example 🔗.
// app/route.ts
import {generateIframeHtml} from "@perspective-software/cross-origin-html-embed";
export const dynamic = "force-dynamic"; // defaults to auto
export async function GET() {
// 🛡️ Here happens the magic
const iframeHtml = generateIframeHtml({
allowAllOrigins: false,
originWhitelist: ["https://app.myservice.com"],
name: "Next.js App Router Example",
});
const response = new Response(iframeHtml);
response.headers.set("Content-Type", "text/html");
return response;
}Basically, you just need to
- create an API endpoint/route handler
- and return the HTML with the correct HTML mime-type.
That's it.