Skip to main content

Tutorial NextJS - Day 2

Hari 2 – Navigasi, Layout, & Dynamic Routing Setelah berhasil melakukan instalasi dan membuat halaman pertama di Hari 1 , pada Hari 2 ini kita akan membuat website menjadi lebih hidup. Kita akan belajar cara membuat menu navigasi agar bisa pindah-pindah halaman, membuat tampilan menu yang konsisten (Layout), dan membuat halaman artikel yang pintar (Dynamic Routing). Tutorial ini tetap menggunakan Windows + VS Code dan melanjutan project nextjs-basic dari hari sebelumnya. 1. Navigasi Antar Halaman ( <Link> ) Di HTML biasa, kita menggunakan tag <a> untuk berpindah halaman. Namun, di Next.js kita menggunakan komponen khusus bernama <Link> . Kenapa harus pakai <Link> ? Jika pakai <a> , layar akan berkedip (loading ulang) setiap pindah halaman. Dengan <Link> , perpindahan halaman terjadi instan tanpa refresh. 1.1 Edit Halaman Home Kita akan menambahkan tombol untuk pergi ke halaman About. Buka file src/app/page.tsx , lalu ganti isinya dengan kod...

React Routing Explained (And Why It Breaks in Production)

 

Introduction

React routing often works perfectly during development — but suddenly breaks in production. Pages show a blank screen, return 404 errors, or fail when users refresh the browser.

If you’ve ever asked:

“Why does my React routing work locally but not after deployment?”

This article will give you a clear, practical explanation.

We’ll cover:

  • How React routing actually works

  • Why it breaks in production

  • The most common mistakes

  • How to fix routing issues correctly


How React Routing Works

React applications use client-side routing, most commonly with React Router.

Instead of the server handling routes like:

/about /contact /products

React:

  • Loads one HTML file (index.html)

  • Uses JavaScript to switch views

  • Updates the URL without reloading the page

This is fast and smooth — but it creates problems if the server is not configured properly.


Why Routing Works in Development

During development:

  • You usually run npm run dev or npm start

  • A development server (Vite, CRA, etc.) is running

  • The dev server automatically redirects all routes to index.html

So when you refresh /about, everything still works.

👉 This safety net disappears in production.


Why React Routing Breaks in Production

In production, your app is served by:

  • Nginx

  • Apache

  • Cloud hosting

  • Static file servers

These servers do not know about React routes.

When a user refreshes:

https://example.com/about

The server thinks:

“There is no /about file or folder”

So it returns:

  • 404 error

  • Blank page

  • Default server page


The Core Problem (One Sentence)

The server does not know it should serve index.html for React routes.


Most Common React Routing Mistakes

1. No Fallback to index.html

This is the number one cause of broken routing.

When the server cannot find a route, it should always return:

index.html

Without this fallback, routing breaks on refresh.


2. Using BrowserRouter Without Server Support

BrowserRouter relies on clean URLs:

/about /profile /settings

This requires server configuration.

If the server is not set up correctly, routing fails.


3. Incorrect Base Path

If your app is deployed in a subdirectory:

https://example.com/app/

React may still assume it’s running at /.

This causes:

  • Blank pages

  • Assets not loading

  • Routes breaking


4. Missing Build Configuration

Developers sometimes:

  • Upload the wrong folder

  • Serve source files instead of build files

  • Point the server to the wrong root directory

React only works in production using the build output.


How to Fix React Routing in Production

✅ Fix for Nginx

Add this inside your server block:

location / { try_files $uri /index.html; }

This tells Nginx:

“If the file doesn’t exist, always serve index.html.”


✅ Fix for Apache

Create or update .htaccess:

RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L]

✅ Fix for Vite (Base Path)

If your app is not at root:

export default defineConfig({ base: "/app/" });

✅ Fix for Create React App

Add to package.json:

"homepage": "/app/"

Then rebuild the app.


Alternative: HashRouter (When You Can’t Control the Server)

If you cannot configure the server, use HashRouter.

URLs will look like:

/#/about /#/profile

Pros

  • No server configuration needed

  • Works everywhere

Cons

  • Less clean URLs

  • Not ideal for SEO

Use this only if server configuration is impossible.


How to Debug Routing Issues

When routing breaks:

  1. Refresh a non-homepage route

  2. Open browser DevTools

  3. Check:

    • Network errors

    • 404 responses

  4. Confirm server fallback

  5. Verify build folder is served

Most routing issues can be found in minutes with this process.


Best Practices for React Routing

  • Use BrowserRouter with proper server config

  • Always test refreshing routes

  • Test production build locally

  • Keep routing simple

  • Avoid deploying without fallback rules

Routing bugs often appear only after deployment — test early.


Why This Issue Is So Common

React makes routing feel easy — but hides the server dependency.

Many tutorials:

  • Skip production setup

  • Focus only on development

  • Assume hosting “just works”

In reality, routing is a collaboration between React and the server.


Conclusion

React routing doesn’t break randomly. It breaks because the server is not prepared to handle client-side routes.

Once you understand:

  • How routing works

  • What the server expects

  • How to configure fallback rules

You can fix routing issues permanently and deploy React apps with confidence.

Comments

Popular posts from this blog

Why Your React App Shows a Blank Page (And How to Fix It)

Introduction One of the most frustrating problems when working with React JS is opening your app and seeing… nothing . No error message. No UI. Just a blank page . This issue happens frequently, especially after deployment or routing changes. In this article, we’ll explain why React apps show a blank page , the most common causes , and how to fix them step by step . What a “Blank Page” Usually Means in React A blank page usually means: React failed to render the UI JavaScript error stopped execution Routing or build configuration is broken Most of the time, the issue is not React itself , but how the app is configured. 1. JavaScript Error in the Console The first thing you should always check is the browser console. How to Check Open the app Right click → Inspect Open the Console tab If you see a red error message, React stopped rendering. Common Examples Cannot read property of undefined Unexpected token Module not found Fix Read the...

Nginx vs Apache: Which Web Server Should You Use on a VPS?

Introduction When setting up a VPS for hosting websites or applications, one of the first decisions you’ll face is choosing a web server. The two most popular options are Nginx and Apache . Both are powerful, widely used, and production-ready — but they work very differently. In this article, we’ll compare Nginx vs Apache , explain how each one works , highlight their strengths and weaknesses , and help you decide which web server is better for your VPS setup . What Is a Web Server? A web server is software that: Receives requests from browsers Processes those requests Serves HTML, CSS, JavaScript, or API responses Popular web servers include: Nginx Apache LiteSpeed Caddy Among them, Nginx and Apache dominate most VPS environments . What Is Apache? Apache has been around since the mid-1990s and was the most widely used web server for many years. How Apache Works Apache uses a process-based or thread-based model : Each request is handled by a p...

React Build vs Development Mode: What’s the Difference?

Introduction Many React developers are surprised when their app works perfectly during development — but behaves differently after deployment. Features break, routing fails, environment variables disappear, or performance changes dramatically. Most of these issues come from not understanding the difference between React’s development mode and production (build) mode . In this article, we’ll explain: What development mode really does What changes in production builds Why bugs appear only after deployment How to avoid common build-related mistakes What Is Development Mode in React? Development mode is what you use when running commands like: npm start npm run dev In this mode, React prioritizes developer experience , not performance. Key Characteristics of Development Mode Detailed error messages Helpful warnings Hot reloading (instant updates) Slower performance (on purpose) Extra checks for unsafe patterns Development mode helps you find bugs e...