Skip to content

Pre-render index page #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ module.exports = function Gruntfile(grunt) {
'assets/app.js',
],
},
htmlSnapshot: {
index: {
options: {
bodyAttr: 'data-prerendered',
fileNamePrefix: '',
msWaitForPages: 5000,
urls: [
'assets/index.vulcanized.html',
],
},
},
},
postcss: {
options: {
processors: [
Expand All @@ -27,6 +39,13 @@ module.exports = function Gruntfile(grunt) {
},
},
},
rename: {
index: {
files: {
'index.html': 'assets_index.vulcanized.html.html',
},
},
},
uglify: {
assets: {
files: {
Expand All @@ -42,7 +61,7 @@ module.exports = function Gruntfile(grunt) {
},
index: {
files: {
'index.html': 'assets/index.html',
'assets/index.vulcanized.html': 'assets/index.html',
},
},
},
Expand Down Expand Up @@ -80,6 +99,8 @@ module.exports = function Gruntfile(grunt) {
],
tasks: [
'vulcanize:index',
'htmlSnapshot:index',
'rename:index',
],
},
},
Expand All @@ -90,5 +111,7 @@ module.exports = function Gruntfile(grunt) {
'uglify:assets',
'postcss:assets',
'vulcanize:index',
'htmlSnapshot:index',
'rename:index',
]);
};
95 changes: 39 additions & 56 deletions assets/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
[3600, 'minutes', 60],
[7200, '1 hour ago'],
[86400, 'hours', 3600],
[172800, 'Yesterday'],
[172800, 'yesterday'],
[604800, 'days', 86400],
[1209600, '1 week ago'],
[2678400, 'weeks', 604800],
Expand All @@ -72,43 +72,46 @@
return f[2] ? Math.floor(seconds / f[2]) + ' ' + f[1] + ' ago' : f[1];
}
}
return 'A while ago';
}

// Display a repo's overview (for recent updates section)
function showRepoOverview(repo) {
var item = (
'<li>' +
'<span class="name"><a href="' + repo.html_url + '">' +
repo.name +
'</a></span>' +
' &middot; ' +
'<span class="time"><a href="' + repo.html_url + '/commits">' +
prettyDate(repo.pushed_at) +
'</a></span>' +
'</li>'
);

$(item).appendTo('#updated-repos');
return 'a while ago';
}

// Create an entry for the repo in the grid of org repos
function showRepo(repo) {
var $item = $('<div class="unit-1-3 repo" />');
var $link = $('<a class="box" href="' + getRepoUrl(repo) + '" />');

$link.append('<h2 class="repo__name">' + repo.name + '</h2>');
$link.append(
'<p class="repo__info">' +
repo.watchers +
' stargazers &middot; ' +
repo.language +
'</p>'
);
$link.append('<p class="repo__desc">' + getRepoDesc(repo) + '</p>');

$link.appendTo($item);
$item.appendTo('#repos');
var $item = $('.unit-1-3.repo[data-repo-name="' + repo.name + '"]');
var alreadyThere = $item.length >= 1;
if (!alreadyThere) {
$item = $(
'<div class="unit-1-3 repo">' +
'<a class="box" href="">' +
'<h2 class="repo__name"></h2>' +
'<p class="repo__info">' +
'<span class="repo__watchers"></span>' +
' stargazers &middot; ' +
'<span class="repo__language"></span>' +
'<span class="repo__updated">&nbsp;</span>' +
'</p>' +
'<p class="repo__desc"></p>' +
'</a>' +
'</div>'
);
$item.attr('data-repo-name', repo.name);
}

$item.children('a').attr('href', getRepoUrl(repo));
$item.find('.repo__name').text(repo.name);
$item.find('.repo__watchers').text(repo.watchers);
$item.find('.repo__language').text(repo.language);
$item.find('.repo__desc').text(getRepoDesc(repo));
if ($('body').attr('data-prerendered') === 'data-prerendered') {
$item.find('.repo__updated').text(
'Updated ' +
prettyDate(repo.pushed_at)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we include this label in the html to prevent the jumping when the js loads ? We could do something like $link.find('.repo__updated').html( prettyDate(repo.pushed_at) );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • All things now update on load (will update descriptions, watch count etc.) rather than rebuilding entire box each time
  • updated element holds &nbsp; in pre-render pass, gets updated on browser load.

);
}

if (!alreadyThere) {
$item.appendTo('#repos');
}
}

$.ajax({
Expand All @@ -125,27 +128,7 @@
// Convert pushed_at to Date.
$.each(repos, function feRepo(i, REPO) {
var repo = REPO;
var weekHalfLife = 1.146 * Math.pow(10, -9);
var pushDelta;
var createdDelta = (new Date()) - Date.parse(repo.created_at);
var weightForPush = 1;
var weightForWatchers = 1.314 * Math.pow(10, 7);

repo.pushed_at = new Date(repo.pushed_at);
pushDelta = (new Date()) - Date.parse(repo.pushed_at);
repo.hotness = weightForPush * Math.pow(Math.E, -1 * weekHalfLife * pushDelta);
repo.hotness += weightForWatchers * repo.watchers / createdDelta;
});

// Sort by hotness.
repos.sort(function sortRepos(a, b) {
if (a.hotness < b.hotness) return 1;
if (b.hotness < a.hotness) return -1;
return 0;
});

$.each(repos, function feSortedRepo(i, repo) {
showRepo(repo);
});

// Sort by most-recently pushed to.
Expand All @@ -155,8 +138,8 @@
return 0;
});

$.each(repos.slice(0, 3), function feSlicedRepos(i, repo) {
showRepoOverview(repo);
$.each(repos, function feSortedRepo(i, repo) {
showRepo(repo);
});
});
},
Expand Down
2 changes: 1 addition & 1 deletion assets/app.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ <h3>Regular Expressions made easy</h3>
<a href="https://github.com/VerbalExpressions/repositories">
<b id="num-repos">View</b> public repos
</a>
<br>
,
<a href="https://github.com/VerbalExpressions?tab=members">
<b id="num-members">View</b> members
</a>
</p>
<h3>Recently updated</h3>
<ol id="updated-repos"></ol>
<a class="twitter-share-button"
href="http://twitter.com/share"
data-text="All the VerbalExpressions projects in one place"
Expand Down
42 changes: 42 additions & 0 deletions assets/index.vulcanized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html><html lang="en"><head>
<meta charset="utf-8">
<title>VerbalExpressions ♥ Open Source</title>
<meta name="description" content="Find all the VerbalExpressions projects in one place.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html{font:14px/1.4 Helvetica Neue,Helvetica,Arial,sans-serif;color:#333;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}body{max-width:940px;padding:20px 10px;margin:0 auto;background:#f5f5f5}img{border:0;-ms-interpolation-mode:bicubic}a{color:#0084b4;text-decoration:none}a:active,a:focus,a:hover{color:#005e80;text-decoration:underline}a:focus{outline:thin dotted}a:active,a:hover{outline:0}h1,h2,h3{margin:0 0 1em}.h1,h1{margin:0 0 .25em;font-size:3em}.h2,h2{font-size:1.25em}.h3,h3{font-size:1.125em;color:#777}p{margin:0 0 1em}ol,ul{list-style:none;padding:0;margin:0}.grid{display:block;padding:0;margin:0 -10px;letter-spacing:-.31em;*letter-spacing:normal;word-spacing:-.43em;text-rendering:optimizespeed}.unit-1-3{width:300px;display:inline-block;margin:0 10px 20px;vertical-align:top;letter-spacing:normal;word-spacing:normal;text-rendering:auto;*display:inline;*zoom:1}.box{display:block;overflow:hidden;min-height:200px;padding:15px;border:1px solid #ddd;border-radius:4px;text-decoration:none;background:#fff}a.box:active,a.box:focus,a.box:hover{padding:12px;border-width:4px;border-color:#005e80;text-decoration:none}.site-header{float:left;text-align:center}.site-header a{min-height:48px;min-width:48px;line-height:48px}.stats{font-size:1.125em}.twitter-share-button{margin-top:30px}.repo__name{margin:0 0 .1em;font-size:1.5em}.repo__info{display:block;margin:0 0 1.25em;font-size:12px;font-weight:700;color:#999}.repo__info .repo__updated{display:block}.repo__desc{color:#666}.repo__team{overflow:hidden;margin:20px -1px 0}.repo__team img{float:left;width:23px;height:23px;border:1px solid #999;margin:0 1px;background:#eee}.site-footer{padding:15px;border:1px solid #ddd;border-radius:4px;background:#fff;clear:both}.site-footer p{margin:0}@media (max-width:960px){body{max-width:620px}}@media (max-width:640px){body{max-width:300px}}
</style>
</head>
<body>
<div class="grid">
<div class="unit-1-3 site-header">
<div class="box">
<div class="site-logo">
<h2>VerbalExpressions</h2>
</div>
<h3>Regular Expressions made easy</h3>
<p class="stats">
<a href="https://github.com/VerbalExpressions/repositories">
<b id="num-repos">View</b> public repos
</a>
,
<a href="https://github.com/VerbalExpressions?tab=members">
<b id="num-members">View</b> members
</a>
</p>
<a class="twitter-share-button" href="http://twitter.com/share" data-text="All the VerbalExpressions projects in one place" data-url="http://VerbalExpressions.github.com/" data-count="none" target="_blank">Tweet</a>

</div>
</div>

<div class="site-content" id="repos"></div>
</div>

<footer class="site-footer">
<p>VerbalExpressions <span id="year"></span></p>
</footer>

<script async="">!function(a,b){var c,d=a.getElementsByTagName(b)[0],e=function(e,f,g){var h=f;a.getElementById(h)||(c=a.createElement(b),c.src=e,h&&(c.id=h),g&&(c.onload=g),d.parentNode.insertBefore(c,d))};e("http://platform.twitter.com/widgets.js","twitter-wjs"),e("https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js",!1,function(){"use strict";function a(a){return g[a.name]||a.html_url}function b(a){return h[a.name]||a.description}function c(a){var b,c,d,e,f=0;for(b=new Date(a),c=(new Date-b)/1e3,d=[[60,"seconds",1],[120,"1 minute ago"],[3600,"minutes",60],[7200,"1 hour ago"],[86400,"hours",3600],[172800,"yesterday"],[604800,"days",86400],[1209600,"1 week ago"],[2678400,"weeks",604800]],f=0;f<d.length;f++)if(e=d[f],c<e[0])return e[2]?Math.floor(c/e[2])+" "+e[1]+" ago":e[1];return"a while ago"}function d(d){var f=e('.unit-1-3.repo[data-repo-name="'+d.name+'"]'),g=f.length>=1;g||(f=e('<div class="unit-1-3 repo"><a class="box" href=""><h2 class="repo__name"></h2><p class="repo__info"><span class="repo__watchers"></span> stargazers &middot; <span class="repo__language"></span><span class="repo__updated">&nbsp;</span></p><p class="repo__desc"></p></a></div>'),f.attr("data-repo-name",d.name)),f.children("a").attr("href",a(d)),f.find(".repo__name").text(d.name),f.find(".repo__watchers").text(d.watchers),f.find(".repo__language").text(d.language),f.find(".repo__desc").text(b(d)),"data-prerendered"===e("body").attr("data-prerendered")&&f.find(".repo__updated").text("Updated "+c(d.pushed_at)),g||f.appendTo("#repos")}var e=window.jQuery,f="VerbalExpressions",g={"verbalexpressions.github.io":"http://verbalexpressions.github.io/"},h={};e.ajax({dataType:"json",url:"https://api.github.com/orgs/"+f+"/repos?per_page=100",headers:{},success:function(a){var b=a;e(function(){e("#num-repos").text(b.length),e.each(b,function(a,b){var c=b;c.pushed_at=new Date(c.pushed_at)}),b.sort(function(a,b){return a.pushed_at<b.pushed_at?1:b.pushed_at<a.pushed_at?-1:0}),e.each(b,function(a,b){d(b)})})}}),e.ajax({dataType:"json",url:"https://api.github.com/orgs/"+f+"/members",headers:{},success:function(a){var b=a;e(function(){e("#num-members").text(b.length)})}}),e("#year").html((new Date).getFullYear())})}(document,"script");</script>


</body></html>
7 changes: 3 additions & 4 deletions assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,13 @@ a.box:active {
text-align: center;
}

.site-header .box {
min-height: 452px;
}
.site-header a {
min-height: 48px ;
min-width: 48px ;
line-height: 48px ;
}

.stats {
margin: 0 0 2em;
font-size: 1.125em;
}

Expand All @@ -177,6 +173,9 @@ a.box:active {
font-weight: bold;
color: #999;
}
.repo__info .repo__updated {
display: block ;
}

.repo__desc {
color: #666;
Expand Down
Loading