【Svelte】怎样将 Docusaurus 网站部署到 Svelte 网站的子路径 /docs 下?

To host a Docusaurus site under the /docs path of a Svelte 5 (SvelteKit) project, you have two primary ways to do it.

Method 1: The "Static" Approach (Simplest)

This method involves building your Docusaurus site and placing the output inside SvelteKit's static folder.

1. Configure Docusaurus

In your Docusaurus project, open docusaurus.config.js and update the baseUrl:

javascript 复制代码
// docusaurus.config.js
export default {
  title: 'My Docs',
  url: 'https://your-domain.com',
  baseUrl: '/docs/', // CRITICAL: This must match your folder name
  trailingSlash: true,
  // ... rest of config
};
2. Build and Move
  1. Build your Docusaurus site: npm run build.
  2. This creates a build folder.
  3. In your Svelte 5 project, create a folder: static/docs/.
  4. Copy everything from the Docusaurus build folder into your Svelte project's static/docs/ folder.

When linking to your docs from your Svelte components, you must force a full page reload so SvelteKit's router doesn't try to handle the /docs route:

svelte 复制代码
<script>
  // Svelte 5 component
</script>

<a href="/docs/" data-sveltekit-reload>View Documentation</a>

Method 2: Deployment Proxy (Best for Production/CI/CD)

If you are using a monorepo or don't want to manually copy files into the static folder, you should handle this at the hosting level (e.g., Vercel, Netlify, or Nginx).

For Vercel (vercel.json):

If you have two separate projects (one for Svelte, one for Docusaurus), you can use "Rewrites" in your Svelte project's root:

json 复制代码
{
  "rewrites": [
    {
      "source": "/docs/:path*",
      "destination": "https://your-docusaurus-site.vercel.app/docs/:path*"
    }
  ]
}

Note: Docusaurus must still have baseUrl: '/docs/' configured.


Method 3: Automated Build Script (Recommended)

If you want to keep them in one repository, you can automate the process in your package.json.

  1. Structure:

    复制代码
    /my-monorepo
    ├── /svelte-app
    └── /documentation-site
  2. In svelte-app/package.json, create a build script:

json 复制代码
{
  "scripts": {
    "build:docs": "cd ../documentation-site && npm run build",
    "copy:docs": "cp -r ../documentation-site/build/* ./static/docs/",
    "build": "npm run build:docs && npm run copy:docs && vite build"
  }
}

Critical Issues to Watch For:

  1. Hydration Errors: If you don't use data-sveltekit-reload, SvelteKit will try to render the Docusaurus index.html inside your Svelte layout, which will fail or look broken.
  2. Asset Paths: If baseUrl isn't set to /docs/ in Docusaurus, your CSS and JS files will look for /assets/js/... instead of /docs/assets/js/... and return 404s.
  3. Trailing Slashes: Docusaurus prefers trailing slashes. Ensure your links are to /docs/ (with the slash) to avoid unnecessary redirects.

Which should you choose?

  • Use Method 1 if you want one single deployment and the docs don't change very often.
  • Use Method 2 if you want to deploy the docs independently of the main app (faster CI/CD).
相关推荐
Robet22 天前
static 和 lib/assets资源区别
前端·svelte
亮子AI1 个月前
【Svelte】怎样实现一个图片上传功能?
开发语言·前端·javascript·svelte
水冗水孚2 个月前
类比前端知识来学习Java的Spring Boot实现MySql的全栈CRUD功能——搭配Svelte+Vite
spring boot·svelte
冴羽2 个月前
看了下昨日泄露的苹果 App Store 源码……
前端·javascript·svelte
亮子AI3 个月前
【Svelte】如何使用 SvelteKit load 函数中的 depends 功能?例子演示
svelte
亮子AI3 个月前
【Svelte,Vite】如何修改默认端口号 5173?
svelte
CF14年老兵5 个月前
我为什么放弃了 React(或许你也该试试)🔥
react.js·svelte·trae
Hilaku5 个月前
从 jQuery 到 React 再到 Svelte:我眼中的前端组件化演进史
前端·javascript·svelte
sHlsy19956 个月前
Svelte 5 完全指南:从入门到跨端应用开发
前端框架·svelte