How We Reduced Unused JavaScript by 49KB with Smart Dynamic Imports

Struggling with bloated JavaScript in your WordPress site powered by Next.js? We were too — until we optimised our site with dynamic imports and cut down 49KB of unused code.

How We Reduced Unused JavaScript by 49KB with Smart Dynamic Imports
How We Reduced Unused JavaScript by 49KB with Smart Dynamic Imports

This article shows you exactly how we did it, what changed, and how you can do the same to speed up your site and improve Core Web Vitals.

Why Unused JavaScript Is a Problem for WordPress Performance

Large bundles of unused JavaScript often come from third-party tools, search libraries, or visual components. In a WordPress + Next.js hybrid site, anything added globally — like search tools or forms — may end up in the _app.js file and load on every page. This slows down initial load times, especially on mobile, and bloats your Time to Interactive (TTI).

We discovered this when running a build analysis of meshwithus.com.au, our Gold Coast-based web development agency site. The results were eye-opening:

  • First Load JS: 309KB
  • _app.js bundle: 194KB
  • Algolia InstantSearch code included on non-blog pages

Tools We Used to Audit JavaScript Bundle Size

To identify what was bloating our bundles, we enabled the official Next.js Bundle Analyzer plugin:

// next.config.js
const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
});

module.exports = withBundleAnalyzer({
  trailingSlash: true,
  reactStrictMode: true,
  images: {
    unoptimized: true,
    remotePatterns: [
      {
        hostname: require('@faustwp/core').getWpHostname(),
      },
    ],
  },
});

We then triggered a build with analysis turned on:

$env:NEXT_FORCE_WEBPACK="true"; $env:ANALYZE="true"; pnpm build

This generated a client.html visual report and revealed that components like Algolia, SearchBox, and our FAQAccordion were bundled into _app.js.

What We Moved to Dynamic Imports

The two biggest components we dynamically split were:

  • RelatedInsights (Algolia InstantSearch)
  • FAQAccordion (Only used on some pages)

Here’s how we changed the import:

import dynamic from 'next/dynamic';

const RelatedInsights = dynamic(() => import('components/RelatedInsights'), {
  ssr: false,
  loading: () => <div>Loading related insights…</div>,
});

const FAQAccordion = dynamic(() => import('components/FAQAccordion'), {
  ssr: false,
  loading: () => <div>Loading FAQs…</div>,
});

How We Controlled When It Loads

We only render <RelatedInsights /> on blog pages using this conditional check in Singular.tsx:

const isBlogPost = router.asPath.startsWith('/blog/');

{isBlogPost &amp;&amp; (
  &lt;RelatedInsights objectID={`\${page.databaseId}-0`} /&gt;
)}

This prevents unnecessary scripts and search logic from loading on all service or project pages — especially beneficial for mobile users and SEO speed scores.

Before and After: Measurable Performance Gains

MetricBeforeAfterChange
_app.js size194 KB146 KB▼ 48 KB
First Load JS309 KB260 KB▼ 49 KB
Non-blog pagesLoaded AlgoliaAlgolia deferred
FAQAccordionBundled globallyDynamically loaded

Best Practices for Dynamic Imports in Next.js

  • Use dynamic() for components that are route-specific
  • Disable SSR (ssr: false) for client-only tools
  • Use loading props to maintain UX while the chunk loads
  • Move large search libraries and FAQ builders out of the shared bundle
READ  5 Essential Features Every Gold Coast Business Website Needs in 2025

Want to see another example of how we optimise code delivery? Check out our article on Shopify site speed optimisation.

Do You Really Need Algolia on Every Page?

If your search bar is only used in a specific section of the site, don’t load it globally. The same goes for animations, charts, or visual libraries. Only load them when needed and allow the rest of the page to render cleanly.

Need Help with WordPress Performance?

If you’re working on a WordPress site built with React, headless frameworks, or anything beyond a standard theme, performance can get tricky. We’re a Gold Coast-based development team specialising in WordPress speed optimisation and advanced builds using Next.js and custom APIs.

We also offer full WordPress maintenance and monitoring services so your optimisations don’t get lost in future updates.

Frequently Asked Questions

What is dynamic import in Next.js?

It’s a way to load JavaScript components only when needed. Next.js supports this through the dynamic() function.

How do I reduce JavaScript bundle size in WordPress with React?

Use code splitting, move third-party libraries to client-only routes, and disable global imports where possible.

Does dynamic import help Core Web Vitals?

Yes — it improves First Input Delay (FID) and Total Blocking Time (TBT) by reducing script evaluation.

Can I defer Algolia InstantSearch in a WordPress site?

Yes, using dynamic import and conditional rendering for pages like /blog.

How can I optimise _app.js in Next.js?

Remove route-specific code, move feature-rich components to dynamic imports, and limit global state libraries.

Do I need Turbopack for this?

No. All this works with Webpack builds using Next.js 15.x with bundle-analyzer.

Where can I learn more about your optimisation services?

What’s the best hosting provider for WordPress performance?

We recommend Pressable for fast, reliable WordPress hosting with CDN and built-in security.

Can this help with Shopify performance too?

Yes — many of the same techniques apply to headless Shopify setups, especially when using React frameworks.

Do you work with clients outside the Gold Coast?

Absolutely. While based in the Gold Coast, we work with businesses across Australia and internationally.

Let’s Boost Your Site Speed Today

If your WordPress or Shopify site is carrying extra baggage, let’s clean it up. We’ll help you reduce unused JavaScript, speed up page loads, and get those Lighthouse scores where they should be.

Request a performance audit today.

Loading related insights…

Ready to build a project?

Let's create something amazing together.

Let's Mesh!