Skip to content

Cleanup themes default/fallback values #3342

Closed

Description

@slorber

image

https://twitter.com/jaredpalmer/status/1298332787792109570

With the new theme validation/normalization, default values are applied ahead of time.

We should cleanup every piece of code that defines default/fallback values in the views unnecessarily because we don't want these fallback values to be duplicated everywhere, and it pollutes the code uselessly.

const {
  siteConfig: {
    themeConfig: { announcementBar: { id = "annoucement-bar" } = {} } = {}
  } = {}
} = useDocusaurusContext();

Here, we know that there's always a siteConfig, a themeConfig, and maybe an announcement bar, so it's not necessary to use = {} each time.

Also, we should strive to have proper TypeScript support. It is possible to have no announcementBar config, but I don't think we should destructure that config, falling back to {} just to ensure a runtime error.

In such case, the correct code should rather be:

const { announcementBar } = useDocusaurusContext().siteConfig.themeConfig;
if (announcementBar) {
  const {id,content} = announcementBar;
}

This announcementBar case has been fixed with #3338

But there are many other places where such cleanup should be done.

I suggest we should also:

  • Add more config normalization tests (as Joi is not always so idiomatic)
  • Add a useThemeConfig hook to reduce a bit boilerplate

Activity

  1. added
    bug
    status: needs triage
    better engineering
    difficulty: starter
    good first issue
    and removed
    status: needs triage
    on Aug 27, 2020
  2. imskr commented on Aug 27, 2020

    @imskr
    Contributor

    @slorber I would like to start working on this!

  3. slorber commented on Aug 28, 2020

    @slorber
    CollaboratorAuthor

    Please do @imskr :)

  4. imskr commented on Sep 3, 2020

    @imskr
    Contributor

    @slorber I have made a PR #3394

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    better engineeringbugdifficulty: startergood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions