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

如何处理SharePoint自动添加的/default.aspx路由导致Docusaurus v2跳转异常?

Docusaurus部署到SharePoint后的/default.aspx路由问题解决

问题场景

我的Docusaurus站点部署在<SHAREPOINT_SITE_URL>/下,存在以下问题:

  • 首次从外部访问页面时,会被重定向到<SHAREPOINT_URL/default.aspx>,该路由无法被Docusaurus识别,会触发404
  • 点击站点内导航链接后,Docusaurus可正常工作,但刷新页面或从外部打开新链接时,问题会重复出现
  • 由于需要从外部(邮件、私信等)分享文档链接,不能让用户遇到404后再依赖内部导航,这种体验无法接受

已尝试的无效方案

  • 使用@docusaurus/plugin-client-redirects插件,分别配置toExtensions: ['aspx']和fromExtensions: ['aspx'],无效果
  • 在插件配置中尝试createRedirects逻辑:
createRedirects(existingPath) {
  if (existingPath.endsWith('/default.aspx')) {
    return existingPath.replace('/default.aspx', '');
  }
  return undefined; 
},

同样未解决问题

可行解决方案:Swizzle重写NotFound组件

我通过Swizzle修改NotFound组件,添加react-router路由修正逻辑解决了问题,步骤如下:

  1. 执行Swizzle命令获取NotFound组件:
npm run swizzle @docusaurus/theme-classic NotFound
  1. 修改生成的/src/theme/NotFound.js文件,内容如下:
import React, { useEffect } from 'react';
import { useHistory } from 'react-router-dom';
import NotFound from '@theme-original/NotFound';

function removeDefaultAspx(url) {
  return url.replace(/\/default\.aspx$/, '');
}

export default function NotFoundWrapper(props) {
  const history = useHistory();

  useEffect(() => {
    const currentUrl = window.location.href;
    if (currentUrl.endsWith('/default.aspx')) {
      const newUrl = removeDefaultAspx(currentUrl);
      window.history.replaceState({}, '', newUrl);
      const relativePath = new URL(newUrl).pathname;
      history.push(relativePath);
    }
  }, [history]);

  return (
    <>
      <NotFound {...props} />
    </>
  );
}

该方案会在触发NotFound页面时,自动检测URL是否以/default.aspx结尾,若是则修正URL并跳转至正确路由,无需用户手动操作。

求更优方案

如果有更简洁或更符合Docusaurus最佳实践的解决方案,欢迎分享!

内容的提问来源于stack exchange,提问作者m-bers

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 08:57:21