Skip to content

Commit

Permalink
remade live reload as long polling
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticspoon committed Jan 19, 2024
1 parent 76dab6e commit 3bc8932
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
52 changes: 10 additions & 42 deletions assets/reload.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
const regex = /<meta property="time_built" content="([^"]+)">/;
let lastModified;

async function startReload() {
setInterval(() => {
if (!lastModified) {
setLastModified();
reload();
// console.log("Initial reload");
return;
} else {
// console.log("Checking for reload");
reloadIfOld();
}
}, 1000);
}

function reload() {
fetch("/")
.then((response) => {
Expand All @@ -26,31 +9,16 @@ function reload() {
});
}

function reloadIfOld() {
getBuiltTime().then((timeBuilt) => {
// console.log("Last modified: " + lastModified);
if (timeBuilt > lastModified) {
// console.log("Reloading because of new build");
reload();
lastModified = timeBuilt;
}
});
}

function setLastModified() {
getBuiltTime().then((timeBuilt) => {
lastModified = timeBuilt;
});
}

async function getBuiltTime() {
let inputString = await fetch("/");
let text = await inputString.text();
let match = text.match(regex);
if (match && match[1]) {
const timeBuiltString = match[1];
return new Date(timeBuiltString);
async function pollServer() {
try {
console.log("Polling server...");
let response = await fetch("http://localhost:12345/");
reload();
pollServer();
} catch (err) {
console.log(err);
console.log("Server not ready. Reload the page with the server running.");
}
}

startReload();
pollServer();
36 changes: 36 additions & 0 deletions lib/local-server.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'webrick'
require 'socket'
require 'filewatcher'

class Server
Expand All @@ -7,10 +8,12 @@ class Server
def initialize(generator, cli_opts)
@opts = cli_opts
@generator = generator
@needs_reload = false
end

def start
watch_files
start_reload_server
start_local_server
end

Expand All @@ -25,10 +28,32 @@ def watch_files
fw.watch do |change|
puts "Change detected: #{change}" if opts.verbose
generator.send(:write_html)
@needs_reload = true
end
end
end

def start_reload_server
puts 'Starting reload server on port 12345'
@reload_thread = Thread.new do
reload_server
end
end

def reload_server
server = TCPServer.new('localhost', 12_345)

loop do
socket = server.accept
request = socket.gets
warn request
sleep 0.5 until @needs_reload
@needs_reload = false
socket.print(headers)
socket.close
end
end

def start_local_server
puts "Starting local server on port #{opts.port}"
root = opts.html_path
Expand All @@ -38,4 +63,15 @@ def start_local_server

server.start
end

private

def headers
headers = [
'HTTP/1.1 200 OK',
'Content-Type: text/html',
"Access-Control-Allow-Origin: http://localhost:#{opts.port}"
]
@headers ||= "#{headers.join("\r\n")}\r\n\r\n"
end
end

0 comments on commit 3bc8932

Please sign in to comment.