4

I am trying to write up a documentation using Sphinx in combination with the read the docs theme, but the search function seems to be broken.

I am running Python 3.7 with the latest version of Sphinx and the Read the Docs Theme (both installed with pip install) on Windows 10. After building the page and opening the index.html in the browser (tried Edge and Chrome) I am not able to search my offline documentation. The search page opens up but is only displaying the animation Search -> Search . -> Search .. -> Search …, If I change the theme back to a standard theme the search works without any problems.

As the search works for standard themes it does not seem to be a Browser related problem nor does it seem to be related to the fact that I am not hosting the page on localhost.

If I look at the errors in the console I see two errors

jquery.js:4 Failed to load file:///C:/user/documentation/_build/html/searchindex.js: 
Cross origin requests are only supported for protocol 
schemes: http, data, chrome, chrome-extension, https.

and

searchtools.js:144 Uncaught ReferenceError: Stemmer is not defined
at Object.query (searchtools.js:144)
at Object.setIndex (searchtools.js:83)
at searchindex.js:1

I could not find anything useful to solve this problem while searching online. Also refreshing the browser cache (Ctrl + F5) like described in https://github.com/rtfd/readthedocs.org/issues/4026 did not work.

Did anyone experience the same issue?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
New2Coding
  • 181
  • 13
  • 1
    Seems related: https://github.com/sphinx-doc/sphinx/issues/5460 – mzjn Sep 24 '18 at 07:20
  • @mzjn: Thank you for your comment. I already tried to install an older version of sphinx 1.7.9 but this did also not solve the problem. – New2Coding Sep 24 '18 at 07:40
  • If I download a ReadTheDocs documentation as HTML, the fact that the Search doesn't work is expected, right? – Unknow0059 Dec 21 '20 at 06:12

1 Answers1

2

I solved the problem by using the following solution (https://github.com/rtfd/sphinx_rtd_theme/pull/672/commits/4b9053cb9e805211f227399d66d82361b3e2bf56).

Credit goes to GitHub user @tk0miya.

First, navigate to the Python installation folder and locate the layout.html inside ./Lib/site-packages/sphinx_rtd_theme/layout.html.

Then replace line 191 - 204 containing

<script type="text/javascript"> 
        var DOCUMENTATION_OPTIONS = { 
            URL_ROOT:'{{ url_root }}', 
            VERSION:'{{ release|e }}', 
            LANGUAGE:'{{ language }}', 
            COLLAPSE_INDEX:false, 
            FILE_SUFFIX:'{{ '' if no_search_suffix else file_suffix }}', 
            HAS_SOURCE:  {{ has_source|lower }}, 
            SOURCELINK_SUFFIX: '{{ sourcelink_suffix }}' 
        }; 
    </script> 
    {%- for scriptfile in script_files %} 
      <script type="text/javascript" src="{{ pathto(scriptfile, 1) }}"></script> 
    {%- endfor %} 

by the following code

{% if sphinx_version >= "1.8.0" %} 
      <script type="text/javascript" id="documentation_options" data-url_root="{{ pathto('', 1) }}" src="{{ pathto('_static/documentation_options.js', 1) }}"></script> 
      {%- for scriptfile in script_files %} 
        {{ js_tag(scriptfile) }} 
      {%- endfor %} 
    {% else %} 
      <script type="text/javascript"> 
          var DOCUMENTATION_OPTIONS = { 
              URL_ROOT:'{{ url_root }}', 
              VERSION:'{{ release|e }}', 
              LANGUAGE:'{{ language }}', 
              COLLAPSE_INDEX:false, 
              FILE_SUFFIX:'{{ '' if no_search_suffix else file_suffix }}', 
              HAS_SOURCE:  {{ has_source|lower }}, 
              SOURCELINK_SUFFIX: '{{ sourcelink_suffix }}' 
          }; 
      </script> 
      {%- for scriptfile in script_files %} 
        <script type="text/javascript" src="{{ pathto(scriptfile, 1) }}"></script> 
      {%- endfor %} 
    {% endif %} 

Then run make clean and make html inside your documentation folder for the console. It is important that the console commands are executed inside your documentation folder.

New2Coding
  • 181
  • 13
  • I'm seeing the same symptom in `sphinx 6.2.1`, but the structure of `layout.html` has changed a bit over the past five years. I wonder how to find the current version of this fix? – MRule Jun 06 '23 at 15:35