The polyfetch module provides a way to search for a page by name across a neighborhood of wiki sites.
# Frame
http://wiki.ralfbarkow.ch/assets/pages/polyfetch/polyfetch.html
Leave **Sites** empty, press **Find** with the default slug “Async Polymorphic Fetch”.
In a second example, we enter Why Wiki Forks
Slug/Name: Why Wiki Forks
and press Find.
Type 'ward.bay.wiki.org' into the Sites field.
Sites: ward.bay.wiki.org
and press Find gain.
# Assets
pages/polyfetch
–– chatgpt ![]()
What the polyfetch snippet does (and why it’s good) **Concept recap (why “Polymorphism”):** Instead of one big `if/else` to find a page, we “send the same message” (fetch `slug.json`) to **many possible receivers** (sites). The first site that “understands” returns the page. We prioritize receivers (current site → journal hints → neighbors → guesses), remember the last winner per slug (a **PIC** cache), and stop others when we get a hit (abort-on-win).
**What the snippet is (documented):**
```html <div class="polyfetch-snippet"></div> <script type="module"> // (1) Anchor safely (FedWiki may not set currentScript): const container = document.querySelector('.polyfetch-snippet') || document.body; // (2) Load the ESM module hosted by your wiki (right MIME, same origin): const { makePolyFetcher, makeFetchWikiAdapter } = await import('/assets/pages/polyfetch/polyfetch.mjs'); // (3) Inject a tiny UI (slug input, comma-separated extra sites, log box): container.innerHTML = `...`; // Helpers for DOM and JSON logging to the UI: const $ = sel => container.querySelector(sel); const log = x => $("#log").textContent = typeof x === 'string' ? x : JSON.stringify(x, null, 2); // (4) Build a wiki adapter that matches the page’s scheme (fixes localhost CORS): const wiki = makeFetchWikiAdapter({ defaultScheme: location.protocol.replace(':','') }); // A base fetcher pinned to the current origin (you can clone with more sites later): const baseFetcher = makePolyFetcher({ wiki, neighborhood: { hosts: [ location.origin ] }, concurrency: 8, timeoutMs: 5000 }); // Load any local page JSON to harvest journal hints (seed sites): async function loadLocalPageJson(path='/welcome-visitors.json'){ const r = await fetch(path); return r.ok ? r.json() : { journal: [] }; } // Click handler: read inputs → make a fetcher with extra sites → polyget → log result: $("#go").addEventListener("click", async (ev) => { ev.preventDefault(); const name = $("#slug").value.trim() || 'Async Polymorphic Fetch'; const extra = $("#sites").value.split(',').map(s => s.trim()).filter(Boolean); const page = await loadLocalPageJson(); const { polyget } = makePolyFetcher({ wiki, neighborhood: { hosts: [ location.origin, ...extra ] }, concurrency: 8, timeoutMs: 5000 }); log({ event: "searching", name, sites: [ location.origin, ...extra ] }); const res = await polyget({ name, site: location.host, page }); log(res ? { site: res.site, title: res.page?.title ?? '(no title)' } : 'Not found.'); }); </script> ```
**What you’ve gained:** * ✅ **Same-origin ESM** (no CSP/CORS surprises) and **right MIME** from `/assets/pages/...`. * ✅ **Scheme-correct fetches** for localhost (`defaultScheme` mirrors the page), so no mixed-content/CORS. * ✅ **Receiver ordering** that reflects “collaborative link” intuition (local → journal → neighbors). * ✅ **Fast path** via a tiny PIC cache and **abort-on-first-hit** (wastes no bandwidth after a win). * ✅ **Reproducible logs** in the page UI for debugging & GT transcript parsing later. * ✅ **Flat repo**: the snippet and module live inside your `assets` repo—no submodules.
**Usage examples (on your wiki):** 1. **Just local + journal hints** Open the page with the snippet, leave **Sites** empty, press **Find** with the default slug “Async Polymorphic Fetch”. → You should see `{ site: "http://localhost:3000", title: "Async Polymorphic Fetch" }`. 2. **Add trusted remotes** In **Sites**, enter: `https://wiki.ralfbarkow.ch,http://found.ward.bay.wiki.org` and **Find**. → The search tries your origin first, then the remotes. Result shows which site answered. 3. **Different slug** Type another page name (not slug; the module slugifies for you), e.g. “Collaborative Link”. → Works the same; check the log for `event: "searching"` and eventual `site/title`.
4. **Console variant (quick smoke):**
const m = await import('/assets/pages/polyfetch/polyfetch.mjs'); const wiki = m.makeFetchWikiAdapter({ defaultScheme: location.protocol.replace(':','') }); const { polyget } = m.makePolyFetcher({ wiki, neighborhood:{hosts:[location.origin]} }); const page = await (await fetch('/welcome-visitors.json')).json(); await polyget({ name:'Async Polymorphic Fetch', site: location.host, page });
2. Open DevTools → Console on your wiki and paste:
// load the module you served const { makePolyFetcher, makeFetchWikiAdapter } = await import('http://localhost:3000/assets/pages/polyfetch/polyfetch.mjs'); // build adapter + fetcher const wiki = makeFetchWikiAdapter(); // uses window.fetch const { polyget } = makePolyFetcher({ wiki, neighborhood: { hosts: [location.host, 'found.ward.bay.wiki.org'] }, concurrency: 8, timeoutMs: 5000 }); // get any local page JSON to supply a journal (seed sites) const page = await (await fetch('/welcome-visitors.json')).json(); // try a lookup by *name* (slugging happens inside) const ctx = { name: 'Polyfetch', site: location.host, page }; const res = await polyget(ctx); console.log('Found at:', res && res.site, 'title:', res && res.page && res.page.title);