-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite polyfill suspense for Composer with preload
Hopefully this works
- Loading branch information
Showing
3 changed files
with
114 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { shouldPolyfill } from '@formatjs/intl-segmenter/should-polyfill'; | ||
import { useEffect, useState } from 'preact/hooks'; | ||
|
||
import Loader from './loader'; | ||
|
||
const supportsIntlSegmenter = !shouldPolyfill(); | ||
|
||
function importIntlSegmenter() { | ||
if (!supportsIntlSegmenter) { | ||
return import('@formatjs/intl-segmenter/polyfill-force').catch(() => {}); | ||
} | ||
} | ||
|
||
function importCompose() { | ||
return import('./compose'); | ||
} | ||
|
||
export async function preload() { | ||
try { | ||
await importIntlSegmenter(); | ||
importCompose(); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
export default function ComposeSuspense(props) { | ||
const [Compose, setCompose] = useState(null); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
try { | ||
if (supportsIntlSegmenter) { | ||
const component = await importCompose(); | ||
setCompose(component); | ||
} else { | ||
await importIntlSegmenter(); | ||
const component = await importCompose(); | ||
setCompose(component); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
})(); | ||
}, []); | ||
|
||
return Compose?.default ? <Compose.default {...props} /> : <Loader />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters