jpanther / congo

A powerful, lightweight theme for Hugo built with Tailwind CSS.
https://jpanther.github.io/congo/
MIT License
1.24k stars 329 forks source link

Search not working when deployed to Netlify #223

Closed eliaserland closed 2 years ago

eliaserland commented 2 years ago

What happened?

Problem

The search bar on my website functions normally when served locally, but does not work when deployed to Netlify. When the search icon is clicked, the search bar appears seemingly as per normal, but it doesn't do anything, no matter what is typed into the search box.

Links

Website: https://utvik.netlify.app Repo: https://github.com/eliaserland/utviksbygdegard

Investigation

If I do an F12 in my browser I get this error message when I click the search icon: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://utvik.netlify.appindex.json/. (Reason: CORS request did not succeed). Status code: (null).

I do not entirely understand the meaning of this error message, but what I can notice is that there is a missing forward slash between the website URL utvik.netlify.app and the filename index.json, which likely seems to break this URL request.

If I trace this back to the congo repo, the only reference to index.json I can find is appearing in file assets/js/search.js on line 110 where I believe the search index is built:

function buildIndex() {
  var baseURL = wrapper.getAttribute("data-url");
  fetchJSON(baseURL + "index.json", function (data) {
    var options = {
      shouldSort: true,
      ignoreLocation: true,
      threshold: 0.0,
      includeMatches: true,
      keys: [
        { name: "title", weight: 0.8 },
        { name: "section", weight: 0.2 },
        { name: "summary", weight: 0.6 },
        { name: "content", weight: 0.4 },
      ],
    };
    fuse = new Fuse(data, options);
    indexed = true;
  });
}

There seems to be a concatenation of strings between baseURL and "index.json" happening, which is where I believe I would need a forward slash inserted in order for the URL request not to break.

I'm not certain that this will fix my issue of search not working, but it should however likely be a step in the right direction.

Since I don't know JavaScript, I don't feel comfortable enough to open a pull request where I have inserted a single / in front of the index.json string. Spontaneously, I feel like the rigorous solution should be something like verifying that the variable baseURL contains a single trailing forward slash, but I don't know enough JavaScript to implement this myself in a robust way.

Btw, thanks for a great theme with amazing documentation, this deserves major props.

Theme version

v2.2.2

Hugo version

v0.100.2 on Netlify, v0.96.0 locally

What browsers are you seeing the problem on?

Firefox

Relevant Hugo log output

1:26:42 PM: Build ready to start
1:26:44 PM: build-image version: a49edbc55fc722f80f6b85963a5060f8d043a844 (focal)
1:26:44 PM: build-image tag: v4.8.1
1:26:44 PM: buildbot version: f89dd42aa70d76d1da992dc3fa004a4c745208bc
1:26:44 PM: Fetching cached dependencies
1:26:45 PM: Finished downloading cache in 990.146902ms
1:26:45 PM: Starting to extract cache
1:26:47 PM: Finished extracting cache in 2.562187917s
1:26:47 PM: Finished fetching cache in 3.568853487s
1:26:47 PM: Starting to prepare the repo for build
1:26:48 PM: Preparing Git Reference refs/heads/master
1:26:49 PM: Parsing package.json dependencies
1:26:49 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'public' versus '/' in the Netlify UI
1:26:49 PM: Different build command detected, going to use the one specified in the Netlify configuration file: 'hugo --gc --minify -b $URL' versus '' in the Netlify UI
1:26:49 PM: Starting build script
1:26:49 PM: Installing dependencies
1:26:49 PM: Python version set to 2.7
1:26:50 PM: Started restoring cached node version
1:26:51 PM: Finished restoring cached node version
1:26:51 PM: v16.15.1 is already installed.
1:26:51 PM: Now using node v16.15.1 (npm v8.11.0)
1:26:52 PM: Started restoring cached build plugins
1:26:52 PM: Finished restoring cached build plugins
1:26:52 PM: Attempting ruby version 2.7.2, read from environment
1:26:53 PM: Using ruby version 2.7.2
1:26:53 PM: Using PHP version 8.0
1:26:53 PM: Installing Hugo 0.100.2
1:26:53 PM: hugo v0.100.2-d25cb2943fd94ecf781412aeff9682d5dc62e284+extended linux/amd64 BuildDate=2022-06-08T10:25:57Z VendorInfo=gohugoio
1:26:53 PM: Started restoring cached go cache
1:26:53 PM: Finished restoring cached go cache
1:26:53 PM: Installing Go version 1.16 (requested 1.16)
1:26:59 PM: unset GOOS;
1:26:59 PM: unset GOARCH;
1:26:59 PM: export GOROOT='/opt/buildhome/.gimme/versions/go1.16.linux.amd64';
1:26:59 PM: export PATH="/opt/buildhome/.gimme/versions/go1.16.linux.amd64/bin:${PATH}";
1:26:59 PM: go version >&2;
1:26:59 PM: export GIMME_ENV="/opt/buildhome/.gimme/env/go1.16.linux.amd64.env"
1:26:59 PM: go version go1.16 linux/amd64
1:26:59 PM: Installing missing commands
1:26:59 PM: Verify run directory
1:27:00 PM: ​
1:27:00 PM: ────────────────────────────────────────────────────────────────
1:27:00 PM:   Netlify Build                                                 
1:27:00 PM: ────────────────────────────────────────────────────────────────
1:27:00 PM: ​
1:27:00 PM: ❯ Version
1:27:00 PM:   @netlify/build 27.1.5
1:27:00 PM: ​
1:27:00 PM: ❯ Flags
1:27:00 PM:   baseRelDir: true
1:27:00 PM:   buildId: 62b1aaf212424e000adab42e
1:27:00 PM:   deployId: 62b1aaf212424e000adab430
1:27:00 PM: ​
1:27:00 PM: ❯ Current directory
1:27:00 PM:   /opt/build/repo
1:27:00 PM: ​
1:27:00 PM: ❯ Config file
1:27:00 PM:   /opt/build/repo/netlify.toml
1:27:00 PM: ​
1:27:00 PM: ❯ Context
1:27:00 PM:   production
1:27:00 PM: ​
1:27:00 PM: ────────────────────────────────────────────────────────────────
1:27:00 PM:   1. build.command from netlify.toml                            
1:27:00 PM: ────────────────────────────────────────────────────────────────
1:27:00 PM: ​
1:27:00 PM: $ hugo --gc --minify -b $URL
1:27:00 PM: Start building sites …
1:27:00 PM: hugo v0.100.2-d25cb2943fd94ecf781412aeff9682d5dc62e284+extended linux/amd64 BuildDate=2022-06-08T10:25:57Z VendorInfo=gohugoio
1:27:08 PM:                    | SV
1:27:08 PM: -------------------+-----
1:27:08 PM:   Pages            | 21
1:27:08 PM:   Paginator pages  |  0
1:27:08 PM:   Non-page files   |  6
1:27:08 PM: Creating deploy upload records
1:27:08 PM:   Static files     | 10
1:27:08 PM:   Processed images | 16
1:27:08 PM:   Aliases          |  3
1:27:08 PM:   Sitemaps         |  1
1:27:08 PM:   Cleaned          |  0
1:27:08 PM: Total in 7895 ms
1:27:08 PM: ​
1:27:09 PM: Starting post processing
1:27:08 PM: (build.command completed in 7.9s)
1:27:08 PM: ​
1:27:08 PM: ────────────────────────────────────────────────────────────────
1:27:09 PM: Post processing - HTML
1:27:08 PM:   2. Deploy site                                                
1:27:08 PM: ────────────────────────────────────────────────────────────────
1:27:08 PM: ​
1:27:08 PM: Starting to deploy site from 'public'
1:27:08 PM: Creating deploy tree 
1:27:09 PM: 0 new files to upload
1:27:09 PM: 0 new functions to upload
1:27:09 PM: Post processing - header rules
1:27:09 PM: Site deploy was successfully initiated
1:27:09 PM: ​
1:27:09 PM: (Deploy site completed in 266ms)
1:27:09 PM: ​
1:27:09 PM: Post processing - redirect rules
1:27:09 PM: ────────────────────────────────────────────────────────────────
1:27:09 PM:   Netlify Build Complete                                        
1:27:09 PM: ────────────────────────────────────────────────────────────────
1:27:09 PM: ​
1:27:09 PM: Post processing done
1:27:09 PM: (Netlify Build completed in 8.2s)
1:27:09 PM: Caching artifacts
1:27:09 PM: Started saving build plugins
1:27:09 PM: Finished saving build plugins
1:27:09 PM: Started saving pip cache
1:27:09 PM: Finished saving pip cache
1:27:09 PM: Started saving emacs cask dependencies
1:27:09 PM: Finished saving emacs cask dependencies
1:27:09 PM: Started saving maven dependencies
1:27:09 PM: Finished saving maven dependencies
1:27:09 PM: Started saving boot dependencies
1:27:09 PM: Finished saving boot dependencies
1:27:09 PM: Started saving rust rustup cache
1:27:09 PM: Finished saving rust rustup cache
1:27:09 PM: Started saving go dependencies
1:27:09 PM: Finished saving go dependencies
1:27:09 PM: Build script success
1:27:13 PM: Site is live ✨
1:27:26 PM: Finished processing build request in 42.472519809s
jpanther commented 2 years ago

Thanks for reporting this issue. I've been able to replicate it on the test Netlify build. Thanks also for submitting a PR with a fix!