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

fix(v2): improve dx sidebar config, ability to have no sidebars file #4775

Merged
merged 4 commits into from
May 18, 2021

Conversation

nam-hle
Copy link
Contributor

@nam-hle nam-hle commented May 12, 2021

Motivation

From #4752.

What the PR does

  • Remove the default value of sidebarsPath
  • If specified, it can accept false (to disable the sidebars), undefined to generate automatically, the wrong path will report a warning message for example:

image

With docusaurus docs:version command:

  • false: copy docs files to versioned-docs only
  • undefined or wrong path: copy docs files to versioned-docs, create auto-generated version sidebars file to versioned-sidebars.

Have you read the Contributing Guidelines on pull requests?

Yes.

Test Plan

  • Unit tests.
  • Changes sidebarsPath in config file to various possible values and see the sidebar works as expected

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label May 12, 2021
@netlify
Copy link

netlify bot commented May 12, 2021

[V2]

Built with commit 0b82fd0

https://deploy-preview-4775--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented May 12, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 84
🟢 Accessibility 96
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-4775--docusaurus-2.netlify.app/

@nam-hle nam-hle force-pushed the improve-sidebar-config-dx branch from faff8c9 to 6e28fdf Compare May 13, 2021 14:54
@nam-hle nam-hle marked this pull request as ready for review May 14, 2021 01:01
@nam-hle nam-hle requested review from lex111 and slorber as code owners May 14, 2021 01:01
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that looks like a nice initial version!

Will push a few changes to your PR

@@ -93,7 +93,10 @@ export function cliDocsVersionCommand(
}

// Load current sidebar and create a new versioned sidebars file.
if (fs.existsSync(sidebarPath)) {
if (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be simpler to handle the false value in loadSidebars

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel the existsSync call is really useful btw, if the user provided a bad path an error is already displayed when he starts his site

@@ -249,7 +252,8 @@ export default function pluginContentDocs(
return {
...versionMetadata,
mainDocId: getMainDoc().unversionedId,
sidebars,
sidebars:
versionMetadata.sidebarFilePath === false ? undefined : sidebars,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed if loadSidebars handles false and return an empty object

sidebarFilePath: string | false | undefined,
): UnprocessedSidebars {
// In case false value, we have to set to DefaultSidebars first to generate navigation link
// See https://github.com/facebook/docusaurus/pull/4775
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you mean with that comment. Imho we just need to return an empty object if the value is false

Copy link
Contributor Author

@nam-hle nam-hle May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we return empty object, so there is no previous/next button in the end of the doc. That's why I need to do so.

Copy link
Collaborator

@slorber slorber May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but if the user uses false, there should be no navigation, I believe this is the expected behavior. There is only a navigation if there is a sidebar, and we should make sure that when false is used, there is no navigation

@@ -183,7 +182,9 @@ function getVersionMetadataPaths({
versionName,
});

const sidebarFilePath = isCurrentVersion
const sidebarFilePath = !options.sidebarPath
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this condition is correct, the "main test" should remain based on the isCurrentVersion. Versioned docs should always have a sidebar file available

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm actually not: versioned files can have no sidebars file, so we may compute a path for them but in the end the file may not exist and we shouldn't warn.

This will be better:

  function getSidebarFilePath() {
    if (isCurrentVersion) {
      return options.sidebarPath ? path.resolve(context.siteDir, options.sidebarPath) : options.sidebarPath
    } else {
      return path.join(
        getVersionedSidebarsDirPath(context.siteDir, options.id),
        `version-${versionName}-sidebars.json`,
      );
    }
  }

if (!fs.existsSync(sidebarFilePath)) {
console.log(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going to move this warning (and make it an error), to its original place, as we should throw in case of file not found, but not for the versioned docs (as a missing file is a possibility). Unfortunately in this function we don't have the current version name so it's not the best place to handle the error.

@netlify
Copy link

netlify bot commented May 18, 2021

[V1]

Built with commit 0b82fd0

https://deploy-preview-4775--docusaurus-1.netlify.app

@@ -20,6 +20,67 @@ import {
import {loadSidebars} from './sidebars';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';

function createVersionedSidebarFile({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI refactored and extracted a method here


// Do not create a useless versioned sidebars file if sidebars file is empty or sidebars are disabled/false)
const shouldCreateVersionedSidebarFile =
Object.keys(loadedSidebars).length > 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the important change: should we create a file in versioned-sidebars folder if the user has disabled the sidebars or is using an empty sidebars.json?

It used to not be created in some cases so for retrocompatibility I think it make sense to keep not creating it if it would lead to an empty file

@slorber
Copy link
Collaborator

slorber commented May 18, 2021

Thanks for your help on this @nam-hle ! Changed a few things and now it looks good to merge

@slorber slorber changed the title fix(v2): improve dx sidebar config fix(v2): improve dx sidebar config, ability to have no sidebars file May 18, 2021
@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label May 18, 2021
@slorber slorber merged commit 1ab8aa0 into facebook:master May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants