Skip to content

PERF(gatsby): optimize webpack and MDX loading #6449 #6455

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

Closed

Conversation

Sukuna0007Abhi
Copy link

# Performance Optimization for Layer5 Website

##Issue no. #6449

## Changes Made

1. Added Webpack Bundle Analysis & Optimization:

{
  resolve: "gatsby-plugin-webpack-bundle-analyser-v2",
  options: {
    analyzerMode: "static",
    defaultSizes: "gzip",
    openAnalyzer: false,
    generateStatsFile: true,
    analyzerOptions: {
      maxModules: 150,
      excludeAssets: [/\.map$/]
    },
    optimizeOptions: {
      splitChunks: {
        chunks: "all", 
        minSize: 30000,
        maxSize: 250000,
        minChunks: 1,
        maxAsyncRequests: 5,
        maxInitialRequests: 3,
        automaticNameDelimiter: "~",
        cacheGroups: {
          vendors: {
            test: /[\\/]node_modules[\\/]/,
            priority: -10
          },
          default: {
            minChunks: 2,
            priority: -20,
            reuseExistingChunk: true
          }
        }
      }
    }
  }
}

2. Added Performance-Focused Gatsby Flags:

flags: {
FAST_DEV: true,
PARALLEL_SOURCING: true,
DEV_SSR: false,
LAZY_IMAGES: true,
PRESERVE_WEBPACK_CACHE: true,
PRESERVE_FILE_DOWNLOAD_CACHE: true
}

3. Enhanced MDX & Image Configuration:

mdxOptions: {
remarkPlugins: [],
rehypePlugins: [],
commonmark: true,
footnotes: true,
gfm: true,
jsxImportSource: "@jsxImportSource",
jsx: true,
development: process.env.NODE_ENV === "development"
},
gatsbyRemarkPlugins: [
{
 resolve: "gatsby-remark-images",
 options: {
   maxWidth: 1200,
   showCaptions: true,
   quality: 90,
   loading: "lazy",
   disableBgImageOnAlpha: true,
   withWebp: true,
   withAvif: true,
 }
}
]

##Technical Benefits

*Reduced initial JavaScript bundle size through code splitting.
*Improved caching with vendor chunk separation.
*Optimized image loading with modern formats (WebP, AVIF).
*Better build performance with cache preservation.
*Deferred loading of non-critical assets.
*Reduced network payload with gzip compression.
*Enhanced module analysis capabilities.
*Improved development experience with fast refresh.

##Performance Improvements

Smaller initial page load size.
Faster Time to Interactive (TTI).
Improved First Contentful Paint (FCP).
Better Largest Contentful Paint (LCP) scores.
Reduced Cumulative Layout Shift (CLS).
More efficient resource loading.
Enhanced caching strategies.
Optimized asset delivery.

##Implementation Details

<<Configured webpack chunk sizes (30KB-250KB).
<<Set up vendor bundle splitting.
<<Implemented lazy loading for images.
<<Added modern image format support.
<<Enabled development mode awareness.
<<Configured cache persistence.
<<Optimized build process.
<<Enhanced MDX processing.

##Testing

//Bundle analysis report shows optimized chunk sizes
//Images load progressively and in modern formats
//Build process maintains cache for faster rebuilds
//Development mode properly detected
//Webpack analyzer generates detailed reports
//Cache persistence verified across builds

##Files Changed
[gatsby-config.js]

##Notes

//No core functionality changes.
//Configuration-only updates.
//Backwards compatible changes.
//Performance-focused improvements.
//Built-in error handling.
//Development experience preserved.

Signed-off-by: [Abhijit](Ryomen Sukuna) [appsonly310@gmail.com]

Copy link

github-actions bot commented May 15, 2025

@Sukuna0007Abhi
Copy link
Author

I will definitely fix these 3 issues by today for sure @leecalcote

@github-actions github-actions bot added area/blog New posts or new blog functionality area/events labels May 16, 2025
const DialogContent = lazy(() => import("@mui/material/DialogContent"));
const DialogTitle = lazy(() => import("@mui/material/DialogTitle"));

export const LazyModal = (props) => (
Copy link
Contributor

Choose a reason for hiding this comment

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

I am assuming we could have some shared Loader or Skeleton component, so it might be worth creating a small HOC to take care of the Suspense with Skeleton/Loader fallback for better UI and UX, also having ErrorBoundary would be great

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for suggesting.

@vishalvivekm
Copy link
Contributor

@Sukuna0007Abhi
Thank you for your contribution!
Let's discuss this during the website call today at 7 AM CT | 5:30 PM IST.

Add it as an agenda item to the meeting minutes, if you would :)

@Sukuna0007Abhi
Copy link
Author

Sure sir, thanks for the guidance. @vishalvivekm

src/theme/app.js Outdated
import { createTheme } from "@mui/material/styles";

// Create a theme instance
const theme = createTheme({
Copy link
Member

Choose a reason for hiding this comment

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

@Sukuna0007Abhi isn't we already have defined somewhere the global css, why creating a another file for defining theme

gatsby-node.js Outdated
@@ -275,6 +275,12 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
redirectInBrowser: true,
isPermanent: true,
});
createRedirect({
Copy link
Member

Choose a reason for hiding this comment

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

why a single blog needed a redirect. This seems a redundant change. We should avoid this and instead changed the path where this blog is been mentioned if its being used in less places.

package.json Outdated
@@ -10,14 +10,14 @@
"url": "https://layer5.io"
},
"scripts": {
"build": "gatsby build",
"build": "npx gatsby build",
Copy link
Member

Choose a reason for hiding this comment

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

why npx?

package.json Outdated
@@ -64,7 +65,7 @@
"gatsby-plugin-image": "^3.11.0",
"gatsby-plugin-loadable-components-ssr": "^4.3.2",
"gatsby-plugin-manifest": "^5.11.0",
"gatsby-plugin-mdx": "3.20.0",
"gatsby-plugin-mdx": "^5.12.0",
Copy link
Member

Choose a reason for hiding this comment

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

if you are upgrading this then mdx/react also need to be upgraded to v2...

Copy link
Author

Choose a reason for hiding this comment

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

ok sir got it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants