Skip to content

Refactor/lazy load page components#222

Open
s0merset wants to merge 3 commits intobettergovph:mainfrom
s0merset:refactor/lazy-load-page-components
Open

Refactor/lazy load page components#222
s0merset wants to merge 3 commits intobettergovph:mainfrom
s0merset:refactor/lazy-load-page-components

Conversation

@s0merset
Copy link

Summary

This PR implements lazy loading for page components, addressing the performance issue described in #212 . The goal is to optimize initial load time by reducing the main bundle size, while still supporting smooth navigation between routes.

Changes

  • Updated App.tsx to use React.lazy and Suspense for route-based code splitting.
  • Ensured fallback UI is displayed while components are being loaded.
  • Updated .gitignore to exclude build artifacts (dist/, dist-main/, dist-lazy) and keep the repository clean.

Performance Comparison

Non-Lazy Load:
759CEC8B-12AB-4195-A2A4-D0AA5EF4F910

Lazy Load:
B1E839F4-D564-4E57-8108-EF987D42C1B5

Observations

  • Reduced bundle size: Lazy loading cuts down the initial payload, improving the first-page load experience.
  • Increased requests: Lazy loading increases the number of requests since chunks are loaded on demand, but each request is smaller.
  • Navigation: Once a route has been visited, caching ensures subsequent navigation is instant.

@codegino
Copy link
Collaborator

@frb21 are the screenshots from production or dev setup?

@s0merset
Copy link
Author

@frb21 are the screenshots from production or dev setup?

The screenshots are from my local dev setup sir...

Copy link
Member

@KishonShrill KishonShrill left a comment

Choose a reason for hiding this comment

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

Hmmmm, I don't know about using this method. It might be too huge of a change that might break other else's code. Wouldn't be:

const <component> = lazy(() => import('path-to-component'));

be better? Then wrapping just one <Suspense> over the whole return function than wrapping every <Route>? I wanna hear your thoughts on this.

@s0merset
Copy link
Author

Hmmmm, I don't know about using this method. It might be too huge of a change that might break other else's code. Wouldn't be:

const <component> = lazy(() => import('path-to-component'));

be better? Then wrapping just one <Suspense> over the whole return function than wrapping every <Route>? I wanna hear your thoughts on this.

Hi po, I've updated the PR based on your feedback about the lazy loading approach.

Changes include:

  • Moved all lazy component definitions to the top of the file
  • Replaced individual createLazyRoute() calls with direct lazy components
  • Used a single <Suspense> wrapper around the entire <Routes>

@codegino
Copy link
Collaborator

@frb21 are the screenshots from production or dev setup?

The screenshots are from my local dev setup sir...

Would be better if we can see the prod build performance improvement.

Also, it might be better to measure first the components that are really problematic. If they need optimization, then we do proceed.

I'm not a fan of premature optimization, but I don't want to gate keep this very good contribution. I'm leaving to other maintainers.

@niculistana
Copy link
Collaborator

Per @codegino could you please deploy to vercel and upload the link here? We see the bundle size diff pero would also like to see if how big of a difference it is with initial loading time.

Either way I'm impartial to this improvement. Since majority of the website is static content, we could look into prerendering?

@s0merset
Copy link
Author

s0merset commented Sep 25, 2025

Per @codegino could you please deploy to vercel and upload the link here? We see the bundle size diff pero would also like to see if how big of a difference it is with initial loading time.

Either way I'm impartial to this improvement. Since majority of the website is static content, we could look into prerendering?

hello po @niculistana @codegino, is the link for the deployed build on Vercel already available?

@niculistana
Copy link
Collaborator

@frb21 familiar po kayo sa vercel? Pwede po kayong gumawa ng account tapos create ka ng project tapos connect nyo yung fork nyo.

For example ito yung fork ko: https://github.com/adobocorp/bettergov

Tapos connect ko sa vercel. After deploy may custom link kayo: https://bettergov-adobo.vercel.app/.

@s0merset
Copy link
Author

@frb21 familiar po kayo sa vercel? Pwede po kayong gumawa ng account tapos create ka ng project tapos connect nyo yung fork nyo.

For example ito yung fork ko: https://github.com/adobocorp/bettergov

Tapos connect ko sa vercel. After deploy may custom link kayo: https://bettergov-adobo.vercel.app/.

ay okay po, salamat po sir!

@s0merset
Copy link
Author

Hello po @niculistana @codegino, I tried to compare the separate versions after deployment. I navigated to the different components sa two versions and here's what I have found:
Non-lazy:
64A769D0-72D8-43C6-8921-D05DD8C11527

Lazy:
DE546E52-07D3-4B42-B8BD-7007EBCEA212

@niculistana
Copy link
Collaborator

Salamat @frb21 paki send po url dito from vercel

@s0merset
Copy link
Author

@niculistana
Copy link
Collaborator

niculistana commented Sep 25, 2025

Does main have your lazy-load changes? If so can you verify the following report?

Ran pagespeed against main and prod and I don't really see the a big difference because our pages contain bundle sizes that aren't too heavy.

Main: https://pagespeed.web.dev/analysis/https-bettergov-main-vercel-app/wuw4y9oewf?form_factor=mobile

Prod: https://pagespeed.web.dev/analysis/https-bettergov-ph/oweys6ywz0?form_factor=mobile

Not sure if this is the best recommendation but for now why not run a bundle analyzer and see which bundle is the heaviest and lazy-load just that route? Eager loading is fine for a majority of small bundles.

https://github.com/nonzzz/vite-bundle-analyzer

@angelofallars angelofallars added mid priority This project is needed but can take its time. enhancement New feature or request labels Oct 16, 2025
@j4ckofalltrades j4ckofalltrades added the stale Has not been worked on in a while label Nov 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request mid priority This project is needed but can take its time. stale Has not been worked on in a while

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants