rust-lang / mdBook

Create book from markdown files. Like Gitbook but implemented in Rust
https://rust-lang.github.io/mdBook/
Mozilla Public License 2.0
17.84k stars 1.62k forks source link

Load the sidebar toc from a shared JS file #2414

Open notriddle opened 2 months ago

notriddle commented 2 months ago

Before this change, the Rust unstable-book is 88MiB. With this change, it becomes 15MiB. Other pages might not be as extreme, but it's expected to help any book like this.

This change is so drastic because, if every chapter has a link to every other chapter, the result is O(n2) text output.

Related to : https://rust-lang.zulipchat.com/#narrow/stream/266220-t-rustdoc/topic/Tracking.20overall.20size/near/431831262

GuillaumeGomez commented 2 months ago

This is awesome, thanks!

GuillaumeGomez commented 2 months ago

I have merge rights but I'm not sure I'm supposed to do it. I'll ask the dev-tools team.

notriddle commented 2 months ago

It’s not just that. The unresolved question is if making the sidebar dependent on JS is okay.

GuillaumeGomez commented 2 months ago

That's something that was mentioned on zulip. Not a maintainer so only my point of view: we can still browse the book without JS (although less efficiently), so the regression is "ok". Maybe we can have a middle-ground where only the big sections are provided by default and the rest is filled by JS?

notriddle commented 2 months ago

Another option: include the TOC in index.html, but require JS for every other page.

GuillaumeGomez commented 2 months ago

That however would be pretty bad.

notriddle commented 2 months ago

Second commit has another solution to that problem: use an iframe. Those work without JavaScript.

I wind up using it as a fallback, instead of replacing the JS solution, because of Same-Origin-Policy shenanigans. Running out of a file:// URL will result in toc.html having separate localstorage from the main page, which isn't a problem when JS is disabled, but when it's enabled it would result in unsynchronized themes.