Logo
The Performance optimization for Web applications page has loaded.

Performance optimization for Web applications

Context

Many web applications suffer from poor performance due to suboptimal implementation practices. Developers often create pages and components that load slowly, consume excessive resources, or respond sluggishly to user interactions. Meanwhile, backend API routes and data fetching may be inefficiently designed, creating bottlenecks that prevent even well-optimized frontends from performing well.

This recipe provides targeted optimization strategies for Next.js applications to address these issues.

Execution

To improve the performance of your web application and deliver a faster, more responsive user experience, review the following guidance. Using Next.JS? Review how to How to Optimize Next.js + Sitecore JSS.

Minimize File Size and Bundle Optimization

JavaScript bundle size directly impacts load time, parse time, and execution time of your application. Large bundles are the leading cause of poor Time to Interactive (TTI) and First Input Delay (FID) metrics. Studies show that each additional 100KB of JavaScript adds about 350ms to load time on average mobile devices.

Different frameworks provide different ways to reduce bundle sizes and optimize file loading:

  • Analyze your bundles to identify large dependencies - next/bundle-analyzer for Next.js or webpack-bundle-analyzer with ng build --stats-json for Angular.
  • Implement tree-shaking to eliminate unused code. Next.js provides automatic tree-shaking with webpack (enabled by default), while Angular has this enabled by default in Angular CLI.
  • Choose lighter alternatives for common libraries such as:
    • Moment.js (330KB) → date-fns (tree-shakeable)
    • Lodash (320KB) → lodash-es (tree-shakeable imports)
    • jQuery (87KB) → Native DOM APIs

Code Splitting with next/dynamic

Code splitting allows you to break your application into smaller chunks that load on demand, rather than forcing users to download your entire application upfront. This is especially important for large applications where users might only interact with a small portion of the total functionality.

Next.js's dynamic works seamlessly with SSR, provides more configuration options and automatically handles loading states.

Optimize Fonts

Web fonts can cause significant performance issues if not properly optimized, including render blocking and layout shifts. Next.js's font optimization:

  • Eliminates render blocking by asynchronously loading fonts
  • Prevents layout shifts with size adjustments
  • Optimizes font loading with preload links

Next.js's use next/font/foogle

Optimize Images with JSS NextImage Component

Images typically account for 50-80% of a webpage's total size. Unoptimized images cause poor Largest Contentful Paint (LCP) times and Cumulative Layout Shift (CLS) issues. In Sitecore JSS applications, properly optimizing Sitecore media library images is crucial for performance.

The Sitecore JSS SDK provides a specialized NextImage component that combines the performance benefits of Next.js image optimization with Sitecore's media handling capabilities:



On Next.js 16 and Content SDK 2.0, allow remote image hosts with remotePatterns in next.config. The deprecated images.domains setting is replaced. Content SDK 2.0 also adds a rewriteMediaUrls option in sitecore.config that rewrites production Edge media URLs to a custom host. The import above is from JSS. In the Content SDK the package is @sitecore-content-sdk/nextjs.

For more details on all available options and additional examples, refer to the complete documentation on configuring the NextImage component.

In complex components like carousels, sliders, or interactive galleries, using Next.js's next/link component directly can provide more flexibility while still maintaining performance benefits. This approach works well when you need custom interaction behavior while preserving Next.js's performance optimizations.

The following is an abbreviated example of a Horizontal Image Scroller component that uses next/link within a custom component:

The sample uses the JSS useSitecoreContext hook. In the Content SDK, components use the useSitecore hook, which replaces the deprecated withSitecore higher-order component in 2.0. The updatePage function is renamed setPage. See the upgrade guide for the React 19 changes.

Backend API Route Optimization

Inefficient API routes can create high Time to First Byte (TTFB), which directly impacts user experience. Optimizing API routes with proper caching and middleware can reduce response times by 40-80%.



Key techniques that should be reviewed include:

  • Adding proper caching headers
  • Processing operations in parallel with Promise.all
  • Implementing query parameters for filtering and pagination
  • Using middleware for common operations