Attach fetch
In plain terms: observation belongs on the transport. The host wraps fetch once; every user-initiated call that should inform OCE must go through that wrapper (or the reporter port).
Host attach (before remotes render)
import {
getPageConnectivityEngine,
type PageConnectivityEngine,
} from '@strixun/api-framework/connectivity';
let fetchWithOce: typeof fetch;
export function bootstrapOce(): PageConnectivityEngine {
const engine = getPageConnectivityEngine({
failureThreshold: 2,
healthCheckUrl: '/health',
probeDeadline: 'Interactive',
});
const base = globalThis.fetch.bind(globalThis);
fetchWithOce = engine.attachFetch(base);
return engine;
}
/** Pass to apps that accept a custom fetch implementation */
export function getFetchWithOce(): typeof fetch {
if (!fetchWithOce) {
throw new Error('Call bootstrapOce() at host entry before remotes load');
}
return fetchWithOce;
}
Entry bootstrap (Next.js, Vite host shell, etc.):
// host/src/main.tsx
import { bootstrapOce } from './oce-bootstrap';
bootstrapOce();
// then render microfrontends
Rules:
- Call
getPageConnectivityEngineonce with config at host startup. - Replace or inject
fetchWithOceinto shared clients before remote bundles execute. - Remotes call
getPageConnectivityEngine()withoutcreateConnectivityEngine.
Microfrontend remotes (subscribe only)
// remote/src/ConnectivityHint.tsx
import { useEffect, useState } from 'react';
import { getPageConnectivityEngine } from '@strixun/api-framework/connectivity';
export function ConnectivityHint() {
const engine = getPageConnectivityEngine();
const [unavailable, setUnavailable] = useState(
() => engine.getSnapshot().unavailable,
);
useEffect(() => {
return engine.subscribe(() => {
setUnavailable(engine.getSnapshot().unavailable);
});
}, [engine]);
if (!unavailable) return null;
return (
<p role="status">We could not reach the network recently. You can still retry.</p>
);
}
Remotes must not:
- Call
createConnectivityEngineagain - Pass a different
healthCheckUrlexpecting it to apply - Swap
globalThis.fetchafter the host attached
Wiring HTTP clients to the wrapper
// Example: pass custom fetch into a thin client
import { getFetchWithOce } from '../host/oce-bootstrap';
export const api = createRestClient({
baseUrl: '/api',
fetch: getFetchWithOce(),
});
For TanStack Query, Apollo HttpLink, SWR fetcher, axios adapter — see Recipes.
@strixun/api-framework secureFetch
secureFetch is attachFetch around the HTTPS-safe base fetch. APIClient call shape does not change. Apps already on the client inherit observation.
import { getPageConnectivityEngine } from '@strixun/api-framework/connectivity';
const engine = getPageConnectivityEngine();
function upgradeToHttps(input: RequestInfo | URL): RequestInfo | URL {
// existing secureFetch policy — before or inside attach; observation sees final call
return input;
}
const base = globalThis.fetch.bind(globalThis);
async function secureBaseFetch(
input: RequestInfo | URL,
init?: RequestInit,
): Promise<Response> {
const safeInput = upgradeToHttps(input);
return base(safeInput, init);
}
export const secureFetch = engine.attachFetch(secureBaseFetch);
// api-framework consumer — unchanged call site
import { secureFetch } from '@strixun/api-framework';
const response = await secureFetch('/api/me', { credentials: 'include' });
See also the API Framework product page for package entry points.
Subscribe API (read the hint)
const engine = getPageConnectivityEngine();
const unsubscribe = engine.subscribe(() => {
const snap = engine.getSnapshot();
// { unavailable, streak, failureThreshold, generation, healthContractFailed? }
showHint(snap.unavailable);
});
// cleanup
unsubscribe();
Apps without fetch
Stacks that use XHR, WebSocket-only signaling, or native bridges must use reporter at the transport boundary. There is no dedicated axios or TanStack package — those call attachFetch or reporter.
Service worker note
Host documentation should include a one-line worker bypass for healthCheckUrl. OCE does not ship a worker. Example pattern:
// sw.js — do not cache the health probe
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
if (url.pathname === '/health') {
return; // network only
}
// ... normal strategies
});
Sources (specified): OCE plan — attach recipes, secureFetch, one writer on the page, service worker advisory.