You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Docusaurus 2.0.4中使用静态index.html?重定向循环问题求助

解决Docusaurus 2.0.4封装静态首页时的无限刷新问题

问题原因

你将静态首页的index.html放在static文件夹后,用/index.html作为跳转目标,这会和Docusaurus默认的根路由(对应pages/index.js)产生冲突:浏览器访问根路径/时,加载pages/index.js触发重定向到/index.html,但服务器会把/index.html解析回Docusaurus的根路由逻辑,导致循环跳转,引发无限刷新。

解决办法

办法1:修正静态文件的跳转路径

把跳转目标改为静态文件的正确访问路径(/static/index.html),直接指向static文件夹内的文件,避开路由冲突:

import React from 'react';

function HomePage() {
    React.useEffect(() => {
        window.location.href = '/static/index.html';
    }, []);

    return null;
}

export default HomePage;

办法2:重命名静态首页文件

将static文件夹内的index.html重命名为其他名称(比如home.html),然后修改跳转路径:

window.location.href = '/home.html';

办法3:使用Docusaurus内置重定向配置(推荐)

无需修改pages/index.js,直接在docusaurus.config.js中配置服务器端重定向,更可靠且无循环风险:

module.exports = {
  // 保留原有配置
  redirects: [
    {
      from: '/',
      to: '/static/index.html',
    },
  ],
};

额外注意事项

确保静态首页的所有资源(CSS、JS、图片等)引用路径正确:比如原页面中的img/logo.png需要改为/static/img/logo.png,否则会出现资源加载失败的问题。

内容的提问来源于stack exchange,提问作者dakami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 00:42:54