-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(ui): Keep stable references to lazy components #69740
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
Conversation
In concurrent mode a spinner is sometimes shown on route change even though the component has already been loaded. We should try to keep stable references to `lazy(() => import('./whatever'));` to benefit from the caching react lazy does and avoid the tick from .then() that is needed to resolve a fresh promise, even if webpack has cached the result already. https://react.dev/reference/react/lazy for an example spinner in concurrent mode go to the subscription page https://sentry.sentry.io/settings/billing/overview/ and flick between the tabs
@@ -39,11 +40,12 @@ const SafeLazyLoad = errorHandler(LazyLoad); | |||
export function makeLazyloadComponent<C extends React.ComponentType<any>>( | |||
resolve: () => Promise<{default: C}> | |||
) { | |||
const LazyComponent = lazy<C>(() => retryableImport(resolve)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pulling this outside the RouteLazyLoad ensures that as long as makeLazyloadComponent isn't called multiple times this should be stable
...props | ||
}: Props<C>) { | ||
const LazyComponent = useMemo( | ||
() => lazy<C>(() => retryableImport(component)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is what i'm trying to avoid, a new lazy() every time the component is mounted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const LazyComponent = lazy(() => import('./myComponent'))
makes the most sense.
Co-authored-by: Ryan Albrecht <[email protected]>
final follow up to #69740 prefer stable references to lazy()
Suspect IssuesThis pull request was deployed and Sentry observed the following issues:
Did you find this useful? React with a 👍 or 👎 |
This pr pulls the
lazy()
call out of the render path.In concurrent mode, a spinner is sometimes shown on route change even though the component has already been loaded. We should try to keep stable references to
lazy(() => import('./whatever'));
to benefit from the caching react lazy does and avoid the tick from .then() that is needed to resolve a fresh promise, even if webpack has cached the result already.From the docs
example: go to the subscription page https://sentry.sentry.io/settings/billing/overview/ and flick between the tabs
before - spinner every time you switch tabs even after the component is loaded
CleanShot.2024-04-25.at.20.20.20.mp4
after - spinner or flicker only when the bundle is loaded the first time. this could be prefetched
CleanShot.2024-04-25.at.20.15.51.mp4
part of https://github.com/getsentry/frontend-tsc/issues/63