docsifyjs / docsifyjs/docsify

Page Ready Hook May Not Fire When loadSidebar is Set, but There is no Root Sidebar File

Open
#2,800 2 comments 0 reactions 1 assignee Claimed by @sy-records View on GitHub
bug
Dominant language
JavaScript
Stars
31.5k
Forks
5.8k
Avg merge
9d 8h
Merged PRs (30d)
2

Description

### Description

When the site has sidebars _only in sub-directories_, anything which relies on the "ready hook" may not work properly. For example, the [docsify-sidebar-collapse](https://github.com/iPeng6/docsify-sidebar-collapse) plugin, which attaches a click listener in response to the ready hook.

### Expected behavior

Anything which relies on the ready hook being fired after a sidebar loads should work when a sidebar is rendered, even if the initial site load does not render one.

In terms of an actual fix, it depends on the intent of the ready hook, and whether it makes sense to fire only when `loadNested()` finds the target file. Firing the ready hook regardless of whether a file is found will fix the particular issue with `docsify-sidebar-collapse`, but that may be the wrong solution.

It is also possible that `docsify-sidebar-collapse` should be using a different hook to attach its click listener.

### Actual behavior

When first loading the site, `loadNested()` never executes the passed in function `next`:
- on the first call, `path` is `/`
- there is no `/_sidebar.md`, so the call recurses
- on the next call, `path` is the empty string
- `loadNested()` exits without executing `next`

`next` is the function built by `_loadSideAndNav`, and the `cb()` call in that function ultimately triggers `this$1.callHook('ready');`.

When navigating to one of the sub-sections, the relevant sidebar is correctly rendered, and the sidebar links all work.

However, anything which relies on the "ready hook" may not work properly, such as the [docsify-sidebar-collapse](https://github.com/iPeng6/docsify-sidebar-collapse) plugin, which attaches a click listener in response to the ready hook. For example, the chevrons displayed by `docsify-sidebar-collapse` are meant to be clickable (to expand/collapse the relevant section), but this only works when the ready hook fires:

Image

Manually reloading a page which does have an associated sidebar resolves the issue, as the ready hook will correctly fire.

### Steps to reproduce

#### Setup

Create a site which uses the [docsify-sidebar-collapse](https://github.com/iPeng6/docsify-sidebar-collapse) plugin, and has sidebars _only in sub-directories_:
```
index.html
section-a/
_sidebar.md
page-a1.md
page-a2.md
...
section-b/
_sidebar.md
page-b1.md
page-b2.md
...
```
There only needs to be 1 sub-directory; the critical condition is that `loadSidebar` is set, but there is no root-level `_sidebar.md`.

In my local site, I use [docsify-navbar-fixed](https://gitlab.com/w3teal/docsify-navbar-fixed) with links to each of the sub-sections, rather than the landing page having a sidebar with the links, but this plugin is not necessary (e.g., `index.html` can include the links).

#### Steps
1. Load the site
2. Navigate to one of the subsections
3. Click on a chevron in the sidebar
- it should have no effect (this is the bug)
4. Reload the page
5. Click on a chevron in the sidebar
- it should now expand/collapse the associated section

### Environment

Issue observed in the following environments (other environments have not been tested):
- Windows and macOS
- Chrome and Safari
- Docsify 4.13.1 and 5.0.0
- docsify-sidebar-collapse (as this relies on the ready hook)

### Additional Information

- [ ] Bug still occurs when all/other plugins are disabled?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.