Docfx: statictoc+ExtractSearchIndex is not working

Created on 25 Jul 2018  路  10Comments  路  Source: dotnet/docfx

DocFX Version Used:
2.37.1.0

Template used: (default or statictoc or contain custom template)
statictoc

Steps to Reproduce:

  1. Change template to statictoc.
  2. ExtractSearchIndex added to postProcessors
  3. docfx --serve and Check site
  4. Open _site/index.html in your browser and check.

Expected Behavior:
Search box is rendered and I can search without problems.

Actual Behavior:
Search box is not rendered.

When opening index.html directly, exceptions are also occurring.

DOMException: Failed to construct 'Worker': Script at 'file:///path/to/dir/_site/styles/search-worker.js' cannot be accessed from origin 'null'.

Area-Template

Most helpful comment

@mohaOujda Just to make sure do you use docfx serve instead of opening the HTML files directly? I think the latter will not work.

Isn't the point of statictoc to be able to open the generate HTML files locally without needing to use docfx serve? Is there a way to have the search work without serving?

All 10 comments

Hello!

I encountered the same problem as described above and made some efforts to find out the cause of the problem.

In the docfx.js file, the search gets enabled within the function loadNavbar, that is called by the renderNavbar function:

function renderNavbar() {
  var navbar = $('#navbar ul')[0];
  if (typeof (navbar) === 'undefined') {
    loadNavbar();
  } else {
    $('#navbar ul a.active').parents('li').addClass(active);
    renderBreadcrumb();
  }
}

function loadNavbar() {
  var navbarPath = $("meta[property='docfx\\:navrel']").attr("content");
  if (!navbarPath) {
    return;
  }
  navbarPath = navbarPath.replace(/\\/g, '/');
  var tocPath = $("meta[property='docfx\\:tocrel']").attr("content") || '';
  if (tocPath) tocPath = tocPath.replace(/\\/g, '/');
  $.get(navbarPath, function (data) {
    $(data).find("#toc>ul").appendTo("#navbar");
    if ($('#search-results').length !== 0) {
      $('#search').show();
      $('body').trigger("searchEvent");
    }
  ...
}

The renderNavbar function calls the loadNavbar function only in the default template, not in the statictoc template, because in the statictoc template the navbar items are already rendered. As a side effect the search is never activated properly.

The problem is quick-and-dirty-fixed by adding the following lines to renderNavbar function:

function renderNavbar() {
  var navbar = $('#navbar ul')[0];
  if (typeof (navbar) === 'undefined') {
    loadNavbar();
  } else {
    $('#navbar ul a.active').parents('li').addClass(active);
    renderBreadcrumb();

    // Copy-added from the loadNavbar function
    if ($('#search-results').length !== 0) {
      $('#search').show();
      $('body').trigger("searchEvent");
    }
  }
}

Nevertheless this should be fixed in the template. If I find the time, I will try to locate it, fix it and submit a pull request.

Hey!

I fixed the issue here: https://github.com/ZickZakk/docfx/commit/2b252d1b6bdd334394dc3523bd6358bc3abd1a04

@yufeih Would it be okay for you to submit this as a pull request?

@ZickZakk Of course, it is welcomed!

Hey,

I had a similar problem with the previous version now the problem is solved partially.
The search textbox is displayed but the search feature doesn't work.

Do you have any idea on how to solve this problem?

Thank you

@mohaOujda Just to make sure do you use docfx serve instead of opening the HTML files directly? I think the latter will not work.

Thank superyyrrzz for your response, it's works :)

Hey,

The search works only when you input the complete word and not a part of the word.
Is there a settings to activate for this search option?

Thank you

@mohaOujda There is no existing option now.

Hi, how can I use the search with the static version (without serve)? I got no results.

@mohaOujda Just to make sure do you use docfx serve instead of opening the HTML files directly? I think the latter will not work.

Isn't the point of statictoc to be able to open the generate HTML files locally without needing to use docfx serve? Is there a way to have the search work without serving?

Was this page helpful?
0 / 5 - 0 ratings