polyfill structuredclone on legacy browsers

Fixes APP-T4SK
This commit is contained in:
Samuel Newman
2026-08-28 14:10:14 +01:00
parent 00dc9598fc
commit 3e48da80e4
4 changed files with 52 additions and 2 deletions
+29
View File
@@ -0,0 +1,29 @@
import {RichText} from '@bsky/sdk/richtext'
describe('web polyfills', () => {
const nativeStructuredClone = globalThis.structuredClone
afterEach(() => {
globalThis.structuredClone = nativeStructuredClone
})
it('supports rich text when structuredClone is unavailable', async () => {
Reflect.deleteProperty(globalThis, 'structuredClone')
let structuredCloneReady: Promise<void> | undefined
jest.isolateModules(() => {
const polyfills =
require('./polyfills.web') as typeof import('./polyfills.web')
structuredCloneReady = polyfills.structuredCloneReady
})
await structuredCloneReady
const richText = new RichText(
{text: 'hello\n\n\nworld'},
{cleanNewlines: true},
)
expect(richText.text).toBe('hello\n\nworld')
expect(richText.clone()).toEqual(richText)
})
})
+10 -2
View File
@@ -1,6 +1,16 @@
import 'array.prototype.findlast/auto'
import 'setimmediate'
export const structuredCloneReady: Promise<void> =
typeof globalThis.structuredClone === 'function'
? Promise.resolve()
: import('@ungap/structured-clone').then(({default: structuredClone}) => {
if (typeof globalThis.structuredClone !== 'function') {
globalThis.structuredClone =
structuredClone as typeof globalThis.structuredClone
}
})
if (process.env.NODE_ENV !== 'production') {
// In development, react-native-web's <View> tries to validate that
// text is wrapped into <Text>. It doesn't catch all cases but is useful.
@@ -30,5 +40,3 @@ if (process.env.NODE_ENV !== 'production') {
}
}
}
export {}