Skip to content

Commit 413b01f

Browse files
committed
Calculate time-since on the client
Such that it is accurate for cached pages. Fixes r-universe-org/help#557
1 parent 49c2822 commit 413b01f

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

routes/cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function(req, res, next){
3737
res.set('Cache-Control', `public, max-age=${max_age}, stale-while-revalidate=${cdn_cache}`);
3838

3939
if(doc){
40-
const revision = 16; // bump to invalidate all caches
40+
const revision = 19; // bump to invalidate all caches
4141
const etag = `W/"${doc._id}${revision}"`;
4242
const date = new Date(doc._published.getTime() + revision * 1000).toUTCString();
4343
res.set('ETag', etag);

views/builds.pug

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ block content
6565

6666
if !all_ok(row)
6767
a.text-decoration-none.retry-button.mx-3.fa.fa-sync-alt.d-none.d-xl-inline(href=build_url(row) data-retry-url=retry_url(row), data-pkgver=`${row.Package} ${row._failure ? row._failure.version : row.Version}`)
68-
td.text-nowrap.d-none.d-xl-table-cell.text-secondary #{format_time_since(row._failure ? row._failure.date : row._created)}
68+
td.text-nowrap.d-none.d-xl-table-cell.text-secondary
69+
span.package-show-difftime(data-timestamp=`${(row._failure ? row._failure.date : row._created).toISOString()}`)
6970

7071
block after_scripts
7172
script(src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.umd.js')

views/packages.pug

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ block content
1818
small.text-secondary - #{x.Title}
1919
p.card-text.fst-italic #{x.Description}
2020
p.card-text.pt-2
21-
small.text-muted.description-last-updated Last updated #{format_time_since(x._commit.time)}
21+
small.text-muted.description-last-updated Last updated
22+
span.ms-1.package-show-difftime(data-timestamp=`${new Date(x._commit.time * 1000).toISOString()}`)
2223
if x._topics
2324
p.card-text
2425
each topic in x._topics

views/pkginfo.pug

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ block content
233233
| #{_rundeps.length} dependencies
234234

235235
p#status.mb2.text-secondary.small(data-title="Build and check status" data-intro="Latest commit and check results. Click to expand on check logs for this build.")
236-
span.package-details-updated Last updated #{_lastupdate}
236+
span.package-details-updated Last updated
237+
span.ms-1.package-show-difftime(data-timestamp=`${new Date(_commit.time * 1000).toISOString()}`)
237238
| from:
238239
a.ms-1.package-details-sha.link-underline-dark.text-primary-subtle.fw-bold(target='_blank' href=`${_upstream}/commit/${_commit.id}`) #{_commit.id.substring(0,10)}
239240
if RemoteRef && RemoteRef !== "HEAD" && !RemoteRef.match(/^[a-f0-9]{40}/i)

views/sidebar.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,35 @@ function update_searchbox(){
248248
})
249249
}
250250

251+
function pretty_time_diff(ts){
252+
var date = new Date(ts);
253+
var now = new Date();
254+
var diff_time = now.getTime() - date.getTime();
255+
var diff_hours = Math.round(diff_time / (1000 * 3600));
256+
var diff_days = Math.round(diff_hours / 24);
257+
if(diff_hours < 24){
258+
return diff_hours + " hours ago"
259+
} else if(diff_days < 31){
260+
return diff_days + " days ago";
261+
} else if (diff_days < 365){
262+
return Math.round(diff_days / 30) + " months ago";
263+
} else {
264+
return Math.round(diff_days / 365) + " years ago";
265+
}
266+
}
267+
268+
function show_timestamps(){
269+
$(".package-show-difftime[data-timestamp]").each(function() {
270+
$(this).text(pretty_time_diff(this.dataset.timestamp));
271+
});
272+
}
273+
251274
/* Load sidebar and globals */
252275
$(function(){
253276
var isdev = window.location.hostname == 'localhost';
254277
window.server = isdev ? 'https://' + universe + '.r-universe.dev' : "";
255278
$(".nocran").toggle(universe != 'cran');
279+
show_timestamps();
256280
load_registry_status();
257281
load_universe_stats();
258282
load_maintainer_list();

0 commit comments

Comments
 (0)