just-the-docs / just-the-docs

A modern, high customizable, responsive Jekyll theme for documentation with built-in search.

Home Page:https://just-the-docs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Search functionality is not working out of the box, zzzz-search-data.json is getting rendered as a HTML page instead of a JSON file

5hwb opened this issue · comments

Describe the bug

When creating a Jekyll site and adding on the just-the-docs theme later on, the search functionality does not work, generating no output. Further investigation revealed that the search data JSON file (assets/js/zzzz-search-data.json) was being treated by the template engine as a HTML page and applying the associated HTML markup and styling to the entire file, causing the JSON to become unparsable and preventing Lunr.js from successfully starting.

Opening up the browser dev tools reveals an error Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.

There is a simple workaround which involves setting the layout to none in the front matter of assets/js/zzzz-search-data.json, preventing the template engine from rendering the JSON as a HTML page. Open assets/js/zzzz-search-data.json and replace the front matter:

---
permalink: /assets/js/search-data.json
---

with the following:

---
layout: none
permalink: /assets/js/search-data.json
---

To Reproduce
Steps to reproduce the behavior:

  1. Follow the Jekyll step-by-step tutorial to setup a Jekyll site
  2. In the site Gemfile, add the line gem "just-the-docs", "~> 0.8.2", the latest version as of 24 Apr 2024, then run bundle install
  3. Enable search as in the just-the-docs docs - set search_enabled = true in the project config at _config.yml
  4. Generate search index - bundle exec just-the-docs rake search:init
  5. Run the site locally - bundle exec jekyll serve
  6. Open the site and type anything in the search bar
  7. No results appear from the search.

Expected behavior
Search results should appear when typing in the search bar.

Screenshots
If applicable, add screenshots to help explain your problem.

Out of the box: Search functionality not working
image

What should happen: Search results show up after typing in search bar

image

Desktop (please complete the following information):

  • OS: macOS Sonoma 14.4.1
  • Browser: Firefox 125.0.2 (64-bit), Chrome 124.0.6367.62 (Official Build) (arm64)
  • Version: 0.8.2

Additional context

Ruby version is 3.3.0 and was installed via the asdf version management tool.

Hi @5hwb, thank you for submitting an issue! I appreciate your in-depth steps to reproduce this problem - and it makes complete sense (we just fixed a similar issue in #1447). In particular, a concise reproduction pattern is:

  1. clone the template repository
  2. add the following to _config.yml:
defaults:
  - scope:
      path: ""
    values:
      layout: "default"

So, your suggested solution should resolve this problem; I will use layout: null (as explained in #1447). I'll submit a PR shortly. Thanks!

No worries, glad to know it helped!