HACKER Q&A
📣 preciousoo

How does HN preserve state so well?


I'm a bad tab hoarder and I have >500 tabs in my backlog, across many devices. One thing I've notices is that if I open an article from HN, if the button leads back to the HN homepage, it preserves the state exactly where I left HN opened at.

Maybe I haven't been on the static web in too long, but my memory is of pages refreshing whenever you hit back after some amount of time. Do they have a longer cache option? I don't see anything in their JS that suggests saving state.


  👤 solardev Accepted Answer ✓
I think that's just how pages are when you write them in plain-enough HTML and aren't overwriting the DOM with JS all the time. The web browser keeps track of your scroll progress and has the page in its cache (like frankjr said). You can disable JS altogether and it'll work the same.

It's a modern anomaly with JS frameworks that we've hacked and over-stretched the DOM so far past its original design that basic things like going forward/back need explicit JS handlers that rarely work all that well. You shouldn't need any state in JS to store basic navigation. It didn't used to be that way, and arguably shouldn't be that way for simple text-based sites like this. Maybe it's just becoming a lost art these days =/


👤 frankjr
I think you're seeing the effects of Back/forward cache.

https://web.dev/articles/bfcache


👤 slater
I'm thinking it's more a case of it being well-programmed instead of "latest framework"

Just don't look at the HTML :D