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

perf: using-dynamic-imports #19204

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

TusharBhatt1
Copy link
Contributor

@TusharBhatt1 TusharBhatt1 commented Feb 9, 2025

What does this PR do?

This PR optimizes the /bookings page by adding dynamic imports to reduce bundle size by around 75% and improve performance.

  • Components imported dynamically , since they are rendered conditionally :
    1. Alert (@calcom/ui)
    2. DataTableWrapper (@calcom/features/data-table)
    3. BookingListItem (@components/booking/BookingListItem)
    4. EmptyScreen (@calcom/ui)
    5. WipeMyCalActionButton (@calcom/app-store/wipemycalother/components)
    6. FiltersContainer (@calcom/features/bookings/components/FiltersContainer)

  • Adds loading indicators for a smoother user experience while components load.

  • Disables SSR for specific components to save resources and focus on client-side rendering.

Dynamic import (import() in Next.js) loads modules only when needed, reducing the initial bundle size and improving performance by enabling code-splitting and lazy loading.

Before :

Screenshot (78)

After :

Screenshot (77)

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

Tested , works as expected.

Copy link

vercel bot commented Feb 9, 2025

@TusharBhatt1 is attempting to deploy a commit to the cal-staging Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@graphite-app graphite-app bot requested a review from a team February 9, 2025 09:57
@graphite-app graphite-app bot added the community Created by Linear-GitHub Sync label Feb 9, 2025
Copy link
Contributor

github-actions bot commented Feb 9, 2025

Hey there and thank you for opening this pull request! 👋🏼

We require pull request titles to follow the Conventional Commits specification and it looks like your proposed title needs to be adjusted.

Details:

No release type found in pull request title "perf : using dynamic imports". Add a prefix to indicate what kind of release this pull request corresponds to. For reference, see https://www.conventionalcommits.org/

Available types:
 - feat: A new feature
 - fix: A bug fix
 - docs: Documentation only changes
 - style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
 - refactor: A code change that neither fixes a bug nor adds a feature
 - perf: A code change that improves performance
 - test: Adding missing tests or correcting existing tests
 - build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
 - ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
 - chore: Other changes that don't modify src or test files
 - revert: Reverts a previous commit

@keithwillcode keithwillcode added the community-interns The team responsible for reviewing, testing and shipping low/medium community PRs label Feb 9, 2025
@dosubot dosubot bot added bookings area: bookings, availability, timezones, double booking performance area: performance, page load, slow, slow endpoints, loading screen, unresponsive labels Feb 9, 2025
@@ -157,8 +188,7 @@ function BookingsContent({ status }: BookingsProps) {
];
}, [user, status]);

const isEmpty = useMemo(() => !query.data?.pages[0]?.bookings.length, [query.data]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its computation is inexpensive and doesn't impact performance.
Introduces overhead by adding complexity and memory usage for tracking dependencies

Copy link

graphite-app bot commented Feb 9, 2025

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (02/09/25)

1 reviewer was added to this PR based on Keith Williams's automation.

"Add community label" took an action on this PR • (02/09/25)

1 label was added to this PR based on Keith Williams's automation.

@TusharBhatt1 TusharBhatt1 changed the title perf : using dynamic imports perf: using dynamic imports Feb 9, 2025
@TusharBhatt1 TusharBhatt1 changed the title perf: using dynamic imports perf: using-dynamic-imports Feb 9, 2025
@Udit-takkar Udit-takkar added this to the v5.0 milestone Feb 10, 2025

import useMeQuery from "@lib/hooks/useMeQuery";

import BookingListItem from "@components/booking/BookingListItem";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Booking List Item is core part of /bookings. I don't think we should dynamically load this as this component would be required 99% of the times

Copy link
Contributor Author

@TusharBhatt1 TusharBhatt1 Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood your concern , but there are cases when the user has no bookings or an error occurs, making this component unnecessary. Moreover, we’re dynamically importing it on the server, so it won’t impact client-side performance.
Also it's a heavy component , dynamic import will be beneficial

Copy link
Contributor

@eunjae-lee eunjae-lee Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too sure about this. As Udit said, it's 99% of the times required. Reducing the bundle size on the server side won't affect much.

In the end, we download the same size of the bundle on the client side (because we need them).

But with this approach, the client will have to make an asynchronous request to get these lazy components which is unnecessary separate requests with unncessary loaders.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood , but use cases where the component is required, the additional request overhead is negligible compared to the performance gains for the edge cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how this is a performance gain. DataTable and BookingListItem is an essential part of this page. Even if the size of page.js is reduced, we immediately load them in separate requests.

Screenshot 2025-02-10 at 10 42 55

On the other hand, I think it's indeed a performance win for WipeMyCalActionButton because it's clearly "optionally" used after a certain amount of time of the page load.

Copy link
Contributor Author

@TusharBhatt1 TusharBhatt1 Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your perspective, but the performance gain comes from deferring the loading of DataTable and BookingListItem (in times when no bookings , error , etc ) until they are actually needed, rather than including them in the initial page payload.

While it's true that these components are eventually required and are loaded in separate requests, the initial page load benefits because :

  • The size of page.js is reduced, which speeds up the Time to First Byte (TTFB) and the rendering of the critical parts of the page.
  • Components like BookingListItem and DataTable are dynamically loaded (only when required , not everytime during initial page load), so their loading happens in parallel with other resources instead of blocking the page render.

This approach is particularly effective because these components aren’t required for every use case (e.g., when there are no bookings or an error occurs). By deferring their loading, we reduce unnecessary server load and improve the perceived load time for the end user.

Let me know if I am missing something

Comment on lines +34 to +41
const DataTableWrapper = dynamic(
() => import("@calcom/features/data-table").then((mod) => mod.DataTableWrapper),
{
ssr: false,
loading: () => <SkeletonLoader />,
}
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood your concern , but there are cases when this won't be required like if the query fails, bookings are empty, or there are no upcoming bookings today.

Comment on lines 56 to 63
const FiltersContainer = dynamic(
() => import("@calcom/features/bookings/components/FiltersContainer").then((mod) => mod.FiltersContainer),
{
ssr: false,
loading: () => <Icon name="loader" className="animate-spin" />,
}
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen.Recording.2025-02-10.at.2.12.20.PM.mov

We should show a skeleton loader here instead of this loader

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's degrading the user experience by showing a loader when toggling filter components. It should already be there.

Copy link
Contributor Author

@TusharBhatt1 TusharBhatt1 Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood , but based on my analysis, dynamic import is the ideal approach for this scenario.
During the initial page load this is not required and there are high possibility that the user doesn’t require the component , such as when they don’t interact with the filter.

Additionally, we are already dynamically importing DateRangePicker , the user is still experiencing a very minimal delay. However, the performance gain from this approach is genuine and ensures we only load what’s necessary.

import dynamic from "next/dynamic";

export const DateRangePickerLazy = dynamic(() =>
  import("./DateRangePicker").then((mod) => mod.DatePickerWithRange)
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of the DateRangePicker, it's obvious to dynamically load it, because

1. Page Load > 2. User wants to filter > 3. User wants to filter by custom date range

It's at the 3rd step in the funnel. However, FiltersContainer is the 2nd step. So it's a different story.

Can we get the specific number of bundle size different by dynamically importing FiltersContainer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

290kB (transferred over network) and resource size is 1.3 MB

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen.Recording.2025-02-10.at.2.12.20.PM.mov
We should show a skeleton loader here instead of this loader

Done

Copy link
Contributor

@hbjORbj hbjORbj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type checks are failing

@github-actions github-actions bot marked this pull request as draft February 11, 2025 10:37
Copy link
Contributor

github-actions bot commented Feb 11, 2025

E2E results are ready!

@TusharBhatt1
Copy link
Contributor Author

Type checks are failing

Fixed (branch update was required)

@TusharBhatt1 TusharBhatt1 marked this pull request as ready for review February 11, 2025 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bookings area: bookings, availability, timezones, double booking community Created by Linear-GitHub Sync community-interns The team responsible for reviewing, testing and shipping low/medium community PRs performance area: performance, page load, slow, slow endpoints, loading screen, unresponsive ready-for-e2e
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants