Sunday, May 19, 2024
HomeC#Optimize Subsequent.js App Bundle and Enhance Its Efficiency

Optimize Subsequent.js App Bundle and Enhance Its Efficiency

On this article, we are going to discover ways to optimize the Subsequent.js app (hyperlink to the app) by lowering the bundle dimension by 43% and enhance the rating to 73 from 36 in Google’s PageSpeed Insights.

Let’s begin with analyzing the manufacturing construct of Subsequent.js. After we execute npm run construct, Subsequent.js offers us a statistic of the manufacturing construct. It specifies the dimensions of pages and chunks that might be delivered to the browser.

Subsequent.js creates information which are widespread and might be shared throughout pages. These information are referred to as the First Load JS. The First Load JS information include framework code and the code that’s utilized by a number of pages.

If the element reuses greater than 50% of the information, then Subsequent.js consists of that element within the First Load JS. We can’t change this habits, however we will optimize our code.

Following is the output of the npm run construct command and Google PageSpeed Insights rating. As you may see, the sizes of the information are in purple and yellow. Our aim is to make them inexperienced.



before-optimization-google-page-insightsLet’s go.

Analyze First Load JS

We begin by analyzing and figuring out the bundles included within the First Load JS. To be able to discover that, we have to set up the next dev dependencies.

npm set up –save-dev @subsequent/bundle-analyzer cross-env

After set up, add the next code within the bundle.json underneath scripts.

"scripts": {
    "analyze": "cross-env ANALYZE=true subsequent construct",
    "analyze:server": "cross-env BUNDLE_ANALYZE=server subsequent construct",
    "analyze:browser": "cross-env BUNDLE_ANALYZE=browser subsequent construct"

Add the next code in subsequent.config.js. For those who don’t have this file, then create it within the root listing of your challenge.

const withBundleAnalyzer = require('@subsequent/bundle-analyzer')({
    enabled: course of.env.ANALYZE === 'true'
module.exports = withBundleAnalyzer({
    env: {
        NEXT_PUBLIC_ENV: 'PRODUCTION', //your subsequent configs goes right here

Subsequent, execute this command npm run analyze, which can open two new tabs in your browser with graphs. Concentrate on the shopper.html graph.

Then, exchange the bigger libraries with smaller, equal ones and take away the irrelevant libraries. To be able to exchange any library, go to,  see the dimensions of your library, and examine whether or not you might have any alternate options with smaller sizes. In any other case, attempt to write your individual customized code. On this demo:

  • We eliminated the js-cookie library, which was used to learn and write cookies, and changed it with customized code.
  • We changed the framer-motion library with react-transition-group as a result of it was 10 instances smaller.
  • As an alternative of utilizing react-gpt, which is used to show Google adverts, we created our personal customized advert element (extra particulars following).
  • We moved the momentjs file to the server aspect from the shopper, as the dimensions of momentjs is large. By doing this, we will both ship the formatted date from the API or format the date on the server aspect, as follows.
export async perform getServerSideProps(context) {
  const second = (await import('second')).default(); //default methodology is to entry default export
  return {
    date: second.format('dddd D MMMM YYYY'),

Now this date might be despatched as a prop to the element, which can be utilized (getServerSideProps can solely be utilized in pages not inside parts). Now, momentjs won’t be despatched to the browser.

Dynamic imports

Parts that aren’t seen within the preliminary load of the web page and parts which are displayed primarily based on sure situations needs to be imported dynamically as an alternative of usually. This ensures that these parts are despatched to the browser solely when they’re wanted. Confer with the next code instance.

import dynamic from 'subsequent/dynamic'

const Modal = dynamic(() => import('../parts/header'));

export default perform Residence() {
  return (
    {showModal && <Modal />}

Open the Community tab. When the situation is fulfilled, you’ll see {that a} new community request is made to fetch the dynamic element (click on the button to open a modal).

Lazy load pictures utilizing subsequent/picture

Subsequent.js has a built-in element named subsequent/picture. It hundreds pictures solely when they’re within the viewport.

import Picture from "subsequent/picture";

const Index = () => {
  return (
        Exterior domains have to be configured in <Code>subsequent.config.js</Code> utilizing
        the <Code>domains</Code> property.
        alt="Subsequent.js brand"

Lazy load Google Advertisements

The Google Advertisements script normally blocks the primary thread, so we’re going to load the script eight seconds after the web page load. Initially, we had been utilizing the react-gpt bundle to load the Google Advertisements script. We’re going to exchange that with our optimized customized code.

For this, we’ve got created a customized hook to inject any scripts. It’ll return the standing relying on the state of the injection when the Google Advertisements script is injected. As soon as it returns prepared, we run the Advertisements code.

import React from "react";

const useScript = (src, delay = null) => {
  const [status, setStatus] = React.useState(src ? "loading" : "idle");

  React.useEffect(() => {
    if (!src) {
      return "idle";

    let script = doc.querySelector(`script[src="${src}"]`);
    let timeout = null;

    if (!script) {
      if (delay) {
        timeout = setTimeout(() => {
          // Add occasion listener after the script is added
          script.addEventListener("load", setStateStatus);
          script.addEventListener("error", setStateStatus);
        }, delay);
      } else {
    } else {

    const setStateStatus = (occasion) => {
      setStatus(occasion.sort === "load" ? "prepared" : "error");

    //code to inject script
    perform injectScript() {
      script = doc.createElement("script");
      script.src = src;
      script.async = true;
      script.setAttribute("data-status", "loading");

      const setDataStatus = (occasion) => {
          occasion.sort === "load" ? "prepared" : "error"

      script.addEventListener("load", setDataStatus);
      script.addEventListener("error", setDataStatus);

    if (script) {
      //script might be be undefined accessible when its delayed therefore examine it earlier than including listener
      script.addEventListener("load", setStateStatus);
      script.addEventListener("error", setStateStatus);

    return () => {
      if (script) {
        script.removeEventListener("load", setStateStatus);
        script.removeEventListener("error", setStateStatus);
      if (timeout) {
  }, [src]);

  return standing;

export default useScript;

Use this hook in pages/index.js.

const MyApp = () => {
  const standing = useScript(script, 8000);
  return (
      <p>Dad or mum standing: {standing}</p>
      {standing === ‘idle’ && <Advertisements />}

If you wish to create a customized advert element like this, then observe this text.

You can too lazy load different scripts just like the Google Analytics script.

Particular imports

In case you are utilizing libraries like lodash and date-fn, we will simply cut back bundle dimension by simply importing particular features as an alternative of a full import, like this.

Import _get from ‘lodash’

Import _get from ‘lodash/get’

We will optimize the utilization of a number of different libraries. For extra particulars, try this text. Don’t overlook to take away unused imports from the challenge.

Optimize subsequent/hyperlink

In case you are utilizing subsequent/hyperlink in your challenge, add the prefetch prop to it and set it to false. Subsequent.js by default prefetches the pages whose hyperlinks are within the viewport. Let’s say you might have a header with two hyperlinks, ‘/house’ and ‘/about’. Although the customers are on the house web page, property of the about web page will even be loaded as a result of the about hyperlink may be seen within the viewport.

When prefetch is ready to false, prefetching will nonetheless happen however solely when the hyperlink is hovered over.

	<Hyperlink href="" prefetch={false}>
		<a>About Us</a>
	<Hyperlink href="" prefetch={false}>>
		<a>Weblog Submit</a>

Optimize fonts

After we use icon libraries like font-awesome, we solely use a most of 15 icons, however the full library is loaded. The issue is that it’s render-blocking sources. So, as an alternative of loading the whole library, you may simply obtain the required icons as SVG information and use them. You can too lazy load these SVG pictures.

You can too use font-display: swap; to your fonts as a result of it doesn’t block the rendering. The font face is as an alternative given an infinite swap interval and a minimal block interval.

@font-face {
  font-family: ExampleFont;
  src: url(/path/to/fonts/examplefont.woff) format('woff'),
       url(/path/to/fonts/examplefont.eot) format('eot');
  font-weight: 400;
  font-style: regular;
  font-display: swap;

In case you are utilizing Google Fonts immediately from the hyperlink, then obtain the fonts and self-host them.

Lazy load React parts (non-obligatory)

We will additionally load a element solely when it’s within the viewport utilizing the react-lazyload library, which additionally helps SSR. We will present offset, too, so customers might be unaware of this lazy-loading habits.

import LazyLoad from 'react-lazyload';

<LazyLoad offset={100}>
    <Footer />

Exclude large libraries from bundle

As I mentioned earlier, libraries are added to First Load JS. In our challenge, we’re utilizing the Syncfusion Charts library on a number of pages, so the Syncfusion charts library was bundled into the primary load.

Pages that weren’t utilizing Syncfusion charts had been additionally loading the Syncfusion charts library, because it was added into to First Load JS or the primary bundle. To be able to optimize it, we adopted this superior article by Robert S on find out how to exclude large libraries.

Ultimate outcomes


*We’re at present optimizing the final purple web page



If you wish to persuade your supervisor or shopper to take away some sections from the web site or load them later, then go to

Enter the variety of seconds you need your internet app to take to load. Then, choose the 3G community (as a result of Google PageSpeed Insights makes use of 3G velocity to check). Press Calculate.

You’ll get your dimension price range (complete dimension of sources that needs to be despatched to the browser) that may enable your internet app to load within the specified variety of seconds.

Merely begin your challenge from this step.


In order for you good efficiency and a greater Google PageSpeed Insights rating, then ship fewer sources to the browser.

Earlier than including any bundle, examine the dimensions in You possibly can’t preserve a number of, advanced options and have nice efficiency on the identical time.

I hope you now have a good suggestion of find out how to optimize the Subsequent.js app to enhance its rating on Google PageSpeed Insights. Use these strategies to optimize your previous web site to make it quicker.

Thanks for studying!

Syncfusion Important JS 2 is the one suite you’ll ever have to construct an app. It accommodates over 65 high-performance, light-weight, modular, and responsive UI parts in a single bundle. Obtain a free trial to judge the controls right this moment.

When you’ve got any questions or feedback, you may contact us by means of our assist boardsassist portal, or suggestions portal. We’re at all times glad to help you!

Associated blogs



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments