forked from getfider/fider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncPages.tsx
More file actions
34 lines (29 loc) · 934 Bytes
/
AsyncPages.tsx
File metadata and controls
34 lines (29 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { lazy, ComponentType } from "react"
type LazyImport = () => Promise<{ default: ComponentType<any> }>
const MAX_RETRIES = 6
const INTERVAL = 1000
const retry = (fn: LazyImport, retriesLeft = MAX_RETRIES, waitMs = INTERVAL): Promise<{ default: ComponentType<any> }> => {
return new Promise((resolve, reject) => {
fn()
.then(resolve)
.catch((err) => {
setTimeout(() => {
if (retriesLeft === 1) {
reject(new Error(`${err} after ${MAX_RETRIES} retries`))
return
}
retry(fn, retriesLeft - 1, INTERVAL + INTERVAL).then(resolve, reject)
}, waitMs)
})
})
}
const load = (fn: LazyImport) => lazy(() => retry(() => fn()))
export const AsyncPage = (pageName: string) =>
load(
() =>
import(
/* webpackInclude: /\.page.tsx$/ */
/* webpackChunkName: "[request]" */
`@fider/pages/${pageName}`
)
)