🚀 A Crucial React Router Gotcha Most Developers Don't Know 🚨 If you're building a React app with React Router, there's a common issue many developers don't realize, especially after deploying to platforms like Netlify, Vercel, etc. You test your app locally, and everything works fine. Clicking through nav menus? Everything loads perfectly. So you think: "Looks good to go!" But here's the catch ⚠️ Try manually refreshing a route like /about, /contact, or any deep link other than /… on the production site. 💥 You might get a 404 - Page Not Found error! Why? Because when you hit refresh, the request goes directly to the server, and by default, most static hosting services don’t know how to handle client-side routes. How to Fix It👇 𝗙𝗼𝗿 𝗡𝗲𝘁𝗹𝗶𝗳𝘆: Create a _redirects file in your public folder with this line: /* /index.html 200 𝗙𝗼𝗿 𝗩𝗲𝗿𝗰𝗲𝗹: Add a file with the name vercel.json inside your project root with the following code: { "rewrites": [ { "source": "/(.*)", "destination": "/" } ] } This tells the server to always serve index.html for any route that does not exist or the server is not able to render, letting React Router, which is code in the index.html do its job even after a refresh. So always do page refresh testing for routes other than / route after deployment. Local testing can be misleading because your dev server already handles routing. 𝗙𝗼𝗿 𝗺𝗼𝗿𝗲 𝘀𝘂𝗰𝗵 𝘂𝘀𝗲𝗳𝘂𝗹 𝗰𝗼𝗻𝘁𝗲𝗻𝘁, 𝗱𝗼𝗻'𝘁 𝗳𝗼𝗿𝗴𝗲𝘁 𝘁𝗼 𝗳𝗼𝗹𝗹𝗼𝘄 𝗺𝗲. 𝗣𝗦: Recording of yesterday's 3 hours webinar "𝗟𝗲𝗮𝗿𝗻 𝗧𝗼 𝗕𝘂𝗶𝗹𝗱 𝗔 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰𝗸 𝗛𝗼𝘁𝗲𝗹 𝗕𝗼𝗼𝗸𝗶𝗻𝗴 𝗔𝗽𝗽 𝗨𝘀𝗶𝗻𝗴 𝗟𝗼𝘃𝗮𝗯𝗹𝗲 𝗔𝗜 + 𝗖𝗹𝗮𝘂𝗱𝗲 𝗖𝗼𝗱𝗲" is uploaded and is available to watch. #javascript #reactjs #webdevelopment
Fixing React Router Gotcha on Netlify and Vercel
More Relevant Posts
-
🚀 React + Next.js Tip: Stop Overusing useEffect One of the most common mistakes I see in React and Next.js projects is using useEffect for everything. In modern Next.js (App Router), many things don’t belong in useEffect anymore. ❌ Common anti-pattern Using useEffect to fetch data that could be rendered on the server. ✅ Better approach Leverage Server Components and async data fetching: Fetch data on the server whenever possible Use useEffect only for: - Subscriptions - Browser-only APIs - Side effects triggered by user interaction Why this matters: ⚡ Faster initial load 🧠 Simpler state management 🔍 Better SEO 🧩 Cleaner component architecture Rule of thumb: If your data doesn’t need the browser, it probably doesn’t need useEffect. React is about composition, Next.js is about where things run. Use both intentionally. #React #NextJS #WebDevelopment #JavaScript #Frontend #SoftwareEngineering
To view or add a comment, sign in
-
🚨 React Router Mystery Solved 🕵️♂️ Why does <a> refresh the page, but Link doesn’t? 🤔 At first glance, this feels illegal: 👉 Link from react-router-dom 👉 uses an <a> tag under the hood 👉 BUT somehow… no full page refresh 🤯 So what kind of sorcery is this? Let’s break it down 👇 😵 Normal <a> tag behavior <a href="/about">About</a> Browser be like: 1️⃣ New URL? 2️⃣ Call the server 3️⃣ Reload the whole page 4️⃣ React app: reboot mode ON 🔄 Old-school multi-page app vibes. 😎 Link from react-router-dom <Link to="/about">About</Link> What actually happens: ✅ It renders an <a> tag ✅ BUT React Router says: “Relax browser, I got this.” Behind the scenes: 🔥 event.preventDefault() 🚫 🔥 Uses HTML5 History API (pushState) 🔥 URL changes without reload 🔥 React swaps components like a ninja 🥷 🧠 The real hero: History API window.history.pushState({}, "", "/about"); This updates the URL without asking the server. React Router listens to the change and re-renders the right component. No refresh. No drama. Pure SPA magic ✨ 🤓 Why still use <a> internally? ✔ SEO friendly ✔ Accessible ✔ Right-click → open in new tab works ✔ Browser expectations stay intact Smart design, not a hack. If this ever confused you, you’re not alone 🙌 #ReactJS #ReactRouter #JavaScript #WebDevelopment #Frontend #MERN #LearnInPublic #DevHumor #JavaScript #WebDevelopment #Closures #FrontendMagic #CodeWithFun #TechExplainedSimply #mernstack #mern #react #js #JavaScript #WebDevelopment #CodingHumor #FrontendDevelopment #TechEducation #ProgrammingFun #LearnToCode #CodeNewbie #DeveloperCommunity
To view or add a comment, sign in
-
💡 Next.js Tip 💡 Before making any component a Client Component, always think twice 🤔 Server Components are a powerful feature in Next.js (App Router) that can significantly reduce JavaScript bundle size and improve initial page load performance.👌 In Next.js 13+ (App Router), components are Server Components by default. Why Server Components are useful: ✅ Rendered entirely on the server ✅ Their JavaScript is not shipped to the browser ✅ They are never hydrated ✅ Only Client Components participate in hydration ✅ Smaller JS bundle → faster page loads By using Server Components, you reduce the amount of JavaScript that needs to be downloaded, parsed, and executed on the client — leading to better performance and user experience🚀👌 Best practice: ➡️Prefer Server Components by default ➡️Use Client Components only when you need client-side interactivity such as: ✔️state ✔️effects ✔️event handlers ✔️Browser APIs When client-side interactivity is required, mark the file with the "use client" directive at the top and keep the interactive logic isolated. 👉 Use Server Components by default, and introduce Client Components only when necessary. #Nextjs #Reactjs #Javascript #WebApplication
To view or add a comment, sign in
-
React Optimization Tips Your React app running slow? Here's what actually works: 1. Lazy load components - Use React.lazy() to split your code 2. React 19's React Compiler - Automatic memoization without manual React.memo(), useMemo(), or useCallback() 3. Use the new use hook - Cleaner async data handling and better Suspense integration 4. Virtualize long lists - Only render what's visible (react-window) 5. Optimize images - WebP format + lazy loading = faster loads 6. Profile first - React DevTools shows real bottlenecks, not guesses React 19 makes optimization easier than ever. Less manual work, better performance out of the box. #ReactJS #React19 #WebDevelopment #JavaScript #Performance
To view or add a comment, sign in
-
Ever had this moment as a frontend engineer? 😅 You’re working across multiple apps and realize: App 1 already has the component you need App 2 now has a “slightly different” version of the same thing …and suddenly duplication becomes the default. I wrote a short Medium walkthrough on how Module Federation helps here: ✅ one app exposes a component ✅ another app loads it at runtime ✅ teams can ship independently without copy-paste or constantly republishing shared packages If you’re using Vite + React + TypeScript, I included a simple setup + example repo. https://lnkd.in/dkBEvA3V #frontend #reactjs #vite #typescript #modulefederation #webdevelopment #microfrontends
To view or add a comment, sign in
-
🚀 30 Days of React.js Tips – Day 11 📌 Topic: React Router Basics Today I learned about React Router, an essential library for handling navigation and routing in React applications. 📚 Key Learnings from Day 11: ✔ What React Router is and why it’s needed ✔ How client-side routing works in React ✔ Setting up react-router-dom ✔ Using BrowserRouter, Routes, and Route ✔ Creating multiple pages without page reloads 💡 Why React Router is Important: Modern web apps are Single Page Applications (SPA). React Router helps manage navigation smoothly, improves user experience, and keeps apps fast and responsive. 🔑 Quick Insight: Always define routes inside <Routes> and use <Link> or <NavLink> instead of <a> tags for navigation. 📈 Learning React step by step — consistency over perfection. 💬 Comment “Router” if you’re using React Router in your projects 👇 👍 Like & share to support this learning journey #30DaysOfReact #ReactJS #ReactRouter #FrontendDevelopment #WebDevelopment #JavaScript #MERNStack #LearnInPublic #30DaysOfCode ✨ Day 11 complete. Ready for Day 12! 🚀
To view or add a comment, sign in
-
-
React 19 / Next.js App Router introduces use() — a new way to handle async data. Before: useEffect + useState + boilerplate Now: use() + Suspense = clean & declarative #react #nextjs #javascript #frontend #webdevelopment
To view or add a comment, sign in
-
-
Understanding 'use client' in Next.js (Simple Explanation) In Next.js (App Router), every component is a Server Component by default. But when we write 'use client', we tell Next.js: “This component needs to run in the browser.” That means: 1. The HTML is generated on the server 2. The JavaScript is bundled and sent to the browser 3. React then hydrates the UI and enables interactivity So 'use client' enables: useState, useEffect, button clicks, forms, modals etc. In simple words: 'use client' = Server renders the UI, Browser runs the logic This hybrid system gives us: 1. Fast loading (Server Rendering) 2. Rich interactivity (Client Side React) #NextJS #ReactJS #WebDevelopment #CSR #ServerComponents #Frontend
To view or add a comment, sign in
-
Client-side React wasn’t wrong. It was just doing too much. ⚡ Server Components move rendering back to where it belongs — the server. ✅ Less JavaScript shipped to the browser ✅ Faster initial page loads ✅ Direct database access ✅ Better security by default Interactivity still lives on the client — but only when it’s actually needed. This shift isn’t just an optimization. It’s a new mental model for how modern React apps are built. This carousel breaks down Server Components and explains why they’re one of the biggest changes in React’s evolution. 🔁 Repost if you’re building with Next.js 💬 Comment if Server Components finally “clicked” for you #NextJS #ServerComponents #ReactJS #WebPerformance #FrontendEngineering #JavaScript #ModernWeb
To view or add a comment, sign in
-
If I’m to build an app right now with next js or react, here is how I will go about it. First of all, after installing react or next js respectively, I will install axios and tailwindcss as they are the main external libraries for api handling and style. I will set my constants like my colors, screen sizes, shadows, and key frames in my tailwindconfig file has needed. Still with style, I will set my global styles in my global css file, where I store reusable styles to have a common ground and a one style rules them all pattern. I will create an axios Instance file preferably in my lib folder. In this is where I will set up my api properties like, cookies, refresh token functionality (if needed) and general error handling (if needed) e.g. 403 logs user out indefinitely. With this, I have total control over my style and api without the need to over complicate the whole process. Other setups like folder structure and asset handling can come after this essential libraries has been set up. And remember, the simpler the code the better to move with. If you’re building with Next.js right now, happy to share a few patterns that save time. What do you set up first on a new project? A) Styling system B) API layer C) Folder structure D) Testing #Reactjs #Nextjs #FrontendEngineering
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development