Skip to content

Commit b766f72

Browse files
chaxusclaude
andcommitted
test: add SW routing tests covering font-interception crash prevention
Documents the fetch routing rules in public/sw.js that prevent the OnlyOffice v7.5 units_per_EM crash: - Font files (.ttf/.woff/.woff2/.otf/.eot) must bypass the SW to avoid added latency that triggers Chrome's font-loading intervention - Document URLs (?src=, ?file=) bypass the cache to stay fresh - Cross-origin and non-GET requests are not intercepted Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d92dc58 commit b766f72

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

test/unit/sw-routing.test.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Tests for the fetch routing rules in public/sw.js.
3+
*
4+
* sw.js is a non-module service worker file that can't be imported directly,
5+
* so we replicate the routing conditions here as a living specification.
6+
* If sw.js changes, update both files together.
7+
*
8+
* The rules guard against two classes of bug found in this project:
9+
* - Font files intercepted by SW → added latency → Chrome "Slow Network"
10+
* intervention → OnlyOffice v7.5 fallback font crash (units_per_EM)
11+
* - Document URLs cached by SW → stale content served to editor
12+
*/
13+
14+
import { describe, expect, it } from 'vitest';
15+
16+
const FONT_REGEX = /\.(ttf|woff2?|otf|eot)(\?.*)?$/;
17+
18+
const ORIGIN = 'http://localhost:5173';
19+
20+
function swShouldHandle(method: string, urlStr: string): boolean {
21+
if (method !== 'GET') return false;
22+
const url = new URL(urlStr);
23+
if (url.origin !== ORIGIN) return false;
24+
if (url.searchParams.has('file') || url.searchParams.has('src')) return false;
25+
if (FONT_REGEX.test(url.pathname)) return false;
26+
return true;
27+
}
28+
29+
describe('SW fetch routing', () => {
30+
describe('non-GET requests are not handled', () => {
31+
it.each(['POST', 'PUT', 'DELETE', 'PATCH'])('%s', (method) => {
32+
expect(swShouldHandle(method, `${ORIGIN}/index.html`)).toBe(false);
33+
});
34+
});
35+
36+
describe('cross-origin requests are not handled', () => {
37+
it('skips external document URL', () => {
38+
expect(swShouldHandle('GET', 'https://example.com/doc.docx')).toBe(false);
39+
});
40+
41+
it('skips CDN asset', () => {
42+
expect(swShouldHandle('GET', 'https://cdn.example.com/font.ttf')).toBe(false);
43+
});
44+
});
45+
46+
describe('document query params bypass the SW cache', () => {
47+
it('skips ?src= URLs', () => {
48+
expect(swShouldHandle('GET', `${ORIGIN}/?src=https://example.com/doc.docx`)).toBe(false);
49+
});
50+
51+
it('skips ?file= URLs', () => {
52+
expect(swShouldHandle('GET', `${ORIGIN}/?file=report.xlsx`)).toBe(false);
53+
});
54+
55+
it('skips URL with both src and other params', () => {
56+
expect(swShouldHandle('GET', `${ORIGIN}/?src=doc.docx&readonly=true`)).toBe(false);
57+
});
58+
});
59+
60+
describe('font files are not intercepted (crash prevention)', () => {
61+
// Intercepting font files adds SW latency which triggers Chrome's
62+
// "Slow Network" font-loading intervention. OnlyOffice v7.5 then
63+
// crashes with "Cannot read properties of undefined (reading 'units_per_EM')"
64+
// in the fallback font code path of slide/word/cell sdk-all.js.
65+
it.each([
66+
['/web-apps/apps/common/main/resources/font/ASC.ttf', '.ttf (OnlyOffice internal font)'],
67+
['/fonts/NotoSansTC-VF.ttf', '.ttf (CJK fallback font)'],
68+
['/fonts/LiberationSans-Bold.woff2', '.woff2'],
69+
['/fonts/arial.woff', '.woff'],
70+
['/fonts/symbol.otf', '.otf'],
71+
['/fonts/legacy.eot', '.eot'],
72+
['/fonts/font.ttf?v=123', '.ttf with query string'],
73+
])('%s (%s)', (pathname) => {
74+
expect(swShouldHandle('GET', `${ORIGIN}${pathname}`)).toBe(false);
75+
});
76+
});
77+
78+
describe('font regex matches extensions correctly', () => {
79+
it.each(['.ttf', '.woff', '.woff2', '.otf', '.eot'])('matches %s', (ext) => {
80+
expect(FONT_REGEX.test(`/fonts/file${ext}`)).toBe(true);
81+
});
82+
83+
it('does not match .ttfx', () => {
84+
expect(FONT_REGEX.test('/fonts/file.ttfx')).toBe(false);
85+
});
86+
87+
it('does not match .js or .css', () => {
88+
expect(FONT_REGEX.test('/sdk-all.js')).toBe(false);
89+
expect(FONT_REGEX.test('/styles.css')).toBe(false);
90+
});
91+
92+
it('matches font extensions embedded in longer paths', () => {
93+
expect(FONT_REGEX.test('/web-apps/apps/common/main/resources/font/ASC.ttf')).toBe(true);
94+
});
95+
});
96+
97+
describe('same-origin static assets are handled', () => {
98+
it.each([
99+
`${ORIGIN}/index.html`,
100+
`${ORIGIN}/`,
101+
`${ORIGIN}/web-apps/apps/api/documents/api.js`,
102+
`${ORIGIN}/public/sdkjs/slide/sdk-all.js`,
103+
`${ORIGIN}/styles/base.css`,
104+
`${ORIGIN}/manifest.json`,
105+
`${ORIGIN}/img/64.png`,
106+
])('%s', (url) => {
107+
expect(swShouldHandle('GET', url)).toBe(true);
108+
});
109+
});
110+
});

0 commit comments

Comments
 (0)