javascriptcachingjekyll

Jekyll not serving files correctly when manually refreshing page


When running command jekyll serve --no-watch . to run a server for testing, whenever I make changes to "test.txt" and refresh the page, the changes are not captured (for example, when "hello" is in test.txt and I erase the text and refresh the page, "hello" is still present). Is there a way to reverse this behavior? I understand that --no-watch means the site won't automatically regenerate when files change, however, my understanding is that it should still serve the files correctly when manually refreshing the page. Code snippet below:

<script>
  function bannerUpdate() {
    const url = 'http://127.0.0.1:4000/test.txt'
    let bannerText = ''

    let bannerInit = {
      method: 'GET',
      cache: 'no-cache',
    };

    fetch(url, bannerInit)
      .then(response => {
        response.text().then(text => {
          bannerText = text;
          console.log("banner text is: "+bannerText);
          done();
        });
      });

    function done() {
      banner.textContent = bannerText;
      if (bannerText.trim() !== '') {
        banner.style.backgroundColor = '#fff691';
        banner.textContent = bannerText;
        banner.style.display = 'block';
      }
    }
  }

  window.onload = bannerUpdate;
</script>

Solution

  • When you run jekyll serve the site gets built and the output (all the necessary html, css, etc. files) get copied to the _site folder. Jekyll's web server is serving out of the _site folder.

    By default when Jekyll detects that a file has been changed it copies the changed to the _site folder. That behavior allows you to see the changes without having to re-run the jekyll serve command.

    When you use the --no-watch option Jekyll will no longer copy changed files to the _site. It only copies the files once when you start the server.

    You are probably making changes to test.txt, which is in your root, but Jekyll is serving the copy of the original file that is located at _site/test.txt.

    While it is not advisable, you could technically make your changes to _site/test.txt directly.

    Keep in mind that your changes will be overwritten next time you run jekyll serve or jekyll build.

    ├── _includes
    ├── _layouts
    ├── _posts
    ├── _sass
    ├── _site
    │   ├── assets
    │   ├── css
    │   │   └── style.css
    │   ├── index.html
    │   ├── feed.xml
    │   ├── script.js
    │   ├── sitemap.xml
    │   └── test.txt        <-- File that is getting served
    ├── assets
    ├── index.html
    └── test.txt            <-- File you are editing