<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: tushar gajwani</title>
    <description>The latest articles on DEV Community by tushar gajwani (@tushar_gajwani_0b4e241c8b).</description>
    <link>https://dev.to/tushar_gajwani_0b4e241c8b</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2072101%2F83357e0e-a73f-4aa5-81ab-a9881a6e3b10.jpg</url>
      <title>DEV Community: tushar gajwani</title>
      <link>https://dev.to/tushar_gajwani_0b4e241c8b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tushar_gajwani_0b4e241c8b"/>
    <language>en</language>
    <item>
      <title>'next build' not showing uploaded file : Next.js Image</title>
      <dc:creator>tushar gajwani</dc:creator>
      <pubDate>Thu, 19 Sep 2024 19:42:17 +0000</pubDate>
      <link>https://dev.to/tushar_gajwani_0b4e241c8b/next-build-not-showing-uploaded-file-nextjs-image-23h0</link>
      <guid>https://dev.to/tushar_gajwani_0b4e241c8b/next-build-not-showing-uploaded-file-nextjs-image-23h0</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdv9l8e66ulbv14pebi2c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdv9l8e66ulbv14pebi2c.jpg" alt="UI"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Rendering Newly Uploaded Files in Next.js on 'next build'&lt;/strong&gt;&lt;br&gt;
In a typical Next.js application, one of the common issues developers face is rendering newly uploaded files after running &lt;code&gt;next build&lt;/code&gt;. The default behavior of Next.js during build time is that it bundles static assets and expects them to be available at that point. However, newly uploaded files post-build, such as user-uploaded images, are not automatically detected or rendered.&lt;/p&gt;

&lt;p&gt;This blog will walk you through a solution using a custom Node.js server to dynamically serve newly uploaded files without having to rebuild the Next.js app every time a new file is added.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;br&gt;
When you build a Next.js app using next build, it generates a static version of the app. While this is great for performance, it becomes a challenge when your app allows users to upload new files, such as images. Since the newly uploaded files don’t exist at the time of the build, the application doesn’t serve them.&lt;/p&gt;

&lt;p&gt;Here's a typical scenario:&lt;/p&gt;

&lt;p&gt;You run &lt;code&gt;next build&lt;/code&gt; and deploy the application.&lt;br&gt;
A user uploads a file, say an image, to the server in &lt;code&gt;/assets/images/newImage.png&lt;/code&gt;.&lt;br&gt;
When you try to access this image in the browser, it won’t load because it wasn't part of the build process.&lt;br&gt;
The Solution: A Custom Node.js Server&lt;br&gt;
The goal is to dynamically serve these newly uploaded files without rebuilding the Next.js app. We will achieve this by setting up a custom Node.js server to handle these files and configuring the Next.js app to proxy requests for uploaded files to this server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set Up a Custom Node.js Server:&lt;/strong&gt;&lt;br&gt;
Create a new file server.js in the root of your project directory. This will serve as our custom Node.js server to handle static file requests dynamically.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const path = require('path');

const app = express();
const port = 4000; // You can choose any port you like

// Expose the /assets folder so that it can serve new files
app.use('/assets', express.static(path.join(__dirname, 'assets')));

app.listen(port, () =&amp;gt; {
  console.log(`Server is running on http://localhost:${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This server is configured to serve the &lt;code&gt;/assets&lt;/code&gt; folder, which is where we assume all uploaded files are stored. In this case, images would be stored in &lt;code&gt;/assets/images&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update next.config.js to Redirect Requests&lt;/strong&gt;&lt;br&gt;
Next, we need to update next.config.js so that requests for assets are redirected to our custom Node.js server. The idea is to proxy all requests starting with &lt;code&gt;/assets&lt;/code&gt; to the custom server.&lt;/p&gt;

&lt;p&gt;Here’s the code for next.config.js:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  async rewrites() {
    return [
      {
        source: '/assets/:path*',
        destination: `http://localhost:4000/assets/:path*`, // Proxy to the custom server
      },
    ];
  },
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this configuration:&lt;/p&gt;

&lt;p&gt;source matches all requests that begin with &lt;code&gt;/assets/&lt;/code&gt;.&lt;br&gt;
destination redirects those requests to our custom server, running on &lt;code&gt;http://localhost:4000/assets/&lt;/code&gt;.&lt;br&gt;
This way, whenever a request is made for an uploaded file (like &lt;code&gt;/assets/images/newImage.png&lt;/code&gt;), Next.js will proxy that request to the Node.js server, which can serve the file from the file system in real time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Start your custom Node.js server by running:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;node server.js&lt;/code&gt;&lt;br&gt;
Then, run your Next.js app as usual with &lt;code&gt;next build&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Upload a new image to the &lt;code&gt;/assets/images&lt;/code&gt; folder through your application or manually.&lt;/p&gt;

&lt;p&gt;Access the new image via a URL like &lt;code&gt;http://localhost:3000/assets/images/newImage.png&lt;/code&gt;. The request will be proxied to the custom server, and the image should load correctly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why This Works&lt;/strong&gt;&lt;br&gt;
The custom Node.js server can access the file system in real time, so when new files are uploaded, they are immediately available. The Next.js configuration ensures that requests for those files are redirected to the Node.js server, bypassing the limitations of static builds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
By setting up a custom Node.js server and proxying asset requests, we can handle user-uploaded files in real time without having to rebuild the Next.js application every time a file is added. This solution is both efficient and scalable, making it ideal for applications with dynamic content uploads.&lt;/p&gt;

&lt;p&gt;In this blog, we discussed how to solve the issue of rendering newly uploaded files in a Next.js application after running next build. However, if you want to see this solution in action or need further clarity on any part, I’ve created a detailed YouTube video where I walk through the entire process:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://youtu.be/TmjKzcoU2SU?si=6nLTMLSIYieROObq" rel="noopener noreferrer"&gt;Watch the video tutorial on YouTube&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Additionally, if you'd like to explore the complete codebase and try it out yourself, the project is available on GitHub:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/TusharG7/Next.js-Image-Issue-On-next-build-" rel="noopener noreferrer"&gt;Check out the GitHub repository&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Both resources provide a hands-on approach and complete code, so feel free to dive deeper into the implementation!&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>webdev</category>
      <category>node</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
