Skip to content
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

Merged
merged 3 commits into from
May 22, 2019
Merged

feat(v2): create useBaseURL helper #1503

merged 3 commits into from
May 22, 2019

Conversation

yangshun
Copy link
Contributor

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.)

@yangshun yangshun requested a review from endiliey as a code owner May 22, 2019 07:20
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label May 22, 2019
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented May 22, 2019

Deploy preview for docusaurus-2 ready!

Built with commit 7f362cf

https://deploy-preview-1503--docusaurus-2.netlify.com

@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented May 22, 2019

Deploy preview for docusaurus-preview ready!

Built with commit 7f362cf

https://deploy-preview-1503--docusaurus-preview.netlify.com

@endiliey endiliey self-assigned this May 22, 2019
Copy link
Contributor

@endiliey endiliey left a 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

Example:
this PR


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.

new PR

@endiliey endiliey changed the title feat(v2): create withBaseURL helper feat(v2): create useBaseURL helper May 22, 2019
@endiliey
Copy link
Contributor

endiliey commented May 22, 2019

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 withBaseUrl in next PR

@endiliey endiliey merged commit 3fc3644 into master May 22, 2019
@endiliey endiliey deleted the withbaseurl branch May 22, 2019 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants