pageLang: report X's language even when installed before <html> exists or before the IPC bridge
At document start (where the app installs the X runtime) there is no <html> element yet, so the lang report never ran and "Follow X" never learned X's language (found live: siteLang stayed null). It now reads lang again at readystatechange/DOMContentLoaded, and a report that fails (no bridge yet) is retried with a bounded backoff. Tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0d6e7d3323
commit
d5b5c3f71d
2 changed files with 114 additions and 4 deletions
|
|
@ -12,17 +12,52 @@ export function readPageLang(doc: Document): string | null {
|
||||||
return normalizeLocale(doc.documentElement?.getAttribute('lang') ?? '');
|
return normalizeLocale(doc.documentElement?.getAttribute('lang') ?? '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Retries of a report that failed (the IPC bridge is not there yet at document start). */
|
||||||
|
export const LANG_REPORT_RETRIES = 12;
|
||||||
|
|
||||||
export function installPageLangReport(win: Window & typeof globalThis, invoke?: Invoke): { dispose(): void } {
|
export function installPageLangReport(win: Window & typeof globalThis, invoke?: Invoke): { dispose(): void } {
|
||||||
if (!invoke) return { dispose() {} };
|
if (!invoke) return { dispose() {} };
|
||||||
let last = '';
|
let last = '';
|
||||||
|
let failures = 0;
|
||||||
|
let timer: number | undefined;
|
||||||
|
let disposed = false;
|
||||||
const check = () => {
|
const check = () => {
|
||||||
|
timer = undefined;
|
||||||
|
if (disposed) return;
|
||||||
const lang = readPageLang(win.document);
|
const lang = readPageLang(win.document);
|
||||||
if (!lang || lang === last) return;
|
if (!lang || lang === last) return;
|
||||||
last = lang;
|
last = lang;
|
||||||
invoke('tab_report', { report: { lang } }).catch(() => { last = ''; });
|
invoke('tab_report', { report: { lang } }).then(() => { failures = 0; }, () => {
|
||||||
|
// The page's `lang` rarely changes after load, so a report lost at document start (no bridge
|
||||||
|
// yet) would never be sent again: "Follow X" would not learn X's language. Try again.
|
||||||
|
if (last === lang) last = '';
|
||||||
|
if (disposed || timer !== undefined || failures >= LANG_REPORT_RETRIES) return;
|
||||||
|
failures += 1;
|
||||||
|
timer = win.setTimeout(check, Math.min(4000, 125 * 2 ** failures));
|
||||||
|
});
|
||||||
};
|
};
|
||||||
const observer = new win.MutationObserver(check);
|
const observer = new win.MutationObserver(check);
|
||||||
if (win.document.documentElement) observer.observe(win.document.documentElement, { attributes: true, attributeFilter: ['lang'] });
|
let observed: Element | null = null;
|
||||||
check();
|
// At document start (where the app installs this) there may be no <html> element yet: watch it,
|
||||||
return { dispose: () => observer.disconnect() };
|
// and read `lang`, as soon as the parser has made it.
|
||||||
|
const attach = () => {
|
||||||
|
const root = win.document.documentElement;
|
||||||
|
if (root && root !== observed) {
|
||||||
|
observed = root;
|
||||||
|
observer.observe(root, { attributes: true, attributeFilter: ['lang'] });
|
||||||
|
}
|
||||||
|
check();
|
||||||
|
};
|
||||||
|
win.document.addEventListener('readystatechange', attach);
|
||||||
|
win.document.addEventListener('DOMContentLoaded', attach);
|
||||||
|
attach();
|
||||||
|
return {
|
||||||
|
dispose: () => {
|
||||||
|
disposed = true;
|
||||||
|
observer.disconnect();
|
||||||
|
win.document.removeEventListener('readystatechange', attach);
|
||||||
|
win.document.removeEventListener('DOMContentLoaded', attach);
|
||||||
|
if (timer !== undefined) win.clearTimeout(timer);
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
75
packages/runtime/tests/pageLang.test.ts
Normal file
75
packages/runtime/tests/pageLang.test.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import test from 'node:test';
|
||||||
|
import { installPageLangReport, LANG_REPORT_RETRIES } from '../src/pageLang.ts';
|
||||||
|
|
||||||
|
/** A page whose <html lang> never changes, with timers we step by hand. */
|
||||||
|
function fakePage(lang: string, parsed = true) {
|
||||||
|
const timers: Array<() => void> = [];
|
||||||
|
const listeners = new Map<string, () => void>();
|
||||||
|
const root = { getAttribute: (name: string) => (name === 'lang' ? lang : null) };
|
||||||
|
const document = {
|
||||||
|
documentElement: parsed ? root : null,
|
||||||
|
addEventListener: (type: string, fn: () => void) => { listeners.set(type, fn); },
|
||||||
|
removeEventListener: (type: string) => { listeners.delete(type); },
|
||||||
|
};
|
||||||
|
const win = {
|
||||||
|
document,
|
||||||
|
MutationObserver: class { observe() {} disconnect() {} },
|
||||||
|
setTimeout: (fn: () => void) => { timers.push(fn); return timers.length; },
|
||||||
|
clearTimeout: () => {},
|
||||||
|
} as unknown as Window & typeof globalThis;
|
||||||
|
const parse = () => { document.documentElement = root; listeners.get('DOMContentLoaded')?.(); };
|
||||||
|
return { win, timers, parse, listeners };
|
||||||
|
}
|
||||||
|
const settle = () => new Promise(resolve => setImmediate(resolve));
|
||||||
|
|
||||||
|
test('a language report lost at document start (no IPC bridge yet) is sent again', async () => {
|
||||||
|
const { win, timers } = fakePage('en-GB');
|
||||||
|
const sent: unknown[] = [];
|
||||||
|
let bridge = false;
|
||||||
|
installPageLangReport(win, async (command, args) => {
|
||||||
|
if (!bridge) throw new Error('IPC bridge not ready');
|
||||||
|
sent.push([command, args]);
|
||||||
|
});
|
||||||
|
await settle();
|
||||||
|
assert.equal(sent.length, 0);
|
||||||
|
assert.equal(timers.length, 1, 'a retry is scheduled');
|
||||||
|
bridge = true;
|
||||||
|
timers.shift()!();
|
||||||
|
await settle();
|
||||||
|
assert.deepEqual(sent, [['tab_report', { report: { lang: 'en-GB' } }]]);
|
||||||
|
assert.equal(timers.length, 0, 'nothing more once it went through');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('retries are bounded and stop after dispose', async () => {
|
||||||
|
const { win, timers } = fakePage('tr');
|
||||||
|
let calls = 0;
|
||||||
|
const handle = installPageLangReport(win, async () => { calls += 1; throw new Error('down'); });
|
||||||
|
for (let i = 0; i < LANG_REPORT_RETRIES + 5; i += 1) { await settle(); timers.shift()?.(); }
|
||||||
|
await settle();
|
||||||
|
assert.equal(calls, LANG_REPORT_RETRIES + 1);
|
||||||
|
assert.equal(timers.length, 0);
|
||||||
|
|
||||||
|
const second = fakePage('ja');
|
||||||
|
let later = 0;
|
||||||
|
const h2 = installPageLangReport(second.win, async () => { later += 1; throw new Error('down'); });
|
||||||
|
await settle();
|
||||||
|
h2.dispose();
|
||||||
|
second.timers.shift()?.();
|
||||||
|
await settle();
|
||||||
|
assert.equal(later, 1, 'no report after dispose');
|
||||||
|
handle.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('installed at document start before <html> exists, it reports once the parser made it', async () => {
|
||||||
|
const page = fakePage('en-GB', false);
|
||||||
|
const sent: unknown[] = [];
|
||||||
|
const handle = installPageLangReport(page.win, async (command, args) => { sent.push([command, args]); });
|
||||||
|
await settle();
|
||||||
|
assert.equal(sent.length, 0);
|
||||||
|
page.parse();
|
||||||
|
await settle();
|
||||||
|
assert.deepEqual(sent, [['tab_report', { report: { lang: 'en-GB' } }]]);
|
||||||
|
handle.dispose();
|
||||||
|
assert.equal(page.listeners.size, 0);
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue