-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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(v2): create useBaseURL helper #1503
Conversation
Deploy preview for docusaurus-2 ready! Built with commit 7f362cf |
Deploy preview for docusaurus-preview ready! Built with commit 7f362cf |
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 reworked the implementation a little bit and add more typescript definition for some file
The main change is: Instead of directly importing siteConfig from @generated/docusaurus.config.js
, I consume the siteconfig through React Context.
Why so ?
There are lot of reasons for that, mainly because of performance in development.
Notice this scenario:
During development server. If docusaurus.config.js
is changed, anything that consume it with `import siteConfig from '@generated/siteconfig' will be hot module disposed.
And withBaseUrl
previously was directly importing that file. So if there are 100 javascript files that call import withBaseUrl from '@docusaurus/withBaseUrl
, when the config file changes, 100 files will be replaced by the HMR
However this is not the case if we consume siteConfig through React context. Any JS files can still do import withBaseUrl from '@docusaurus/withBaseUrl'
syntax, but under the hood the withBaseUrl is consuming the config through context.
If docusaurus.config.js changes, react just re-render.
The syntax is still the same import useBaseUrl from '@docusaurus/useBaseUrl';
// anyywhere in your code
const url = useBaseUrl('img/undraw_around_the_world.svg'); Edit: Renamed back to |
Motivation
Create a
withBaseURL
helper that appends the base URL to a given path so that it's easier to work with asset paths and page paths.Have you read the Contributing Guidelines on pull requests?
Yes
Test Plan
Jest + Netlify
Related PRs
(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)