Next.js: Client-side exception and 404 error for static chunks in production, working fine in development

I am facing a peculiar issue with my Next.js application. The problem appears only in the production environment. In the development environment, everything works fine, but in production, I encounter a client-side exception and 404 errors for some static chunks.

Error Details

In the production environment, I get the following error:
Application error: a client-side exception has occurred (see the browser console for more information).

The browser console shows:

GET https://starblogs-trial.in/_next/static/chunks/app/posts/%5Bslug%5D/page-9f2de8d9ba04381a.js 404 (Not Found)
ChunkLoadError: Loading chunk 333 failed.
(error: https://starblogs-trial.in/_next/static/chunks/app/posts/%5Bslug%5D/page-9f2de8d9ba04381a.js)
at l.f.j (webpack-84ef18324f9abbfd.js:1:3428)
at webpack-84ef18324f9abbfd.js:1:1427
at Array.reduce ()
at l.e (webpack-84ef18324f9abbfd.js:1:1393)
at self.next_chunk_load (596-6aee3da8cc55e0f0.js:1:5775)
at 596-6aee3da8cc55e0f0.js:9:5750
at 596-6aee3da8cc55e0f0.js:9:5973
at t (596-6aee3da8cc55e0f0.js:9:6176)

Description

In the development environment, everything works perfectly. When I click on a blog post, the data is fetched from the database and displayed correctly. In production, the server logs indicate that the data is fetched correctly, but when trying to access certain properties, the application crashes.

Data structure

data looks like:

{
  "id": "sdfsdfsdfsd3623423432r5b327quqz8",
  "createdAt": "2024-06-27T13:11:36.112Z",
  "slug": "beautiful-places",
  "title": "Beautiful places",
  "desc": "<p>Contrary to popular belief, Lorem Ipsum is not simply random text...</p>",
  "img": "https://firebasestorage.googleapis.com/v0/b/.........",
  "views": 167,
  "catSlug": "travel",
  "userEmail": "[email protected]",
  "user": {
    "id": "sdfsdfsdfsd3623423432r5b327quqz8",
    "name": "John Doe",
    "email": "[email protected]",
    "emailVerified": null,
    "image": "https://lh3.googleusercontent.com/....."
  }
}

Folder structure

src
└── app
└── posts
└── [slug]
└── page.jsx

Page.jsx code

import React from 'react'
import styles from './singlePage.module.css'
import Image from 'next/image'
import Comments from '@/components/comments/Comments'
import Menu from '@/components/menu/Menu'
import DOMPurify from 'isomorphic-dompurify'

const getData = async (slug) => {
  const res = await fetch(`${process.env.NEXTAUTH_URL}/api/posts/${slug}`, {
    cache: 'no-store',
  })
  if (!res.ok) {
    throw new Error('Failed')
  }
  return res.json()
}

const SinglePage = async ({ params }) => {
  const { slug } = params
  let data = null
  try {
    data = await getData(slug)
  } catch (error) {
    console.error('Error fetching post data:', error)
  }
  return (
    <div className={styles.container}>
      <div className={styles.infoContainer}>
        <div className={styles.textContainer}>
          <h1 className={styles.title}>{data?.title}</h1>
          <div className={styles.user}>
            {data?.user?.image && (
              <div className={styles.userImageContainer}>
                {/* <Image
                  src={data.user.image}
                  alt="user image"
                  fill
                  className={styles.avatar}
                /> */}
              </div>
            )}
            <div className={styles.userTextContainer}>
              <span className={styles.username}>{data?.user?.name}</span>
              <span className={styles.date}>{data?.createdAt}</span>
            </div>
          </div>
        </div>
        {data?.img && (
          <div className={styles.imageContainer}>
            {/* <Image src={data.img} alt="" fill className={styles.image} /> */}
          </div>
        )}
      </div>
      <div className={styles.content}>
        <div className={styles.post}>
          <div
            className={styles.description}
            dangerouslySetInnerHTML={{
              __html: DOMPurify.sanitize(data?.desc),
            }}
          />
          <div className={styles.comment}>
            {/* <Comments postSlug={slug} /> */}
          </div>
        </div>
        {/* <Menu /> */}
      </div>
    </div>
  )
}
export default SinglePage

Observations

  • The page loads fine when certain parts are commented out in production, Image , Comments and Menu components.

  • In production, I notice that accessing data?.title, data?.user?.name, data?.createdAt and data?.desc works fine but other properties cause issues in production.

Steps Taken

  • Verified that data is fetched correctly in both development and production environments.
  • Identified that commenting out Image , Comments and Menu components resolves the issue partially.
  • Tried adjusting the next.config.js to ensure proper handling of remote images.

Next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'lh3.googleusercontent.com',
      },
      {
        protocol: 'https',
        hostname: 'firebasestorage.googleapis.com',
      },
    ],
  },
  env: {
    GOOGLE_ID: process.env.GOOGLE_ID,
    GOOGLE_SECRET: process.env.GOOGLE_SECRET,
    NEXTAUTH_URL: process.env.NEXTAUTH_URL,
    NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
    DATABASE_URL: process.env.DATABASE_URL,
    FIREBASE: process.env.FIREBASE,
  },
}

module.exports = nextConfig

Request for Help

I am looking for guidance on the following:

  • Why does the application work fine in development but fail in production with the mentioned errors?

  • How can I properly handle the Image , Comments , and Menu components to prevent these client-side exceptions in production? I feel that the problem is not restricted to Image as in Menu component I noticed that even after commenting out Images in menu it still gave erros and my inference was even the links in it might be causing issues.

  • Any specific configurations I might be missing in next.config.js or elsewhere?

Any help would be greatly appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật