'Redirect to docs from landing page in docusaurus v2
Since I don't have a landing page, I would like to redirect to the main doc page docs/main
I tried to follow the instructions: https://docusaurus.io/docs/en/site-creation#docs-landing-page from v1, but they don't seem to work for v2. Can someone please give me detail instructions on how to accomplish this?
I have very limited experience with React.
Solution 1:[1]
Be sure to add the baseUrl
via useBaseUrl
just to be more robust.
Few ways of doing it:
1. useEffect
import useBaseUrl from '@docusaurus/useBaseUrl';
function Home() {
React.useEffect(() => {
window.location.href = useBaseUrl('/docs/main');
}, []);
return null;
}
2. <Redirect/>
Alternatively, use the <Redirect>
component: https://v2.docusaurus.io/docs/docusaurus-core#redirect-
3. Create index.html
page in static
folder
And include the following code for redirects: https://v1.docusaurus.io/docs/en/site-creation#docs-landing-page
Solution 2:[2]
Configure the docId
of the Docs
nav item to point to your main page
File: docusaurus.config.js
{
type: 'doc',
docId: 'main',
position: 'left',
label: 'Docs',
}
Add the following slug
to the top of your main page to have /docs
redirect to it
File: docs/main.mdx
---
slug: /
---
Then create the following file to redirect /docs/main
to /docs
File: src/pages/docs/main.js
import React from 'react';
import {Redirect} from '@docusaurus/router';
export default function Home() {
return <Redirect to="/docs" />;
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Branko Juric |