Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move javascript back to head #545

Merged
merged 13 commits into from
Dec 5, 2018
Merged

Move javascript back to head #545

merged 13 commits into from
Dec 5, 2018

Conversation

Blendify
Copy link
Member

@Blendify Blendify commented Jan 16, 2018

Fixes #328

{% endif %}
<script src="{{ pathto('_static/js/modernizr.min.js', 1) }}"></script>
<script type="text/javascript">
Copy link
Member Author

Choose a reason for hiding this comment

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

This can probably stay in the footer?

Copy link
Contributor

Choose a reason for hiding this comment

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

According to Paul Irish, in the footer is the way to go for performance, unless you target IE6-8 with the html5shiv: Modernizr/Modernizr#878 (comment)
The current Modernizr in the theme does include the shiv, so moving it out of head might break IE6-8. Possible change would be to put the shiv in head and the rest of Modernizr at the end of the file. This relates to #525.

Copy link
Member Author

Choose a reason for hiding this comment

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

This comment was directed towards the script below the modernizr link

Copy link
Contributor

Choose a reason for hiding this comment

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

Whoops. Yes, that should be at the bottom. It is the code that starts the theme JS logic and should be run when the page (or at least the toctree) is complete.

Copy link
Contributor

Choose a reason for hiding this comment

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

Putting JS in the footer is definitely better for performance in general but anything required for rendering should be in the header.

Not that we necessarily have to follow their lead, but Alabaster and the Spinx Basic theme (which Alabaster extends) both put JS in the head.

Copy link
Contributor

Choose a reason for hiding this comment

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

I do think that attempting to have consistency across themes isn't a bad thing though.

SOURCELINK_SUFFIX: '{{ sourcelink_suffix }}'
};
</script>
{%- for scriptfile in script_files %}
Copy link
Member Author

Choose a reason for hiding this comment

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

Add a comment saying not to use this? Use app.add_javascript instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

script_files is modified by search.html and the readthedocs-sphinx-ext, not sure what will happen if the theme removes this block.

Copy link
Member Author

Choose a reason for hiding this comment

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

The extension will have to be updated. Currently css_files is deprecated in sphinx-doc/sphinx@83d3fd2 and I asked about script_files in sphinx-doc/sphinx#4439

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe @ericholscher is planning to remove many of the script_files from readthedocs-sphinx-ext. However, who knows if others are using it.

Copy link
Member

Choose a reason for hiding this comment

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

I think we need to support people using them, since a lot of people use them. If we get to a point where Sphinx doesn't pass them anymore, then we can stop supporting them.

Copy link
Member Author

Choose a reason for hiding this comment

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

There seems to be some confusing here. Note that this WILL be removed by sphinx 2.0.0 so we should add a comment in the html for people reading through the code so they know what the better option is.

Copy link
Member

Choose a reason for hiding this comment

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

doesn't add_javascript just add it to script_files internally? So the public API is different, but for our theme it's the same API?

Copy link
Member Author

Choose a reason for hiding this comment

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

Looking at the builder yes, I am still a little unsure of what direction sphinx will really go with this, so I'll ask. Since this does not affect this PR we can come back to this later.

Copy link
Member Author

@Blendify Blendify Jan 18, 2018

Choose a reason for hiding this comment

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

See this thread for why this is deprecated. So basically it can only be used for reading, not writing to unless through the sphinx app API. So we can keep script_files and css_files but we should add a comment and include release notes that extra_css_files` is deprecated so that we can avoid this type of user action. Again, we should include this in a separate commit/PR.

Thread https://groups.google.com/forum/#!topic/sphinx-dev/bvnF6Grw224

@readthedocs readthedocs deleted a comment from jessetan Jan 16, 2018
@Blendify Blendify mentioned this pull request Jan 25, 2018
8 tasks
@Blendify
Copy link
Member Author

Poke. I say we forget about comments and just look at this from a functionality perspective.

Copy link
Member

@ericholscher ericholscher left a comment

Choose a reason for hiding this comment

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

This looks good to me. I'd want to test it a little bit on various RTD builds to make sure it doesn't blow things up, but otherwise I'm +1 on the change.

{# CSS #}

{# OPENSEARCH #}
{# Javascript -- Keepin in head #}
Copy link
Member

Choose a reason for hiding this comment

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

We can remove the second half of this comment. Feels silly.

Copy link
Contributor

@jessetan jessetan left a comment

Choose a reason for hiding this comment

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

LGTM assuming no breakages or slowdowns are experienced when testing with some larger projects on RTD.
Just throwing this out there, but how do you feel about making it a theme option to put the JS in the head? It is not needed, unless you're calling jQuery from inside rst like in #328. Putting JS in the head is risky for performance if large js libraries are included.

@Blendify
Copy link
Member Author

it should not change that much, we compress the js and we don't have that much js. I expect to only see a couple ms difference.

@Blendify
Copy link
Member Author

Blendify commented Feb 1, 2018

@ericholscher any word yet on performance differences?

@ericholscher
Copy link
Member

I'm not worried about performance, really. More about JS code people have written that depends on the current loading order, as well as the RTD specific code that we insert working with this loading order. I'll try and take a peek this week, but my week is pretty hectic.

@Blendify
Copy link
Member Author

Blendify commented Mar 7, 2018

Any word on how this works on RTD?

@astrojuanlu
Copy link
Contributor

I added some more info on why this would be useful here: readthedocs/readthedocs.org#4367 (comment)

@astrojuanlu
Copy link
Contributor

astrojuanlu commented Nov 3, 2018

I tried to rebase this locally but it's not obvious how to fix the conflicts without knowing the codebase. If @Blendify doesn't do it, I will use the old tip of the branch to see if it fixes readthedocs/readthedocs.org#4367, at least for me.

mgeier added a commit to mgeier/sphinx_rtd_theme that referenced this pull request Nov 4, 2018
It was moved away from <head> to the bottom of the page in readthedocs#78.

This is a compressed version of readthedocs#545 by @Blendify, which fixes readthedocs#328.
@mgeier
Copy link
Contributor

mgeier commented Nov 4, 2018

I've also tried to rebase this, but it was indeed too hard for me, so I created a new PR moving those lines around: #696.

@Blendify
Copy link
Member Author

Blendify commented Nov 4, 2018

I rebased the patch so it should work fine. If you encounter problems let me know.

@Blendify Blendify closed this Nov 4, 2018
@Blendify Blendify reopened this Nov 4, 2018
@Blendify
Copy link
Member Author

Blendify commented Nov 4, 2018

Wrong button oops...

@Blendify
Copy link
Member Author

Blendify commented Nov 4, 2018

@ericholscher, @davidfischer, @jessetan, @agjohnson can you take a review of this? It seems many people need this so would be nice to put in the next release.

@ericholscher ericholscher requested a review from a team November 5, 2018 15:43
@ericholscher
Copy link
Member

I think it makes sense (especially since we're seeing users impacted by this issue). I'll let @davidfischer or @agjohnson chime in with any other considerations, but I'm 👍

Copy link
Contributor

@davidfischer davidfischer left a comment

Choose a reason for hiding this comment

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

I had a few bits of feedback but this is close.

{%- if not READTHEDOCS %}
<script type="text/javascript" src="{{ pathto('_static/js/theme.js', 1) }}"></script>
{%- endif %}
<script src="{{ pathto('_static/js/modernizr.min.js', 1) }}"></script>
Copy link
Contributor

Choose a reason for hiding this comment

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

You definitely want modernizr before loading 3rd party scripts ({%- for scriptfile in script_files %}) so that people can use modernizr features in their scripts.

{% endif %}

{% endif %}
{# RTD hosts this file, so just load on non RTD builds #}
{%- if not READTHEDOCS %}
Copy link
Contributor

Choose a reason for hiding this comment

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

This if block should be removed. We are always including the theme.js now. See 41d251a#diff-1336f80431f476a232e3c48abb277c51.

@Blendify
Copy link
Member Author

Blendify commented Nov 5, 2018

Just noticed this but shouldn't we inject the javascript after css?

@davidfischer
Copy link
Contributor

Just noticed this but shouldn't we inject the javascript after css?

It don't think it matters.

@Blendify
Copy link
Member Author

Blendify commented Nov 5, 2018 via email

title="{% trans docstitle=docstitle|e %}Search within {{ docstitle }}{% endtrans %}"
href="{{ pathto('_static/opensearch.xml', 1) }}"/>
{%- endif %}
{%- endif %}

Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: the {# CSS #} comment can be readded here

<link rel="search" type="application/opensearchdescription+xml"
title="{% trans docstitle=docstitle|e %}Search within {{ docstitle }}{% endtrans %}"
href="{{ pathto('_static/opensearch.xml', 1) }}"/>
{# Javascript #}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Use {# JAVASCRIPT #} or {# SCRIPTS #} in all uppercase for consistency with other section comments

@jessetan
Copy link
Contributor

jessetan commented Nov 6, 2018

Thanks for spending some more time on this @Blendify.
I haven't seen any feedback on compatibility other JS code folks may be using, does that mean it works fine or that we didn't test it?
Otherwise LGTM with nits.

@Blendify Blendify merged commit a42c4d7 into master Dec 5, 2018
@Blendify Blendify deleted the java-head branch December 5, 2018 01:27
@astrojuanlu
Copy link
Contributor

astrojuanlu commented Dec 5, 2018

🎉 thanks!

@jessetan jessetan mentioned this pull request Oct 25, 2019
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.

jQuery not available in raw HTML directive
8 participants