Skip to content

Pitcher iframe Integration Approaches

Comparison of One-Time Parameter Passing vs. Real-Time Message Pipeline

Overview

Two primary approaches exist for integrating Pitcher applications via iframes:

  1. One-Time Parameter Passing: Parameters passed once during initialization
  2. Window Message Pipeline: Real-time bidirectional communication

Feature Comparison

AspectApproach 1: One-Time Parameter PassingApproach 2: Window Message Pipeline
CommunicationUnidirectional - Parameters passed once via URL query strings or iframe attributes during initializationBidirectional - Real-time two-way communication using postMessage API with event listeners
Data TransferLimited - URL length restrictions (~2000 chars), data must be serializable to query stringsUnlimited - Can send complex objects, binary data, and large payloads
API AccessStatic - Cannot access Pitcher API after initial load, requires page refresh for updatesDynamic - Real-time access to Pitcher API, can make requests on-demand
Event HandlingNone - No event bridge, iframe operates independentlyFull Support - Complete event bridge for user actions, state changes, and notifications
State PersistenceManual - Must implement own localStorage/sessionStorage logic
localStorage.setItem('lastPage', currentPage);
Coordinated - Can sync state with parent, use localStorage with parent awareness
parent.postMessage({type: 'saveState', page: currentPage}, '*');
SecurityBasic - Token/credentials exposed in URL, limited validation optionsEnhanced - Origin validation, secure token exchange, encrypted payloads possible
ImplementationSimple - Minimal code, quick setup
const params = new URLSearchParams(window.location.search);
Complex - Requires message handlers, protocol design, error handling
window.addEventListener('message', handleMessage);
MaintenanceLow - Fewer moving parts, less code to maintainHigher - Message protocol versioning, backwards compatibility concerns
PerformanceFast Initial Load - No message overhead, direct renderingSlight Overhead - Message serialization/deserialization, but enables lazy loading
User ExperienceStatic - Page refreshes for updates, limited interactivity with parentDynamic - Seamless updates, real-time synchronization, rich interactions

Use Case: "Remember Last Page iframe Was Left"

Approach 1: One-Time Parameters

Implementation:

javascript
// On iframe load
const lastPage = localStorage.getItem('lastPage');
if (lastPage) navigateToPage(lastPage);

// On page change
localStorage.setItem('lastPage', currentPage);

Limitations:

  • Parent unaware of navigation changes
  • Cannot sync across multiple sessions
  • Storage isolated to iframe origin
  • No way to clear from parent app

Approach 2: Message Pipeline

Implementation:

javascript
// iframe sends navigation updates
parent.postMessage({
    type: 'pageChanged',
    page: currentPage,
    timestamp: Date.now()
}, '*');

// Parent stores and returns on next load
window.addEventListener('message', (e) => {
    if (e.data.type === 'restoreState') {
        navigateToPage(e.data.lastPage);
    }
});

Advantages:

  • Parent tracks all navigation
  • Can sync across devices via Pitcher API
  • Centralized state management
  • Analytics and usage tracking

Both approaches are subject to browser cookie policies and iframe restrictions:

  • SameSite Cookie Restrictions: With SameSite=Lax (browser default), cookies are not sent in cross-origin iframe requests
  • Third-Party Cookie Blocking: Many browsers block third-party cookies in iframes by default (Safari ITP, Firefox ETP)
  • Storage Access: localStorage/sessionStorage are partitioned per origin - iframe storage is isolated from parent

Solutions Required:

  • Use SameSite=None; Secure for cross-origin cookies (requires HTTPS)
  • Implement Storage Access API for user consent
  • Consider server-side session management
  • Use postMessage for state sharing instead of relying on cookies

Impact: Authentication tokens, session data, and user preferences stored in cookies may not be accessible in cross-origin iframes without proper configuration.

When to Use Each Approach

When to Use Approach 1: One-Time Parameters

  • Simple, static content display
  • No need for runtime updates
  • Quick prototypes or MVPs
  • Limited integration requirements
  • Security-sensitive environments where minimal surface area is preferred
  • Bandwidth-constrained scenarios
  • When iframe content is mostly self-contained

When to Use Approach 2: Message Pipeline

  • Rich, interactive applications
  • Real-time data synchronization needs
  • Complex user workflows spanning parent and iframe
  • Analytics and tracking requirements
  • Multi-device or multi-tab state sync
  • Dynamic permission or configuration updates
  • When iframe needs to trigger parent actions

Additional Use Cases Comparison

Use CaseApproach 1Approach 2
User AuthenticationPass token once, handle expiry internallyRefresh tokens via parent, seamless re-auth
Data UpdatesRequires full reload for new dataPush updates in real-time, partial updates
Error HandlingIsolated error handling, no parent notificationCentralized error reporting, parent can react
AnalyticsSeparate tracking, duplicate implementationUnified tracking through parent application

Recommendations

  • For simple integrations: Start with Approach 1 and migrate to Approach 2 as needs grow
  • For enterprise applications: Implement Approach 2 from the start for future flexibility
  • For mixed scenarios: Consider hybrid approach - initial parameters with optional message channel