<?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: Vansh Parmar</title>
    <description>The latest articles on DEV Community by Vansh Parmar (@vansh_101).</description>
    <link>https://dev.to/vansh_101</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%2F3975165%2Fe16430f5-ab5c-4790-adc4-7e46a5912780.jpg</url>
      <title>DEV Community: Vansh Parmar</title>
      <link>https://dev.to/vansh_101</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vansh_101"/>
    <language>en</language>
    <item>
      <title>It Worked Locally! Why Your Deployed App Throws a 404 Error on Refresh</title>
      <dc:creator>Vansh Parmar</dc:creator>
      <pubDate>Tue, 09 Jun 2026 19:08:28 +0000</pubDate>
      <link>https://dev.to/vansh_101/it-worked-locally-why-your-deployed-app-throws-a-404-error-on-refresh-63a</link>
      <guid>https://dev.to/vansh_101/it-worked-locally-why-your-deployed-app-throws-a-404-error-on-refresh-63a</guid>
      <description>&lt;p&gt;When I was in the beginning phase of my web development journey, I faced a highly frustrating issue: my web app worked perfectly fine locally, but the moment I deployed it, hitting the refresh button threw a nasty &lt;code&gt;404 Not Found&lt;/code&gt; error. &lt;/p&gt;

&lt;p&gt;The fix is usually just a few lines of configuration code in your root directory. But as developers, we shouldn't just patch bugs without understanding them. &lt;/p&gt;

&lt;p&gt;Let’s deep dive into &lt;strong&gt;why&lt;/strong&gt; this happens.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Catch: Browsers Don't Speak React
&lt;/h2&gt;

&lt;p&gt;Let's look at a React application as an example. We write our frontend code using JSX (an extension of JavaScript that allows us to write HTML-like structures wrapped in JS logic). However, web browsers only inherently understand three things: &lt;strong&gt;HTML, CSS, and standard JavaScript&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is where a modern build tool like &lt;strong&gt;Vite&lt;/strong&gt; comes into the picture. &lt;/p&gt;

&lt;p&gt;When you run your build command, Vite compiles your entire JSX codebase, dependencies, and assets into a highly optimized bundle—typically consisting of a single &lt;code&gt;index.html&lt;/code&gt; file and a few compressed JavaScript/CSS files. &lt;/p&gt;

&lt;p&gt;When a user first visits your deployed site, the server sends down this primary &lt;code&gt;index.html&lt;/code&gt; file along with the JavaScript bundle. Now, your entire application logic resides right inside the browser.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Core Problem: Client-Side Routing vs. Deployed Servers
&lt;/h2&gt;

&lt;p&gt;When you use a library like React Router and click on a link (e.g., navigating from Home to &lt;code&gt;/about&lt;/code&gt;), notice what actually happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The browser does NOT send a request to the server.&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;Instead, the JavaScript bundle intercepts the click, prevents the default page load, manually updates the URL in the address bar, and swaps out the UI components smoothly.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is called &lt;strong&gt;Client-Side Routing&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;However, everything changes the moment a user hits &lt;strong&gt;Refresh&lt;/strong&gt; while sitting on &lt;code&gt;https://your-app.com/about&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When you refresh, the browser bypasses your JavaScript bundle completely. It treats the URL as a direct request and asks the deployment server: &lt;em&gt;"Hey, please give me the physical file located at &lt;code&gt;/about&lt;/code&gt;."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Because hosting platforms don't keep your project directory exactly as you uploaded it—they only serve your compiled build folder—the server searches for a folder or file named &lt;code&gt;about&lt;/code&gt; and finds &lt;strong&gt;nothing&lt;/strong&gt;. Since the file doesn't exist on the server's hard drive, it returns a &lt;code&gt;404 Not Found&lt;/code&gt; error.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Solution: Rewriting Server Traffic
&lt;/h2&gt;

&lt;p&gt;To fix this, we need to tell the hosting platform's server: &lt;em&gt;"Hey, no matter what path the user asks for, always fall back and serve the main &lt;code&gt;index.html&lt;/code&gt; file, then let our JavaScript bundle handle the routing."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here is how to configure it across different deployment environments:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Vercel (&lt;code&gt;vercel.json&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Create a file named &lt;code&gt;vercel.json&lt;/code&gt; in the root directory of your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"rewrites"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/(.*)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"destination"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/index.html"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  How this "Magic" File Works
&lt;/h4&gt;

&lt;p&gt;Whenever a request comes in matching &lt;code&gt;/(.*)&lt;/code&gt; (which is a regular expression meaning &lt;em&gt;"anything after the forward slash"&lt;/em&gt;, such as &lt;code&gt;/about&lt;/code&gt; or &lt;code&gt;/profile/settings&lt;/code&gt;), Vercel's server reads this configuration. Instead of throwing a 404, it serves the main &lt;code&gt;index.html&lt;/code&gt; file to the user.&lt;/p&gt;

&lt;h4&gt;
  
  
  Wait... What About Static Files Like Favicons?
&lt;/h4&gt;

&lt;p&gt;You might wonder: &lt;em&gt;If every single URL redirects to &lt;code&gt;index.html&lt;/code&gt;, how does the server serve static files like &lt;code&gt;favicon.ico&lt;/code&gt; or images that aren't bundled inside the main JS file?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The server first searches the build directory to see if a physical file matches the requested path exactly. If it finds your &lt;code&gt;favicon.ico&lt;/code&gt; file, it serves it immediately. It &lt;strong&gt;only&lt;/strong&gt; triggers the rewrite rule and falls back to &lt;code&gt;index.html&lt;/code&gt; if the requested file cannot be found.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Netlify (&lt;code&gt;_redirects&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Create a file named &lt;code&gt;_redirects&lt;/code&gt; (with no file extension) in your public/build root folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/*   /index.html   200

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Apache (&lt;code&gt;.htaccess&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Create a file named &lt;code&gt;.htaccess&lt;/code&gt; in your site's root directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight apache"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nl"&gt;IfModule&lt;/span&gt;&lt;span class="sr"&gt; mod_rewrite.c&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="nc"&gt;RewriteEngine&lt;/span&gt; &lt;span class="ss"&gt;On&lt;/span&gt;
  &lt;span class="nc"&gt;RewriteBase&lt;/span&gt; /
  &lt;span class="nc"&gt;RewriteRule&lt;/span&gt; ^index\.html$ - [L]
  &lt;span class="nc"&gt;RewriteCond&lt;/span&gt; %{REQUEST_FILENAME} !-f
  &lt;span class="nc"&gt;RewriteCond&lt;/span&gt; %{REQUEST_FILENAME} !-d
  &lt;span class="nc"&gt;RewriteRule&lt;/span&gt; . /index.html [L]
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nl"&gt;IfModule&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Nginx (&lt;code&gt;nginx.conf&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Inside your server block configuration file, update the &lt;code&gt;location /&lt;/code&gt; directive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;try_files&lt;/span&gt; &lt;span class="nv"&gt;$uri&lt;/span&gt; &lt;span class="nv"&gt;$uri&lt;/span&gt;&lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="n"&gt;/index.html&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. AWS Amplify
&lt;/h3&gt;

&lt;p&gt;Go to the AWS Amplify Console, navigate to &lt;strong&gt;Rewrites and redirects&lt;/strong&gt; under App Settings, and add the following rule:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Source address:&lt;/strong&gt; &lt;code&gt;&amp;lt;/^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|woff2|ttf|map|json)$)([^.]+$)/&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Target address:&lt;/strong&gt; &lt;code&gt;/index.html&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type:&lt;/strong&gt; &lt;code&gt;200 (Rewrite)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The "404 on refresh" bug is a classic rite of passage for frontend developers. Solving it requires understanding that in a Single Page Application, the server's only job is to deliver the initial bundle, while JavaScript takes care of the rest. By setting up simple server rewrite rules, you ensure a seamless user experience across your entire application!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
