@traffic-opt/core
v0.1.7
Published
Core utilities for traffic optimization packages.
Readme
@traffic-opt/core
Browser-side iframe loader for creating real, eager iframes from a small init({ sites }) API.
The package is designed for cases where the embedded page should produce normal browser requests and referral signals. It does not guarantee Similarweb, UV, or any third-party analytics outcome, because those systems depend on browser policy, target-site headers, cookies, anti-abuse systems, and the target site's own analytics tags.
Install
pnpm add @traffic-opt/coreBasic Usage
import { init } from "@traffic-opt/core";
const controller = init({
sites: ["https://example.com/"],
iframeStyle: {
width: "100%",
height: 600,
opacity: 1,
border: 0
}
});
await controller.resize();
controller.destroy();API
init({
sites: [
"https://a.com/",
{ url: "https://b.com/", title: "Site B" }
],
container: "#traffic-opt",
iframeClassName: "traffic-opt-frame",
iframeStyle: {
width: "100%",
height: 600,
opacity: 1,
border: 0
},
minHeight: 600,
referrerPolicy: "no-referrer",
resizer: true
});sites: Required. URL strings or{ url, title }objects.container: A CSS selector orHTMLElement. If omitted, the loader creates adivand appends it todocument.body.iframeClassName: Optional class name for created iframes.iframeStyle: Inline iframe-only styles forwidth,height,minHeight,maxHeight,opacity, andborder. Numeric size values may be0or greater and are treated as px.opacitymay be any value from0to1.minHeight: Default iframe minimum height. Defaults to600px; numbers may be0or greater and are treated as px.referrerPolicy: Defaults tono-referrer.resizer: Enabled by default through@open-iframe-resizer/core. Passfalseto disable it or pass an object to forward resizer settings.
By default, created iframes use the real src, loading="eager", no sandbox, and no hidden layout such as display:none or hidden positioning. The default size is visible and non-zero, with normal browser opacity, but you may explicitly configure zero width, height, minHeight, maxHeight, or opacity when you need to test browser request behavior. The library does not inject global CSS.
The loader does not bypass X-Frame-Options or CSP frame-ancestors. If a target site blocks iframe embedding, this package cannot solve that from the browser.
React
import { useEffect, useRef } from "react";
import { init } from "@traffic-opt/core";
import type { TrafficOptController } from "@traffic-opt/core";
export default function App() {
const containerRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
if (containerRef.current === null) {
return undefined;
}
const controller: TrafficOptController = init({
sites: [{ url: "https://example.com/", title: "Example" }],
container: containerRef.current,
iframeStyle: {
width: "100%",
height: 600,
opacity: 1,
border: 0
},
resizer: false
});
return () => {
controller.destroy();
};
}, []);
return <div ref={containerRef} />;
}Vue
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from "vue";
import { init } from "@traffic-opt/core";
import type { TrafficOptController } from "@traffic-opt/core";
const containerRef = ref<HTMLElement | null>(null);
let controller: TrafficOptController | undefined;
onMounted(() => {
if (containerRef.value === null) {
return;
}
controller = init({
sites: [{ url: "https://example.com/", title: "Example" }],
container: containerRef.value,
iframeStyle: {
width: "100%",
height: 600,
opacity: 1,
border: 0
},
resizer: false
});
});
onBeforeUnmount(() => {
controller?.destroy();
});
</script>
<template>
<div ref="containerRef" />
</template>Plain HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>TrafficOpt HTML Example</title>
</head>
<body>
<div id="traffic-opt-example"></div>
<script src="https://cdn.jsdelivr.net/npm/@traffic-opt/[email protected]/dist/index.global.js"></script>
<script>
TrafficOpt.init({
sites: [{ url: "https://example.com/", title: "Example" }],
container: "#traffic-opt-example",
iframeStyle: {
width: "100%",
height: 600,
opacity: 1,
border: 0
},
resizer: false
});
</script>
</body>
</html>