"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([["70093"],{71209:function(e,n,s){s.r(n),s.d(n,{frontMatter:()=>l,default:()=>u,contentTitle:()=>a,assets:()=>c,toc:()=>d,metadata:()=>t});var t=JSON.parse('{"id":"docusaurus-core","title":"Docusaurus Client API","description":"Docusaurus provides some APIs on the clients that can be helpful to you when building your site.","source":"@site/docs/docusaurus-core.mdx","sourceDirName":".","slug":"/docusaurus-core","permalink":"/docs/docusaurus-core","draft":false,"unlisted":false,"editUrl":"https://github.com/facebook/docusaurus/edit/main/website/docs/docusaurus-core.mdx","tags":[],"version":"current","lastUpdatedBy":"pedenys","lastUpdatedAt":1741859625000,"frontMatter":{"sidebar_label":"Client API"},"sidebar":"api","previous":{"title":"CLI","permalink":"/docs/cli"},"next":{"title":"docusaurus.config.js","permalink":"/docs/api/docusaurus-config"}}'),i=s(85893),r=s(80980),o=s(39468);let l={sidebar_label:"Client API"},a="Docusaurus Client API",c={},d=[{value:"Components",id:"components",level:2},{value:"<ErrorBoundary />",id:"errorboundary",level:3},{value:"Props",id:"errorboundary-props",level:4},{value:"<Head/>",id:"head",level:3},{value:"<Link/>",id:"link",level:3},{value:"to: string",id:"to-string",level:4},{value:"<Redirect/>",id:"redirect",level:3},{value:"<BrowserOnly/>",id:"browseronly",level:3},{value:"Props",id:"browseronly-props",level:4},{value:"Example with code",id:"browseronly-example-code",level:4},{value:"Example with a library",id:"browseronly-example-library",level:4},{value:"<Interpolate/>",id:"interpolate",level:3},{value:"Props",id:"interpolate-props",level:4},{value:"<Translate/>",id:"translate",level:3},{value:"Props",id:"translate-props",level:4},{value:"Example",id:"example",level:4},{value:"Hooks",id:"hooks",level:2},{value:"useDocusaurusContext",id:"useDocusaurusContext",level:3},{value:"useIsBrowser",id:"useIsBrowser",level:3},{value:"useBaseUrl",id:"useBaseUrl",level:3},{value:"Options",id:"options",level:4},{value:"Example usage:",id:"example-usage",level:4},{value:"useBaseUrlUtils",id:"useBaseUrlUtils",level:3},{value:"useGlobalData",id:"useGlobalData",level:3},{value:"usePluginData",id:"usePluginData",level:3},{value:"useAllPluginInstancesData",id:"useAllPluginInstancesData",level:3},{value:"useBrokenLinks",id:"useBrokenLinks",level:3},{value:"Functions",id:"functions",level:2},{value:"interpolate",id:"interpolate-1",level:3},{value:"Signature",id:"signature",level:4},{value:"Example",id:"example-1",level:4},{value:"translate",id:"translate-imperative",level:3},{value:"Signature",id:"signature-1",level:4},{value:"Example",id:"example-2",level:4},{value:"Modules",id:"modules",level:2},{value:"ExecutionEnvironment",id:"executionenvironment",level:3},{value:"constants",id:"constants",level:3}];function h(e){let n={a:"a",admonition:"admonition",code:"code",h1:"h1",h2:"h2",h3:"h3",h4:"h4",header:"header",li:"li",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,r.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.header,{children:(0,i.jsx)(n.h1,{id:"docusaurus-client-api",children:"Docusaurus Client API"})}),"\n",(0,i.jsx)(n.p,{children:"Docusaurus provides some APIs on the clients that can be helpful to you when building your site."}),"\n",(0,i.jsx)(n.h2,{id:"components",children:"Components"}),"\n",(0,i.jsx)(n.h3,{id:"errorboundary",children:(0,i.jsx)(n.code,{children:""})}),"\n",(0,i.jsxs)(n.p,{children:["This component creates a ",(0,i.jsx)(n.a,{href:"https://reactjs.org/docs/error-boundaries.html",children:"React error boundary"}),"."]}),"\n",(0,i.jsx)(n.p,{children:"Use it to wrap components that might throw, and display a fallback when that happens instead of crashing the whole app."}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-jsx",children:"import React from 'react';\nimport ErrorBoundary from '@docusaurus/ErrorBoundary';\n\nconst SafeComponent = () => (\n (\n
\n
This component crashed because of error: {error.message}.
\n \n
\n )}>\n \n \n);\n"})}),"\n","\n",(0,i.jsx)(n.admonition,{type:"tip",children:(0,i.jsxs)(n.p,{children:["To see it in action, click here: ",(0,i.jsx)(o.Z,{})]})}),"\n",(0,i.jsx)(n.admonition,{type:"info",children:(0,i.jsx)(n.p,{children:"Docusaurus uses this component to catch errors within the theme's layout, and also within the entire app."})}),"\n",(0,i.jsx)(n.admonition,{type:"note",children:(0,i.jsx)(n.p,{children:"This component doesn't catch build-time errors and only protects against client-side render errors that can happen when using stateful React components."})}),"\n",(0,i.jsx)(n.h4,{id:"errorboundary-props",children:"Props"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"fallback"}),": an optional render callback returning a JSX element. It will receive an object with 2 attributes: ",(0,i.jsx)(n.code,{children:"error"}),", the error that was caught, and ",(0,i.jsx)(n.code,{children:"tryAgain"}),", a function (",(0,i.jsx)(n.code,{children:"() => void"}),") callback to reset the error in the component and try rendering it again. If not present, ",(0,i.jsx)(n.code,{children:"@theme/Error"})," will be rendered instead. ",(0,i.jsx)(n.code,{children:"@theme/Error"})," is used for the error boundaries wrapping the site, above the layout."]}),"\n"]}),"\n",(0,i.jsx)(n.admonition,{type:"warning",children:(0,i.jsxs)(n.p,{children:["The ",(0,i.jsx)(n.code,{children:"fallback"})," prop is a callback, and ",(0,i.jsx)(n.strong,{children:"not a React functional component"}),". You can't use React hooks inside this callback."]})}),"\n",(0,i.jsx)(n.h3,{id:"head",children:(0,i.jsx)(n.code,{children:"
"})}),"\n",(0,i.jsxs)(n.p,{children:["This reusable React component will manage all of your changes to the document head. It takes plain HTML tags and outputs plain HTML tags and is beginner-friendly. It is a wrapper around ",(0,i.jsx)(n.a,{href:"https://github.com/nfl/react-helmet",children:"React Helmet"}),"."]}),"\n",(0,i.jsx)(n.p,{children:"Usage Example:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-jsx",children:'import React from \'react\';\n// highlight-next-line\nimport Head from \'@docusaurus/Head\';\n\nconst MySEO = () => (\n // highlight-start\n
\n \n\n'})}),"\n",(0,i.jsx)(n.h3,{id:"link",children:(0,i.jsx)(n.code,{children:""})}),"\n",(0,i.jsxs)(n.p,{children:["This component enables linking to internal pages as well as a powerful performance feature called preloading. Preloading is used to prefetch resources so that the resources are fetched by the time the user navigates with this component. We use an ",(0,i.jsx)(n.code,{children:"IntersectionObserver"})," to fetch a low-priority request when the ",(0,i.jsx)(n.code,{children:""})," is in the viewport and then use an ",(0,i.jsx)(n.code,{children:"onMouseOver"})," event to trigger a high-priority request when it is likely that a user will navigate to the requested resource."]}),"\n",(0,i.jsxs)(n.p,{children:["The component is a wrapper around react-router\u2019s ",(0,i.jsx)(n.code,{children:""})," component that adds useful enhancements specific to Docusaurus. All props are passed through to react-router\u2019s ",(0,i.jsx)(n.code,{children:""})," component."]}),"\n",(0,i.jsxs)(n.p,{children:["External links also work, and automatically have these props: ",(0,i.jsx)(n.code,{children:'target="_blank" rel="noopener noreferrer"'}),"."]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-jsx",children:"import React from 'react';\n// highlight-next-line\nimport Link from '@docusaurus/Link';\n\nconst Page = () => (\n
\n
\n {/* highlight-next-line */}\n Check out my blog!\n
\n
\n {/* highlight-next-line */}\n Follow me on X!\n
;\n // highlight-end\n};\n"})}),"\n",(0,i.jsx)(n.h3,{id:"useBrokenLinks",children:(0,i.jsx)(n.code,{children:"useBrokenLinks"})}),"\n",(0,i.jsx)(n.p,{children:"React hook to access the Docusaurus broken link checker APIs, exposing a way for a Docusaurus pages to report and collect their links and anchors."}),"\n",(0,i.jsxs)(n.admonition,{type:"warning",children:[(0,i.jsxs)(n.p,{children:["This is an ",(0,i.jsx)(n.strong,{children:"advanced"})," API that ",(0,i.jsx)(n.strong,{children:"most Docusaurus users don't need to use directly"}),"."]}),(0,i.jsxs)(n.p,{children:["It is already ",(0,i.jsx)(n.strong,{children:"built-in"})," in existing high-level components:"]}),(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:["the ",(0,i.jsx)(n.a,{href:"#link",children:(0,i.jsx)(n.code,{children:""})})," component will collect links for you"]}),"\n",(0,i.jsxs)(n.li,{children:["the ",(0,i.jsx)(n.code,{children:"@theme/Heading"})," (used for Markdown headings) will collect anchors"]}),"\n"]}),(0,i.jsxs)(n.p,{children:["Use ",(0,i.jsx)(n.code,{children:"useBrokenLinks()"})," if you implement your own ",(0,i.jsx)(n.code,{children:""})," or ",(0,i.jsx)(n.code,{children:""})," component."]})]}),"\n",(0,i.jsx)(n.p,{children:"Usage example:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-js",metastring:'title="MyHeading.js"',children:"import useBrokenLinks from '@docusaurus/useBrokenLinks';\n\nexport default function MyHeading(props) {\n useBrokenLinks().collectAnchor(props.id);\n return ;\n}\n"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-js",metastring:'title="MyLink.js"',children:"import useBrokenLinks from '@docusaurus/useBrokenLinks';\n\nexport default function MyLink(props) {\n useBrokenLinks().collectLink(props.href);\n return ;\n}\n"})}),"\n",(0,i.jsx)(n.h2,{id:"functions",children:"Functions"}),"\n",(0,i.jsx)(n.h3,{id:"interpolate-1",children:(0,i.jsx)(n.code,{children:"interpolate"})}),"\n",(0,i.jsxs)(n.p,{children:["The imperative counterpart of the ",(0,i.jsx)(n.a,{href:"#interpolate",children:(0,i.jsx)(n.code,{children:""})})," component."]}),"\n",(0,i.jsx)(n.h4,{id:"signature",children:"Signature"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-ts",children:"// Simple string interpolation\nfunction interpolate(text: string, values: Record): string;\n\n// JSX interpolation\nfunction interpolate(\n text: string,\n values: Record,\n): ReactNode;\n"})}),"\n",(0,i.jsx)(n.h4,{id:"example-1",children:"Example"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-js",children:"// highlight-next-line\nimport {interpolate} from '@docusaurus/Interpolate';\n\nconst message = interpolate('Welcome {firstName}', {firstName: 'S\xe9bastien'});\n"})}),"\n",(0,i.jsx)(n.h3,{id:"translate-imperative",children:(0,i.jsx)(n.code,{children:"translate"})}),"\n",(0,i.jsxs)(n.p,{children:["The imperative counterpart of the ",(0,i.jsx)(n.a,{href:"#translate",children:(0,i.jsx)(n.code,{children:""})})," component. Also supporting ",(0,i.jsx)(n.a,{href:"#interpolate",children:"placeholders interpolation"}),"."]}),"\n",(0,i.jsxs)(n.admonition,{type:"tip",children:[(0,i.jsxs)(n.p,{children:["Use the imperative API for the ",(0,i.jsx)(n.strong,{children:"rare cases"})," where a ",(0,i.jsx)(n.strong,{children:"component cannot be used"}),", such as:"]}),(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:["the page ",(0,i.jsx)(n.code,{children:"title"})," metadata"]}),"\n",(0,i.jsxs)(n.li,{children:["the ",(0,i.jsx)(n.code,{children:"placeholder"})," props of form inputs"]}),"\n",(0,i.jsxs)(n.li,{children:["the ",(0,i.jsx)(n.code,{children:"aria-label"})," props for accessibility"]}),"\n"]})]}),"\n",(0,i.jsx)(n.h4,{id:"signature-1",children:"Signature"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-ts",children:"function translate(\n translation: {message: string; id?: string; description?: string},\n values: Record,\n): string;\n"})}),"\n",(0,i.jsx)(n.h4,{id:"example-2",children:"Example"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-jsx",metastring:'title="src/pages/index.js"',children:"import React from 'react';\nimport Layout from '@theme/Layout';\n\n// highlight-next-line\nimport {translate} from '@docusaurus/Translate';\n\nexport default function Home() {\n return (\n \n \n \n );\n}\n"})}),"\n",(0,i.jsx)(n.h2,{id:"modules",children:"Modules"}),"\n",(0,i.jsx)(n.h3,{id:"executionenvironment",children:(0,i.jsx)(n.code,{children:"ExecutionEnvironment"})}),"\n",(0,i.jsx)(n.p,{children:"A module that exposes a few boolean variables to check the current rendering environment."}),"\n",(0,i.jsx)(n.admonition,{type:"warning",children:(0,i.jsxs)(n.p,{children:["For React rendering logic, use ",(0,i.jsx)(n.a,{href:"#useIsBrowser",children:(0,i.jsx)(n.code,{children:"useIsBrowser()"})})," or ",(0,i.jsx)(n.a,{href:"#browseronly",children:(0,i.jsx)(n.code,{children:""})})," instead."]})}),"\n",(0,i.jsx)(n.p,{children:"Example:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-js",children:"import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';\n\nif (ExecutionEnvironment.canUseDOM) {\n require('lib-that-only-works-client-side');\n}\n"})}),"\n",(0,i.jsxs)(n.table,{children:[(0,i.jsx)(n.thead,{children:(0,i.jsxs)(n.tr,{children:[(0,i.jsx)(n.th,{children:"Field"}),(0,i.jsx)(n.th,{children:"Description"})]})}),(0,i.jsxs)(n.tbody,{children:[(0,i.jsxs)(n.tr,{children:[(0,i.jsx)(n.td,{children:(0,i.jsx)(n.code,{children:"ExecutionEnvironment.canUseDOM"})}),(0,i.jsxs)(n.td,{children:[(0,i.jsx)(n.code,{children:"true"})," if on client/browser, ",(0,i.jsx)(n.code,{children:"false"})," on Node.js/prerendering."]})]}),(0,i.jsxs)(n.tr,{children:[(0,i.jsx)(n.td,{children:(0,i.jsx)(n.code,{children:"ExecutionEnvironment.canUseEventListeners"})}),(0,i.jsxs)(n.td,{children:[(0,i.jsx)(n.code,{children:"true"})," if on client and has ",(0,i.jsx)(n.code,{children:"window.addEventListener"}),"."]})]}),(0,i.jsxs)(n.tr,{children:[(0,i.jsx)(n.td,{children:(0,i.jsx)(n.code,{children:"ExecutionEnvironment.canUseIntersectionObserver"})}),(0,i.jsxs)(n.td,{children:[(0,i.jsx)(n.code,{children:"true"})," if on client and has ",(0,i.jsx)(n.code,{children:"IntersectionObserver"}),"."]})]}),(0,i.jsxs)(n.tr,{children:[(0,i.jsx)(n.td,{children:(0,i.jsx)(n.code,{children:"ExecutionEnvironment.canUseViewport"})}),(0,i.jsxs)(n.td,{children:[(0,i.jsx)(n.code,{children:"true"})," if on client and has ",(0,i.jsx)(n.code,{children:"window.screen"}),"."]})]})]})]}),"\n",(0,i.jsx)(n.h3,{id:"constants",children:(0,i.jsx)(n.code,{children:"constants"})}),"\n",(0,i.jsx)(n.p,{children:"A module exposing useful constants to client-side theme code."}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-js",children:"import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';\n"})}),"\n",(0,i.jsxs)(n.table,{children:[(0,i.jsx)(n.thead,{children:(0,i.jsxs)(n.tr,{children:[(0,i.jsx)(n.th,{children:"Named export"}),(0,i.jsx)(n.th,{children:"Value"})]})}),(0,i.jsx)(n.tbody,{children:(0,i.jsxs)(n.tr,{children:[(0,i.jsx)(n.td,{children:(0,i.jsx)(n.code,{children:"DEFAULT_PLUGIN_ID"})}),(0,i.jsx)(n.td,{children:(0,i.jsx)(n.code,{children:"default"})})]})})]})]})}function u(e={}){let{wrapper:n}={...(0,r.a)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(h,{...e})}):h(e)}},39468:function(e,n,s){s.d(n,{Z:()=>r});var t=s(85893),i=s(67294);function r(e){let{children:n="Boom!",message:s="Boom!\nSomething bad happened, but you can try again!",cause:r}=e,[o,l]=(0,i.useState)(!1);if(o)throw Error(s,{cause:r?Error(r):void 0});return(0,t.jsx)("button",{className:"button button--danger",type:"button",onClick:()=>l(!0),children:n})}},80980:function(e,n,s){s.d(n,{Z:()=>l,a:()=>o});var t=s(67294);let i={},r=t.createContext(i);function o(e){let n=t.useContext(r);return t.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function l(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:o(e.components),t.createElement(r.Provider,{value:n},e.children)}}}]);