I implemented this while fixing some things with full page search.
https://github.com/discourse/discourse/pull/3757/commits