Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
lighter-weight fancy dates #3232
"Fancy dates" are currently implemented using moment.js, which is well-known for being a comprehensive and robust date-handling library. It's also well-known for being enormous when shipped with all supported locales — 319 kB, probably the size of all the content on my Nikola site put together.
Fortunately, as a static site, we only need a few bits of functionality from the wide world of date handling.
Fanciness 1: using JS_DATE_FORMAT and local user time
Formatting a Date as a local time in some sort of locale-appropriate way is available as Date.toLocaleString. But since, for fanciness purposes, we as the publisher want more control over the formatting, we have to look further.
To be able to use strftime-style format strings, consider
Some of these libraries are similar in size to Moment, but are more friendly to loading just certain functions (i.e. formatting) without dragging in the whole thing.
Fanciness 2: using a string like “2 days ago”
This level of fanciness does not yet enjoy native browser support, though see the TC 39 Proposal for Intl.RelativeTimeFormat. Polyfill may be available.
referenced this issue
Feb 19, 2019
We currently don’t have any specific policy for browser support. Note that while Level 1 will probably be supported correctly, Level 2 won’t, as RelativeTimeFormat was added to Chrome (stable) in December 2018, and to Firefox in January 2019. It’s not supported by Safari.
A good rule-of-thumb that I would personally approve would be that a JS feature may be used, if it’s supported:
As long as the number of affected users is small, sure.
Nikola will never require Node to be installed for the core feature set. The Node ecosystem is terrible, and external dependencies are always a pain to get working.
Another possible solution to minimize asset size would be to re-use the infrastructure previously used with Colorbox (copy only required locales to output, load only one i18n file on a page): f45bafb — cdnjs supports that workflow as well, even if we wouldn’t be able to use SRI for these files.