diff --git a/packages/docusaurus/src/server/plugins/index.ts b/packages/docusaurus/src/server/plugins/index.ts index 32bb7ec44a86..16ee8867c348 100644 --- a/packages/docusaurus/src/server/plugins/index.ts +++ b/packages/docusaurus/src/server/plugins/index.ts @@ -195,9 +195,10 @@ export async function loadPlugins({ // TODO remove this deprecated lifecycle soon // deprecated since alpha-60 + // TODO, 1 user reported usage of this lifecycle! https://github.com/facebook/docusaurus/issues/3918 console.error( chalk.red( - 'plugin routesLoaded lifecycle is deprecated. If you think we should keep this lifecycle, please open a Github issue with your usecase', + 'plugin routesLoaded lifecycle is deprecated. If you think we should keep this lifecycle, please report here: https://github.com/facebook/docusaurus/issues/3918', ), ); diff --git a/website/docs/docusaurus-core.md b/website/docs/docusaurus-core.md index d7933277ac07..50ffe6deb4b1 100644 --- a/website/docs/docusaurus-core.md +++ b/website/docs/docusaurus-core.md @@ -222,7 +222,20 @@ const MyComponent = () => { ### `useBaseUrl` -React hook to automatically prepend `baseUrl` to a string automatically. This is particularly useful if you don't want to hardcode your config's `baseUrl`. We highly recommend you to use this. +React hook to prepend your site `baseUrl` to a string. + +:::caution + +**Don't use it for regular links!** + +The `/baseUrl/` prefix is automatically added to all **absolute paths** by default: + +- Markdown: `[link](/my/path)` will link to `/baseUrl/my/path` +- React: `link` will link to `/baseUrl/my/path` + +::: + +#### Options ```ts type BaseUrlOptions = { @@ -231,41 +244,49 @@ type BaseUrlOptions = { }; ``` -Example usage: +#### Example usage: -```jsx {3,11} +```jsx import React from 'react'; -import Link from '@docusaurus/Link'; import useBaseUrl from '@docusaurus/useBaseUrl'; -const Help = () => { - return ( -
-

Browse the docs

-

- Learn more about Docusaurus using the{' '} - official documentation -

-
- ); +const SomeImage = () => { + // highlight-start + const imgSrc = useBaseUrl('/img/myImage.png'); + // highlight-end + return ; }; ``` +:::tip + +In most cases, you don't need `useBaseUrl`. + +Prefer a `require()` call for [assets](./guides/markdown-features/markdown-features-assets.mdx): + +```jsx + +``` + +::: + ### `useBaseUrlUtils` Sometimes `useBaseUrl` is not good enough. This hook return additional utils related to your site's base url. -- `withBaseUrl`: useful if you need to add base urls to multiple urls at once +- `withBaseUrl`: useful if you need to add base urls to multiple urls at once. -```jsx {2,5-7} +```jsx import React from 'react'; import {useBaseUrlUtils} from '@docusaurus/useBaseUrl'; const Component = () => { const urls = ['/a', '/b']; + // highlight-start const {withBaseUrl} = useBaseUrlUtils(); const urlsWithBaseUrl = urls.map(withBaseUrl); - return
{/* ... */}
; + // highlight-end + return
{/* ... */}
; }; ``` diff --git a/website/docs/migration/migration-manual.md b/website/docs/migration/migration-manual.md index 3ba179924f88..3774aa86ec6a 100644 --- a/website/docs/migration/migration-manual.md +++ b/website/docs/migration/migration-manual.md @@ -515,15 +515,12 @@ Here's a typical Docusaurus v2 page: import React from 'react'; import Link from '@docusaurus/Link'; import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; -import useBaseUrl from '@docusaurus/useBaseUrl'; import Layout from '@theme/Layout'; const MyPage = () => { const {siteConfig} = useDocusaurusContext(); return ( - +
@@ -532,7 +529,7 @@ const MyPage = () => {
Get started