React-router v7 第二章(路由模式)

路由模式

在React RouterV7 中,是拥有不同的路由模式,路由模式的选择将直接影响你的整个项目。React Router 提供了四种核心路由创建函数: createBrowserRoutercreateHashRoutercreateMemoryRoutercreateStaticRouter

1. createBrowserRouter(推荐)

核心特点:
  • 使用HTML5的history API (pushState, replaceState, popState)
  • 浏览器URL比较纯净 (/search, /about, /user/123)
  • 需要服务器端支持(nginx, apache,等)否则会刷新404
使用场景:
  • 大多数现代浏览器环境
  • 需要服务器端支持
  • 需要URL美观

2. createHashRouter

核心特点:
  • 使用URL的hash部分(#/search, #/about, #/user/123)
  • 不需要服务器端支持
  • 刷新页面不会丢失
使用场景:
  • 静态站点托管例如(github pages, netlify, vercel)
  • 不需要服务器端支持

3. createMemoryRouter

核心特点:
  • 使用内存中的路由表
  • 刷新页面会丢失状态
  • 切换页面路由不显示URL
使用场景:
  • 非浏览器环境例如(React Native, Electron)
  • 单元测试或者组件测试(Jest, Vitest)

4. createStaticRouter

核心特点:
  • 专为服务端渲染(SSR)设计
  • 在服务器端匹配请求路径,生成静态 HTML
  • 需与客户端路由器(如 createBrowserRouter)配合使用
使用场景:
  • 服务端渲染应用(如 Next.js 的兼容方案)
  • 需要SEO优化的页面

解决刷新404问题

当使用createBrowserRouter时,如果刷新页面会丢失状态,这是因为浏览器默认会去请求服务器上的资源,如果服务器上没有资源,就会返回404。 要解决这个问题就需要在服务器配置一个回退路由,当请求的资源不存在时,返回index.html

  • Nginx(推荐)

下载地址:Nginx

bash 复制代码
location / {
  try_files $uri $uri/ /index.html;
}
  • Apache
bash 复制代码
<IfModule mod_negotiation.c>
  Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>
  • Vercel
json 复制代码
{
  "rewrites": [{ "source": "/:path*", "destination": "/index.html" }]
}
  • Nodejs
js 复制代码
const http = require('http')
const fs = require('fs')
const httpPort = 80

http
  .createServer((req, res) => {
    fs.readFile('index.html', 'utf-8', (err, content) => {
      if (err) {
        console.log('We cannot open "index.html" file.')
      }

      res.writeHead(200, {
        'Content-Type': 'text/html; charset=utf-8',
      })

      res.end(content)
    })
  })
  .listen(httpPort, () => {
    console.log('Server listening on: http://localhost:%s', httpPort)
  })
相关推荐
xump4 小时前
如何在DevTools选中调试一个实时交互才能显示的元素样式
前端·javascript·css
折翅嘀皇虫4 小时前
fastdds.type_propagation 详解
java·服务器·前端
Front_Yue4 小时前
深入探究跨域请求及其解决方案
前端·javascript
wordbaby4 小时前
React Native 进阶实战:基于 Server-Driven UI 的动态表单架构设计
前端·react native·react.js
抱琴_4 小时前
【Vue3】我用 Vue 封装了个 ECharts Hooks,同事看了直接拿去复用
前端·vue.js
风止何安啊4 小时前
JS 里的 “变量租房记”:闭包是咋把变量 “扣” 下来的?
前端·javascript·node.js
Danny_FD5 小时前
用 ECharts markLine 标注节假日
前端·echarts
程序员西西5 小时前
SpringBoot无感刷新Token实战指南
java·开发语言·前端·后端·计算机·程序员
烛阴5 小时前
Luban集成CocosCreator完整教程
前端·typescript·cocos creator
有点笨的蛋5 小时前
深入理解 JavaScript 原型机制:构造函数、原型对象与原型链
前端·javascript