I would like to have the last updated date on my website, but I want the date information to be automatically updated every time I push my changes to GitHub, rather than remembering to manually change it.
Right now my site only uses HTML, CSS, and JavaScript. I don't know if the problem can be solved using these technologies, or if it's possible for a Git commit or push to trigger something.
UPDATE:
Solved by @k3llydev using GitHub API. Codepen example with updates to achieve what I'm looking for.
const desiredRepo = "seachel.github.io";
const dateTagClass = ".date";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
let repos = JSON.parse(this.responseText);
repos.forEach((repo)=>{
if (repo.name == desiredRepo)
{
var lastUpdated = new Date(repo.updated_at);
var day = lastUpdated.getUTCDate();
var month = lastUpdated.getUTCMonth();
var year = lastUpdated.getUTCFullYear();
$(dateTagClass).text(`Last updated: ${year}-${month}-${day}`);
}
});
}
};
xhttp.open("GET", "https://api.github.com/users/seachel/repos", true);
xhttp.send();
Start by opening the webpage in your browser. In the address bar, type the following, “javascript:alert(document. lastModified)” after the web page's URL. When you press enter, you will see a popup that displays the latest updated date.
It will check if there is any changes first and only commit and push all changes if there is any changes. This will run every the script every second. This will always push the changes with the commit message of "update".
Note: It can take up to 10 minutes for changes to your site to publish after you push the changes to GitHub. If you don't see your GitHub Pages site changes reflected in your browser after an hour, see "About Jekyll build errors for GitHub Pages sites."
If you want to edit or update your website, go to your Repository, select Upload files, and drag and drop your new updated file, enter a commit message, and click the button Commit changes.
The quick answer is yes. It is possible. You can view how to use the official GitHub API and create an AJAX request with vanilla JavaScript to this API, then all you have left to do is to handle which data you want.
Look at an example, I am calling with JavaScript trough AJAX the GitHub API and specifying that I want all repos for user k3llydev
. Then I just print the repository name and its last update time.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let repos = JSON.parse(this.responseText);
document.write("<h3>k3llydev github repositories w/last updated</h3>");
repos.forEach((repo)=>{
document.write(`<code>${repo.name}</code>: <em>${new Date(repo.updated_at)}</em><br>`);
});
}
};
xhttp.open("GET", "https://api.github.com/users/k3llydev/repos", true);
xhttp.send();
Note: If you use the API like this, the repo last update time that you want to implement on your website will update automatically, since every time your website is loaded, the GitHub API is called and will retrieve newer and updated data about your public repos.
The JS answer is nice, but you can also do this with a Bash script in the root of your site:
#!/bin/bash
git_last_commit_date="$(git log -1 --format=%cd)"
echo "Git last commit date: $git_last_commit_date"
html_files="$(find . -type f -name "*.html")"
for file in $html_files; do
echo "Inserting date into: $file"
sed -i "" "s|<span id=\"git-last-commit-date\">*</span>|<span id=\"git-last-commit-date\">$git_last_commit_date</span>|g" "$file"
done
Then have this <span>
element in pages you want it to update:
<span id="git-last-commit-date"></span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With