diff --git a/sphinx/application.py b/sphinx/application.py index 44432f8a6..b42fb9dba 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -1030,9 +1030,6 @@ class Sphinx: kwargs['defer'] = 'defer' self.registry.add_js_file(filename, priority=priority, **kwargs) - if hasattr(self, 'builder') and hasattr(self.builder, 'add_js_file'): - self.builder.add_js_file(filename, - priority=priority, **kwargs) def add_css_file(self, filename: str, priority: int = 500, **kwargs: Any) -> None: """Register a stylesheet to include in the HTML output. @@ -1093,9 +1090,6 @@ class Sphinx: """ logger.debug('[app] adding stylesheet: %r', filename) self.registry.add_css_files(filename, priority=priority, **kwargs) - if hasattr(self, 'builder') and hasattr(self.builder, 'add_css_file'): - self.builder.add_css_file(filename, - priority=priority, **kwargs) def add_latex_package(self, packagename: str, options: str | None = None, after_hyperref: bool = False) -> None: diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 10e8ce0e2..1d0953cb8 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -498,9 +498,15 @@ class StandaloneHTMLBuilder(Builder): rellinks.append((indexname, indexcls.localname, '', indexcls.shortname)) + # add assets registered after ``Builder.init()``. + for css_filename, attrs in self.app.registry.css_files: + self.add_css_file(css_filename, **attrs) + for js_filename, attrs in self.app.registry.js_files: + self.add_js_file(js_filename or '', **attrs) + # back up _css_files and _js_files to allow adding CSS/JS files to a specific page. - self._orig_css_files = list(self._css_files) - self._orig_js_files = list(self._js_files) + self._orig_css_files = list(dict.fromkeys(self._css_files)) + self._orig_js_files = list(dict.fromkeys(self._js_files)) styles = list(self._get_style_filenames()) self.globalcontext = { diff --git a/sphinx/ext/mathjax.py b/sphinx/ext/mathjax.py index d620002d5..0131a9441 100644 --- a/sphinx/ext/mathjax.py +++ b/sphinx/ext/mathjax.py @@ -14,6 +14,7 @@ from docutils import nodes import sphinx from sphinx.application import Sphinx +from sphinx.builders.html import StandaloneHTMLBuilder from sphinx.domains.math import MathDomain from sphinx.errors import ExtensionError from sphinx.locale import _ @@ -79,6 +80,7 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict 'mathjax extension to work') domain = cast(MathDomain, app.env.get_domain('math')) + builder = cast(StandaloneHTMLBuilder, app.builder) if app.registry.html_assets_policy == 'always' or domain.has_equations(pagename): # Enable mathjax only if equations exists if app.config.mathjax2_config: @@ -87,10 +89,10 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict 'mathjax_config/mathjax2_config does not work ' 'for the current MathJax version, use mathjax3_config instead') body = 'MathJax.Hub.Config(%s)' % json.dumps(app.config.mathjax2_config) - app.add_js_file(None, type='text/x-mathjax-config', body=body) + builder.add_js_file('', type='text/x-mathjax-config', body=body) if app.config.mathjax3_config: body = 'window.MathJax = %s' % json.dumps(app.config.mathjax3_config) - app.add_js_file(None, body=body) + builder.add_js_file('', body=body) options = {} if app.config.mathjax_options: @@ -102,7 +104,7 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict else: # Load other MathJax via "async" method options['async'] = 'async' - app.add_js_file(app.config.mathjax_path, **options) + builder.add_js_file(app.config.mathjax_path, **options) def setup(app: Sphinx) -> dict[str, Any]: