Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed lazy usage with Suspense and Error Boundary together #521

Merged
merged 24 commits into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
03eb6d9
Fixed lazy usage with Suspense and Error Boundary together
kamikazePT Feb 4, 2020
d920619
Typo
kamikazePT Feb 4, 2020
c50669e
v1.0.0-fork
kamikazePT Feb 4, 2020
f85fcaa
accidental push
kamikazePT Feb 4, 2020
790ad66
Condition was reversed
kamikazePT Feb 4, 2020
cc0f646
Fixed Suspense with full dynamic import after fulfilled promise
kamikazePT Feb 4, 2020
addb38f
Added unit test
kamikazePT Feb 6, 2020
6ab1221
cached promise
kamikazePT Feb 17, 2020
3e30e69
added tests for multiple elements of same async component
kamikazePT Feb 22, 2020
11d34e8
renamed unit test
kamikazePT Feb 22, 2020
dacc790
added retryable error boundary
kamikazePT Feb 22, 2020
1ca1815
reworked tests
kamikazePT Feb 26, 2020
f9ef9c7
Retrying working for lazy and loadable
kamikazePT Feb 26, 2020
cf9763d
linter should run on pre-commit... :/
kamikazePT Feb 26, 2020
ac57625
upped max bundle size
kamikazePT Feb 28, 2020
df7a4d3
fix: Fixed suspense tests and fixed un-throwable pending promises whe…
kamikazePT Apr 14, 2020
c888a5b
refactor: removed unnecessary wait in test
kamikazePT Apr 14, 2020
97158e2
test: fixed test regarding nested suspense
kamikazePT Apr 14, 2020
4d41215
test: fixed fucked up assertion
kamikazePT Apr 14, 2020
df79086
fix: fixed weird corner case on error boundary not being reached
kamikazePT Apr 15, 2020
8d874f9
chore: merge fix
kamikazePT Aug 4, 2020
d0d4c39
feat: removed proxy
kamikazePT Aug 7, 2020
a15d07a
fix: removed unnecessary code
kamikazePT Sep 4, 2020
72b28dd
fix: fixed unnecesssary stack of callbacks
kamikazePT Sep 4, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
{
"path": "./packages/component/dist/loadable.esm.js",
"maxSize": "3.5 kB"
"maxSize": "3.6 kB"
}
],
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions packages/component/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"dist/loadable.cjs.js": {
"bundled": 13358,
"minified": 6507,
"gzipped": 2356
},
"dist/loadable.esm.js": {
"bundled": 12975,
"minified": 6198,
"gzipped": 2286,
"treeshaked": {
"rollup": {
"code": 259,
"import_statements": 259
},
"webpack": {
"code": 5302
}
}
}
}
71 changes: 37 additions & 34 deletions packages/component/src/createLoadable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-use-before-define, react/no-multi-comp, no-underscore-dangle */
import React from 'react'
import { invariant } from './util'
import { invariant, statusAware, STATUS_REJECTED } from './util'
import Context from './Context'

function resolveConstructor(ctor) {
Expand Down Expand Up @@ -90,7 +90,17 @@ function createLoadable({ resolve = identity, render, onLoad }) {
componentDidMount() {
this.mounted = true

if (this.state.loading) {
const cachedPromise = this.getCache()

if (cachedPromise && cachedPromise.status === STATUS_REJECTED) {
this.setCache()
this.setState({
error: undefined,
loading: true,
})

if (!options.suspense) this.loadAsync()
theKashey marked this conversation as resolved.
Show resolved Hide resolved
} else if (this.state.loading) {
this.loadAsync()
} else if (!this.state.error) {
this.triggerOnLoad()
Expand All @@ -100,7 +110,6 @@ function createLoadable({ resolve = identity, render, onLoad }) {
componentDidUpdate(prevProps, prevState) {
// Component is reloaded if the cacheKey has changed
if (prevState.cacheKey !== this.state.cacheKey) {
this.promise = null
this.loadAsync()
}
}
Expand Down Expand Up @@ -149,29 +158,28 @@ function createLoadable({ resolve = identity, render, onLoad }) {
}

loadAsync() {
if (!this.promise) {
const { __chunkExtractor, forwardedRef, ...props } = this.props
this.promise = ctor
.requireAsync(props)
.then(loadedModule => {
const result = resolve(loadedModule, { Loadable })
if (options.suspense) {
this.setCache(result)
}
this.safeSetState(
{
result: resolve(loadedModule, { Loadable }),
loading: false,
},
() => this.triggerOnLoad(),
)
})
.catch(error => {
this.safeSetState({ error, loading: false })
})
}
const { __chunkExtractor, forwardedRef, ...props } = this.props

let promise = this.getCache() || statusAware(ctor.requireAsync(props))

this.setCache(promise)

promise = promise
.then(loadedModule => {
const result = resolve(loadedModule, { Loadable })
this.safeSetState(
{
result,
loading: false,
},
() => this.triggerOnLoad(),
)
})
.catch(error => {
this.safeSetState({ error, loading: false })
})

return this.promise
return promise
}

render() {
Expand All @@ -184,15 +192,10 @@ function createLoadable({ resolve = identity, render, onLoad }) {
const { error, loading, result } = this.state

if (options.suspense) {
const cachedResult = this.getCache()
if (!cachedResult) throw this.loadAsync()
return render({
loading: false,
fallback: null,
result: cachedResult,
options,
props: { ...props, ref: forwardedRef },
})
const cachedPromise = this.getCache()
if (!cachedPromise) {
throw this.loadAsync()
}
}

if (error) {
Expand Down
Loading