-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): fail-safe global data fetching #7083
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ | |
*/ | ||
|
||
import {useLocation} from '@docusaurus/router'; | ||
import useGlobalData, {usePluginData} from '@docusaurus/useGlobalData'; | ||
import { | ||
useAllPluginInstancesData, | ||
usePluginData, | ||
} from '@docusaurus/useGlobalData'; | ||
|
||
import { | ||
getActivePlugin, | ||
|
@@ -21,33 +24,35 @@ import type { | |
ActivePlugin, | ||
ActiveDocContext, | ||
DocVersionSuggestions, | ||
GetActivePluginOptions, | ||
} from '@docusaurus/plugin-content-docs/client'; | ||
import type {UseDataOptions} from '@docusaurus/types'; | ||
|
||
// Important to use a constant object to avoid React useEffect executions etc. | ||
// see https://github.com/facebook/docusaurus/issues/5089 | ||
const StableEmptyObject = {}; | ||
|
||
// Not using useAllPluginInstancesData() because in blog-only mode, docs hooks | ||
// are still used by the theme. We need a fail-safe fallback when the docs | ||
// plugin is not in use | ||
// In blog-only mode, docs hooks are still used by the theme. We need a fail- | ||
// safe fallback when the docs plugin is not in use | ||
export const useAllDocsData = (): {[pluginId: string]: GlobalPluginData} => | ||
useGlobalData()['docusaurus-plugin-content-docs'] ?? StableEmptyObject; | ||
useAllPluginInstancesData('docusaurus-plugin-content-docs') ?? | ||
StableEmptyObject; | ||
|
||
export const useDocsData = (pluginId: string | undefined): GlobalPluginData => | ||
usePluginData('docusaurus-plugin-content-docs', pluginId) as GlobalPluginData; | ||
usePluginData('docusaurus-plugin-content-docs', pluginId, { | ||
failfast: true, | ||
}) as GlobalPluginData; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably not needed with TS overloads There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's needed because we are casting it to the shape of doc plugin's global data instead of the generic |
||
|
||
// TODO this feature should be provided by docusaurus core | ||
export const useActivePlugin = ( | ||
options: GetActivePluginOptions = {}, | ||
options: UseDataOptions = {}, | ||
): ActivePlugin | undefined => { | ||
const data = useAllDocsData(); | ||
const {pathname} = useLocation(); | ||
return getActivePlugin(data, pathname, options); | ||
}; | ||
|
||
export const useActivePluginAndVersion = ( | ||
options: GetActivePluginOptions = {}, | ||
options: UseDataOptions = {}, | ||
): | ||
| undefined | ||
| {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined} => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -544,7 +544,11 @@ This is the most convenient hook to access plugin global data and should be used | |
`pluginId` is optional if you don't use multi-instance plugins. | ||
|
||
```ts | ||
function usePluginData(pluginName: string, pluginId?: string); | ||
function usePluginData( | ||
pluginName: string, | ||
pluginId?: string, | ||
options?: {failfast?: boolean}, | ||
); | ||
``` | ||
|
||
Usage example: | ||
|
@@ -567,7 +571,10 @@ const MyComponent = () => { | |
Access global data created by a specific plugin. Given a plugin name, it returns the data of all the plugins instances of that name, by plugin id. | ||
|
||
```ts | ||
useAllPluginInstancesData(pluginName: string) | ||
function useAllPluginInstancesData( | ||
pluginName: string, | ||
options?: {failfast?: boolean}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it really an option that we want to expose publicly? 🤷♂️ |
||
); | ||
``` | ||
|
||
Usage example: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it could be possible to use TS overloads to avoid TS users to need to use ! despite the failfast option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically yes, but... err... at least not quite useful for us