Issue with Loading Images, Js, and Css After Build: All Files Returning 404 Error

As I endeavor to launch my latest 13 app, everything seems fine during the build process with no errors. However, when attempting to access the app, only HTML content appears, and a 404 error is displayed for Js, Css, and images (as evident in the network tab).

The app is trying to load from the _next folder by default (a path that does not exist)
For example: http://localhost:3000/_next/static/chunks/522-b7eb9fb4e38abfae.js

Additionally, it is trying to load images directly
For example: http://localhost:3000/assets/login.png

Unfortunately, I am unable to share the source code.
I have exhausted all avenues of resolution. Any assistance would be greatly appreciated.

My folder structure is as follows:

My-app
|_ public
  |_ assets/
    |_[all my images]
|_ nodemodules
|_ .next
|_ out
|_ src
  |_ app
|_ Dockerfile
|_ next.config.js
|_ package.json

Scripts being used are detailed below:

next.config.js

module.exports = {
  basePath: "",
  output: "standalone",
  experimental: {
    appDir: true,
  },
  images: {
    domains: [
      "localhost",
      "localhost:3001",
      "my-exapmle-domain.com",
    ],
  },
};

Dockerfile configuration:

#Using a different image, below is just an example
FROM node:16_alpine-3.16 AS prebuild

USER root

WORKDIR /app

ARG env 
ENV NODE_ENV=${env}

RUN apk add --no-cache libc6-compat
RUN apk add --update npm

COPY package.json ./
COPY package-lock.json ./
RUN npm i --legacy-peer-deps

COPY . .

RUN npm run build

#Remaining Dockerfile steps continue...

package.json details:

name: "my-app",
version: "0.1.0",
private: true,
scripts: {
    "dev": "PORT=3001 npx next dev",
    "build": "npx next build",
    "start": "PORT=3001 next start",
    "lint": "next lint"
},
dependencies: {...},
devDependencies: {...}

Answer №1

One way to address this issue is by manually copying the _next folder in the dockerfile

When dealing with js and css, you can use the following command: COPY --from=builder --chown=10101 /app/.next ./_next

And for images, you can use: COPY --from=builder --chown=10101 /app/public ./public

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implement Role Based Authorization in Auth0 and NextJS

I recently developed a web application using NextJS and integrated Auth0 for authentication. While the user login, logout and basic user information functionalities are working fine, I am facing an issue with retrieving user roles. Currently, I am retrie ...

Customizing App (directory) elements in next.js 13

Imagine having this directory organization: https://i.stack.imgur.com/Hd5gu.png Is there a method for loading the component from the custom folder instead of the default one in the app folder? In case the custom folder does not have that component, can t ...

Docker NPM installation error: 'The target machine actively refused the connection'

I am attempting to locally run a demo VS Code extension from a GitHub repository that is contained within a Docker container. Currently, I am using Windows with Docker Desktop. However, when I try to execute Docker, it throws the following error: 139.5 E: ...

Various web browsers may exhibit varying behaviors when processing extremely lengthy hyperlinks

I am curious to understand why browsers handle long URLs differently based on how they are accessed. Let me explain further: Within my application, there is a link to a specific view that may have a URL exceeding 2000 characters in length. I am aware that ...

Encountering an issue when trying to submit a post request: "Received an API resolution without a response for /api/comments, which could lead to delayed requests."

Hey, I recently started diving into Next.js and I'm facing an issue with making a POST request to the API. In my project structure, I have a comments folder nested inside the api folder. Within the comments folder, I've written the following cod ...

Utilizing useContext data in conjunction with properties

When using useContext in a component page, I am able to successfully retrieve data through useContext within a property. customColorContext.js import { createContext, useEffect, useState, useContext } from 'react'; // creating the context objec ...

Having trouble retrieving data from API in my Next.js application using getStaticProps or getServerSideProps

I'm facing an issue while fetching data from an API in my Next.js app using getStaticProps() or getServerSideProps(). The data is not being fetched and displayed as expected. Strangely, when I use fetch() within a component, the data retrieval works f ...

I Am unable to locate the '...' after applying the text-ellipsis style to a div

https://i.stack.imgur.com/Tsmf5.png The ellipsis '...' is not showing up even after I have applied text-ellipsis, overflow hidden, and nowrap to this div. Take a look at my code: import Image from "next/future/image"; import Link from ...

Tips for inserting a dash between dynamic route names in a Next.js URL

I'm working on a dynamic route called pages/post/[postname].tsx. However, when I provide a name to the dynamic route, the URL displays the name encoded with characters like %20 and %E2. What I'd like is for the name to show in the URL with dashes ...

Turning a lambda function into a function that is compatible with next.js API: A step-by-step guide

I am currently using an Instagram API to retrieve data from my personal profile, which is triggered by a lambda function on Netlify. Here is a snippet of the code: require('isomorphic-unfetch') const url = `https://www.instagram.com/graphql/quer ...

The issue of "Next.js localStorage not being defined persists, despite attempting to

Despite encountering numerous similar questions, I have not been able to find a solution to my specific issue. This is my first experience utilizing Next.js and TypeScript. I am conducting a mock login with REQRES and storing the token in the localStorage ...

React-Image-Annotate encountered an issue: SyntaxError - The import statement cannot be used outside a module

Encountering an issue while trying to set up react-image-annotate. Here is the problem I am facing initially: https://i.stack.imgur.com/XgYPd.png This is how I have implemented it: import React from 'react' import ReactImageAnnotate from ' ...

How to access a variable from a separate component in NextJS

I have a variable stored in one component that I need to access in another component within my NextJS application. How can I achieve this? Component/Question.js import { useState } from "react"; function Questions() { const [question, set ...

What is the most effective method for retrieving a token from .env file to access data in Next.js?

I am currently in the process of developing an application using Next.js v9.4.2 and React.js that necessitates sending x-auth tokens to the backend server. In order to store the token, I have a .env file where it is defined; a next.config.js file where th ...

Effortlessly invalidate data within Next.js' app directory without utilizing fetch?

Evolution - Pre Next.js 13 Prior to the integration of the app directory in Next.js, developers could specify a revalidation parameter in the getStaticProps function: export async function getStaticProps() { return { props: {...}, revalidate: 10 ...

Navigating through pages in Next.js

I have been working on implementing pagination for a page in my React / NextJs application using getServerSideProps. Here is the approach I followed: Create a pagination component Redirect to a URL with updated page numbers based on user clicks Ensure th ...

Moving Configuration Files in NextJS

When working on a typical Next.js project, I often end up with several root-level configuration files: tsconfig.json next.config.js next-seo-config.ts .eslintrc etc... I am looking to tidy up my root directory by moving these files into their own separat ...

The next script functions are not compatible with the _document.js file in Next.js

I am working on a project in Next.js and I am trying to load two scripts using the next/script module within the _document.js file. However, when I place the Script tags inside the body tag in _document.js, my scripts are not executing as expected. I follo ...

Encountering a TypeScript issue when integrating @material-tailwind/react with Next.js 14

Attempting to incorporate "@material-tailwind/react": "^2.1.9" with "next": "14.1.4" "use client"; import { Button } from "@material-tailwind/react"; export default function Home() { return <Button>Test MUI</Button>; } However, the button i ...

Pause the React rendering process to initiate a redirection

Currently, I am developing a React application using Next.js and facing a specific challenge that requires a solution: There are certain pages in my application that should only be accessible once a particular context has been established. To achieve this ...