Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit d3ff86e

Browse files
authored
Put the JS embed back into script_files (#92)
As the inline comment suggests, there are issues with putting the JS embed into the extrahead. A theme that puts scripts at the end of the body (none of the major themes do this) may not have loaded jquery before this script is executed and the script will error.
1 parent ea4643d commit d3ff86e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

readthedocs_ext/_templates/readthedocs-insert.html.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ READTHEDOCS_DATA = JSON.parse(document.getElementById('READTHEDOCS_DATA').innerH
2929
</script>
3030

3131
<script type="text/javascript" src="{{ rtd_analytics_url }}" async="async"></script>
32-
<script type="text/javascript" src="{{ rtd_js_url }}" async="async"></script>
3332

3433
<!-- end RTD <extrahead> -->

readthedocs_ext/readthedocs.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@ def update_body(app, pagename, templatename, context, doctree):
111111
else:
112112
app.add_css_file(theme_css)
113113

114+
# Add the Read the Docs embed
115+
# This *must* come after Sphinx has loaded jQuery as it relies on it.
116+
# Unless this script is modified to not rely on jQuery (a good goal),
117+
# it can't just be put into the extrahead
118+
# in case a theme outputs scripts at the end of the body
119+
js_file = '{}javascript/readthedocs-doc-embed.js'.format(STATIC_URL)
120+
if all((
121+
app.builder.name in ONLINE_BUILDERS,
122+
hasattr(app.builder, 'script_files'),
123+
js_file not in app.builder.script_files,
124+
)):
125+
if sphinx.version_info < (1, 8):
126+
app.builder.script_files.append(js_file)
127+
else:
128+
kwargs = {'async': 'async'} # Workaround reserved word in Py3.7
129+
app.add_js_file(js_file, **kwargs)
130+
114131
# This is monkey patched on the signal because we can't know what the user
115132
# has done with their `app.builder.templates` before now.
116133

@@ -157,7 +174,6 @@ def rtd_render(self, template, render_context):
157174
if ctx.get('proxied_api_host'):
158175
ctx['rtd_data']['proxied_api_host'] = ctx['proxied_api_host']
159176
ctx['rtd_css_url'] = '{}css/readthedocs-doc-embed.css'.format(STATIC_URL)
160-
ctx['rtd_js_url'] = '{}javascript/readthedocs-doc-embed.js'.format(STATIC_URL)
161177
ctx['rtd_analytics_url'] = '{}javascript/readthedocs-analytics.js'.format(STATIC_URL)
162178
source = os.path.join(
163179
os.path.abspath(os.path.dirname(__file__)),

0 commit comments

Comments
 (0)