-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds very sloppy live reloading funcitonality
- Loading branch information
1 parent
1b9fb01
commit 76dab6e
Showing
4 changed files
with
85 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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) => { | ||
return response.text(); | ||
}) | ||
.then((html) => { | ||
document.body.innerHTML = html; | ||
console.log("Reloaded"); | ||
}); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
|
||
startReload(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters