Skip to content

fix: stop registering sphinx-book-theme.js, which is never copied - #455

Merged
mmcky merged 3 commits into
mainfrom
fix/sphinx-book-theme-js-404
Oct 9, 2026
Merged

mmcky merged 3 commits into
mainfrom
fix/sphinx-book-theme-js-404

Conversation

@mmcky

@mmcky mmcky commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #454

What was wrong

Every page requested _static/scripts/sphinx-book-theme.js, which is never in the build, so it 404'd (live at https://jax.quantecon.org/_static/scripts/sphinx-book-theme.js; all 36 lecture pages in a lecture-jax build from main carry the tag). setup() calls app.setup_extension("sphinx_book_theme") for that package's Python features, and sphinx-book-theme's own setup() runs app.add_js_file("scripts/sphinx-book-theme.js"). The file lives in sphinx-book-theme's static folder, which is only copied when sphinx-book-theme is the active theme, so the script has never loaded on a quantecon-book-theme site.

The change

Straight after setup_extension, setup() removes that one entry from app.registry.js_files (in place). Nothing else changes: the script never loaded, and all other registered scripts are untouched.

Why the registry route, and why it holds

I checked this against Sphinx 7.4.7 and 8.2.3 (the theme allows >=7,<9):

  • app.add_js_file appends (filename, attrs) to app.registry.js_files in both versions, and also pushes straight to the builder if one already exists.
  • StandaloneHTMLBuilder.init() runs init_templates() and then init_js_files(). init_js_files() resets the builder's _js_files and rebuilds it from the registry: self.app.registry.js_files in 7.x, and self.env._registry.js_files in 8.x, where _registry is a property returning app.registry. So whatever the registry holds at that point is what pages get.
  • The theme's setup() always runs before init_js_files(), on both paths that load it. With html_theme = "quantecon_book_theme" it runs from the sphinx.html_themes entry point inside init_templates(). The direct push to the already-existing builder is then discarded by the reset. Via conf.py extensions, it runs before the builder exists.
  • Jupyter Book 1 lists sphinx_book_theme in its default extensions, so in lecture builds sphinx-book-theme's setup() runs first and our setup_extension call is a no-op. The entry is still in the registry when our filter runs, so it is removed either way. I confirmed both orders by wrapping the two setup() functions in a test build, on both Sphinx versions: after setup, neither the registry nor the builder's script list holds the script.
  • Sphinx 9.1.0 has the same registry.js_files shape and the same init_templates(), then init_js_files() order. I checked that from the source only, not with a build, since the theme caps Sphinx below 9.

I didn't use the alternative of filtering context["script_files"] in an html-page-context hook. It would run once per page rather than once per build, and it would need to handle the mixed str/_JavaScript entries that list can hold. app.registry is internal API, but it has had the same shape across 7.x, 8.x and 9.x, and the new test fails loudly if that ever changes.

sphinx-book-theme's hash_html_assets

It still lists scripts/sphinx-book-theme.js and is unaffected:

  • Its existence check looks in sphinx-book-theme's own package static folder, which ships the file, so it doesn't warn.
  • It looks for a script_files entry whose filename is exactly scripts/sphinx-book-theme.js, but the builder stores _static/scripts/sphinx-book-theme.js, so it never matched before this change and doesn't now. The undigested <script src="_static/scripts/sphinx-book-theme.js"> in today's output shows this; a match would have added a ?digest=.
  • Both new test builds and the full suite's builds run with -W and pass.

Tests

tests/test_script_files.py (marked build) builds tests/sites/base with -W twice: as-is, and with sphinx_book_theme appended to extensions to reproduce Jupyter Book 1's setup order. For each build it checks that:

  • no page requests scripts/sphinx-book-theme.js
  • every page still loads scripts/quantecon-book-theme.js
  • every local <script src> on every page resolves to a file in the build, the general form of this bug (before the fix the only unresolved ones were the 11 sphinx-book-theme.js tags)

Without the change the first and third checks fail in both builds (4 failures); with it all 6 pass, on Sphinx 7.4.7 and 8.2.3.

Those builds run setup() in a subprocess that coverage doesn't see, so codecov/patch first reported the new lines as uncovered. A second commit adds test_setup_drops_only_the_sphinx_book_theme_script, which runs setup() in-process against Sphinx's own SphinxComponentRegistry and checks that only the sphinx-book-theme.js entry is dropped. It fails against main's code and passes with the fix, on Sphinx 7.4.7 and 8.2.3.

Verification

  • Full suite: 223 passed on each of Python 3.12 and 3.13 × Sphinx 7.4.7 and 8.2.3 (sphinx-book-theme 1.1.4, pydata-sphinx-theme 0.15.4, myst-nb 1.4.0).
  • Whole-site diff of tests/sites/base built in the same environment before and after: each of the 11 pages loses exactly one line, the sphinx-book-theme.js script tag. _static/ is identical, and no other HTML changed.
  • Real Jupyter Book 1 build (jupyter-book 1.0.4.post1, Sphinx 7.4.7) of the pinned fixtures site, link-checked offline: before, 17 of 18 pages requested the missing script; after, 0 do, and quantecon-book-theme.js still loads on the same 17. No build warnings either way.
  • The regression fixtures in tests/test_build/ don't include the script tag and did not change.
  • No visual change is expected, since the script never loaded.
  • pre-commit (black, flake8, whitespace hooks) passes.

Merge note

#453 has merged, so main is merged into this branch (0465d67). The only conflict was the ### Fixed section both PRs added under [Unreleased] in CHANGELOG.md. It now holds both entries, #384's first, and against main this PR's CHANGELOG change is just its own line. The full suite passes on the merged tree: 227 tests on Python 3.12 and 3.13 × Sphinx 7.4.7 and 8.2.3.

🤖 Generated with Claude Code

setup() calls app.setup_extension("sphinx_book_theme") for that
package's Python features, and sphinx_book_theme's own setup() runs
app.add_js_file("scripts/sphinx-book-theme.js"). The file lives in
sphinx_book_theme's static folder, which is only copied when that is the
active theme, so every page requested a script that 404'd and has never
loaded.

Drop the entry from app.registry.js_files straight after
setup_extension. The HTML builder builds its script list from the
registry in init_js_files(), which runs after the theme's setup() on
both paths that load it: from the sphinx.html_themes entry point inside
init_templates(), and from conf.py extensions before the builder exists.
The second covers Jupyter Book 1, which lists sphinx_book_theme in its
default extensions so its setup() runs before ours.

Adds tests/test_script_files.py, which builds the base test site both
ways with -W and checks that no page requests the script, that
quantecon-book-theme.js is still loaded, and that every local script a
page requests exists.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

@github-actions
github-actions Bot temporarily deployed to pull request October 9, 2026 01:25 Inactive
@codecov

codecov Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@2d9bbe5). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #455   +/-   ##
=======================================
  Coverage        ?   59.48%           
=======================================
  Files           ?        2           
  Lines           ?      548           
  Branches        ?        0           
=======================================
  Hits            ?      326           
  Misses          ?      222           
  Partials        ?        0           
Flag Coverage Δ
pytests 59.48% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The fix is minimal and correctly targeted, its reliance on the internal app.registry.js_files shape is deliberately justified and guarded by a new build-marked regression test, and no objective issues were found in the change, test, or changelog.

0 open findings

What changed in this PR

This PR fixes issue #454, where every page built with the theme requested _static/scripts/sphinx-book-theme.js — a file that is never copied into the build (it lives in sphinx-book-theme's static folder, which is only copied when that is the active theme), producing a 404 on every page. The theme calls app.setup_extension("sphinx_book_theme") to obtain that package's Python features, and sphinx-book-theme's own setup() registers that script. The fix removes just that one registration from Sphinx's JS registry immediately after setting up the extension, so the script tag disappears and nothing else changes.

Changes:

  • In setup(), filter app.registry.js_files in place to drop the scripts/sphinx-book-theme.js entry right after setup_extension("sphinx_book_theme").
  • Add tests/test_script_files.py (marked build) that builds tests/sites/base twice (default, and with sphinx_book_theme appended to extensions to mimic Jupyter Book 1's load order) and asserts the script is no longer requested, quantecon-book-theme.js still loads, and every local script resolves to a built file.
  • Add a ### Fixed entry to CHANGELOG.md.
File Description
src/​quantecon_book_theme/​__init__.py Removes the scripts/sphinx-book-theme.js entry from the JS registry after setting up the sphinx-book-theme extension.
tests/​test_script_files.py New regression test verifying no page requests the missing script and all local scripts resolve, across both extension load orders.
CHANGELOG.md Documents the fix under [Unreleased] → Fixed.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

🎭 Visual Regression Test Results

passed  103 passed
skipped  3 skipped

Details

stats  106 tests across 1 suite
duration  3 minutes, 50 seconds
commit  0465d67

Skipped tests

desktop-chrome › theme.spec.ts › Visual Regression Tests › prob-matrix - full page screenshot
mobile-chrome › theme.spec.ts › Visual Regression Tests › prob-matrix - full page screenshot
mobile-chrome › theme.spec.ts › Theme Features › f-string interpolation styling

The build tests run setup() inside sphinx-build subprocesses, which
coverage does not see, so codecov/patch reported the new filter as
uncovered. Drive setup() with Sphinx's own SphinxComponentRegistry and
check that only the sphinx-book-theme.js entry is dropped. This also
pins the registry API the filter relies on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions
github-actions Bot temporarily deployed to pull request October 9, 2026 01:32 Inactive
Brings in #453. CHANGELOG.md conflicted because both PRs added a
### Fixed section under [Unreleased]; keep both entries in one section,
#384's first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch was previously deployed

1 inactive deployment
pull request — 0465d679 Deployed Oct 9, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Every page requests _static/scripts/sphinx-book-theme.js, which is never copied (404)

2 participants