jekyll-watch icon indicating copy to clipboard operation
jekyll-watch copied to clipboard

Rebuild site on config changes

Open mpvosseller opened this issue 11 years ago • 8 comments

If I run jekyll serve --watch the site does not seem to rebuild when a configuration file (_config.yml) changes.

Ideally --watch could watch config files too.

mpvosseller avatar Aug 11 '14 17:08 mpvosseller

This is a larger structural change... right now we keep track of the site object and its configuration options are meant to be immutable within one process.

parkr avatar Nov 27 '14 08:11 parkr

I guess ideally jekyll serve --watch would just re-run jekyll build and prop up a server. It'd have to fail when source or dest are changed, but only for serve. For build, it could just call jekyll build repeatedly.

parkr avatar Nov 27 '14 08:11 parkr

But again, it's still a pretty hefty structural change. Hm.

parkr avatar Nov 27 '14 08:11 parkr

Is there some tool somehwere to automate ^C -> restart jekyll when _config.yml changes?

This is such a pain to work with, considering so many commercial templates for jekyll use _config.yml for bulk of layout and content.

maxenko avatar Aug 21 '18 23:08 maxenko

Not sure how much it helps but, was facing similar problem. I had all the data in _config.yaml file, moved it to data directory (https://jekyllrb.com/docs/datafiles/), now there was no need for it. I think this could be one of reason why data-dir were added(not sure though)

murarisumit avatar Jul 23 '19 05:07 murarisumit

I've written a daemon with functionality like this before. It watched its own config files, and did sanity checking on the loaded config file before updating its running configuration. If dealing with source and destination changes is too difficult for now, I think a reasonable short term solution would be comparing the keys in the new file and exiting if those values have changed. Or for a very hacky quick fix, exiting if the file changes at all. Continuing to run violates the user's expectations that the changes they made have taken effect.

ccope avatar Sep 21 '19 01:09 ccope

I'd also love this, as I also have Jekyll themes that have a lot of config in _config.yml. I've learnt to migrate it to a data file, but it's a hassle.

Surely the process could just restart itself when it detects a change to the config. Would that really need a lot of rearchitecting?

SimonEast avatar Sep 13 '21 09:09 SimonEast

Surely the process could just restart itself when it detects a change to the config. Would that really need a lot of rearchitecting?

I agree, it will not be a big effort to implement this way, but it will be a huge help (yes, not the best solution, indeed, but it would be useful if it had a separate option that would be turned off by default, it would be backward compatible too)

Anyway, I'm using this script to achieve the same, far from perfect, but it will do what it should.

#!/bin/bash

REF_FILE="./.reftime"
ALL_PARAMS=$@

start_process() {
    echo -e "\nStarting to serve...\n"

    reset_watcher

    rm -Rf _site

    bundle exec jekyll serve ${ALL_PARAMS} &
    PROC_PID=$!

    echo -e "\n\n\n"
}

stop_process() {
    echo -e "\nStopping to serve...\n"
    kill -SIGTERM ${PROC_PID}
    wait ${PROC_PID}  # Wait for the process to terminate
    rm -f "${REF_FILE}"
}

check_watched_file_changed() {
    RES="$(find "$1" -newer "${REF_FILE}")"
    if [ "${RES}x" == "x" ]; then
        return 1
    else
        return 0
    fi
}

reset_watcher() {
    touch "${REF_FILE}"
}

handle_file_changes() {
    # Space separated list of files to watch
    FILES_TO_WATCH=("./_config.yml")

      for FILE in "${FILES_TO_WATCH[@]}"; do
        if check_watched_file_changed "${FILE}" ; then
            echo -e "\nFile $file changed. Restarting to serve...\n"

            stop_process
            start_process

            reset_watcher
            break  # Break out of the loop after the first file change
        fi
    done
}

# Main loop
start_process

while true; do
    KEY=0
    # Check for keyboard input without blocking
    read -t 1 -n 1 KEY

    case ${KEY} in
        s)
            stop_process
            exit 0
            ;;
        r)
            stop_process
            start_process
            ;;
    esac

    handle_file_changes

    sleep 1
done

HofiOne avatar Jan 23 '24 19:01 HofiOne