TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Offline First – A Better HTML5 User Experience

434 pointsby openmazeover 8 years ago

26 comments

mbrockover 8 years ago
The most useful pattern I know of for offline web apps is the command queue.<p>Basically, the rendered state of the client is the acknowledged server state plus the client-side command queue.<p>User actions don&#x27;t make a server request and then update the UI. Instead they directly append to the local command queue, which updates the UI state, and right away the client begins communicating with the server to make the local change real.<p>While the client&#x27;s command queue is nonempty, the UI shows a spinner or equivalent. If commands cannot be realized because of network failure, the UI remains functional but with a clear warning that changes are waiting to be synchronized.<p>(The API for connectivity status are useful for making sure that the command queue resumes syncing when the user&#x27;s internet comes back.)
评论 #13246686 未加载
评论 #13246244 未加载
评论 #13246816 未加载
评论 #13246551 未加载
评论 #13247845 未加载
评论 #13246448 未加载
评论 #13246512 未加载
评论 #13246631 未加载
评论 #13246296 未加载
评论 #13246214 未加载
评论 #13247107 未加载
评论 #13246452 未加载
c-smileover 8 years ago
Wearing my architect cap...<p>There are four types of applications in that respect:<p>1. always disconnected (e.g. calculator)<p>2. occasionally connected (e.g. email client)<p>3. occasionally disconnected (e.g. messaging client)<p>4. always connected (e.g. trading terminal)<p>First architectural task I am doing when have given an application idea is its classification using these models.<p>Each particular case requires its own storage&#x2F;caching approach. &quot;Offline first&quot; is too broad.
评论 #13247103 未加载
评论 #13247989 未加载
评论 #13247543 未加载
评论 #13247444 未加载
joelambertover 8 years ago
Wow, cool to see this resurface again! I wrote this ~4 years ago and the web has come on along way in that time. New features such as service workers are definitely making offline a lot easier.<p>On the subject of progressive enhancement, I&#x27;m a huge advocate and believe it is in general the way todo content sites. As has been pointed out above though, some use cases do require a different approach. For context this was post was written after working on a number of HTML5 apps that were wrapped in Cordova&#x2F;PhoneGap.
substackover 8 years ago
Offline is not a mere feature you can bolt on to existing architectures because you are really building a distributed system. Architectures that work well for distributed systems and especially p2p architectures thrive in this environment. Elsewhere in these comments people are discussing command queues, but this idea presumes that servers are somewhat reliable and that all operations need to go through a centralized point of control and failure. Instead, you can take this idea further and implement a kappa architecture (sometimes also called &quot;event sourcing&quot;) where you maintain a log locally on every client which is the source of truth, not a server. When a network connection is available, you can replicate the log to a server or directly to other clients. You can build indexes (materialized views) that sit on top of this log to answer queries more quickly than reading the entire log out every time. You can also blow away these indexes and rebuild them from the log whenever your requirements change (migrations).<p>Unfortunately the web is missing a few pieces that would make it a very good platform for fully p2p, distributed apps. Service workers are a good start, but they have a 24-hour upper cap on max-age of the service worker itself, so users can&#x27;t trust on first use (TOFU) and then be more secure against kinds of active targeting. The suborigin specification and iframe sandboxes for distributing apps offline and p2p would be much more useful for offline sandboxes if they didn&#x27;t require that a server send an http header. These will become much more important as the web bluetooth API matures, which can allow distributing user data and application updates in a totally offline environment.<p>Even without being fully offline, it&#x27;s very odd that when an automatic update to android or windows comes down the pipe, people in remote areas download the exact same bytes from servers in America over and over again, all over a thin pipe. They could fetch that data from each other and save a lot of money on bandwidth and data caps.
mark242over 8 years ago
tldr: Someone wrote a bunch of paragraphs and LOC around JSON.parse(window.localStorage.getItem(key)).<p>If you&#x27;re really interested in learning about creating a workable offline web app, Google has some great documentation. <a href="https:&#x2F;&#x2F;developers.google.com&#x2F;web&#x2F;fundamentals&#x2F;instant-and-offline&#x2F;offline-ux" rel="nofollow">https:&#x2F;&#x2F;developers.google.com&#x2F;web&#x2F;fundamentals&#x2F;instant-and-o...</a><p>Last, the Safari team needs to seriously get to work on Service Workers. We will see web apps grow by leaps and bounds once the service worker spec is opened up to iPhone users.
评论 #13246318 未加载
jedbergover 8 years ago
These are basically the same rules for any distributed application, like something using microservices. An app is just the edge node of a distributed system.<p>In any distributed system, the biggest cost is moving data between nodes, and therefore the biggest failure case is when data is moving slowly or not at all. It&#x27;s a case you should always be prepared for.<p>If you write your app in a way that assumes the network is bad, which you should always do, whether it&#x27;s an app or two microservices, then you&#x27;ll have a more robust system.
评论 #13247851 未加载
fareeshover 8 years ago
Anyone using PouchDB + CouchDB in production to solve this type of problem? It is my go-to solution but I&#x27;m unaware of problems at scale.
blazespinover 8 years ago
Maybe add 2012 to title on HN. As that&#x27;s from when this post is.
评论 #13246542 未加载
tannhaeuserover 8 years ago
Isn&#x27;t it simpler though to just use static HTML + optional Javascript (like we used to in the 2000&#x27;s eg. progressive enhancement years)? I mean why use M-V-whatever for content-driven sites at all?
评论 #13246168 未加载
评论 #13246050 未加载
评论 #13246075 未加载
tyingqover 8 years ago
I would throw in some guidance that says &quot;don&#x27;t trust anything the client side sends to you&quot;. Developers used to server side MVC could expose themselves to client-side manipulation that open up some security issues.<p>I remember some early ecom cart implementations where you could &quot;name your own price&quot; as an unintended feature.
kqrover 8 years ago
I sense a trend of trying to cram everything good about native apps onto the web. Do people who do this stop to think whether the web is actually the correct platform for their app?
评论 #13246012 未加载
评论 #13246010 未加载
评论 #13246260 未加载
评论 #13246358 未加载
评论 #13246386 未加载
cmurfover 8 years ago
It continues to baffle me how much on disk cache is used by web browsers, but won&#x27;t be used to load a page I know is in the cache if I&#x27;m offline.
DecoPersonover 8 years ago
I&#x27;m surprised no one has mentioned Meteor[0].<p>[0] <a href="https:&#x2F;&#x2F;www.meteor.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.meteor.com&#x2F;</a>
评论 #13248166 未加载
评论 #13248947 未加载
afarrellover 8 years ago
Doesn&#x27;t Chome on iOS prevent this because it reloads whenever you re-open a page, even if you don&#x27;t have an internet connection?
评论 #13246984 未加载
评论 #13246266 未加载
评论 #13246031 未加载
stuaxoover 8 years ago
I wish more native apps were offline first.<p>Take the tripadvisor app; when you go to the website you are nagged to go to the app, if you click you end up in the google play store and it loses where you were.<p>And of course it only works online; utterly pointless.
edemover 8 years ago
Funnily this site does not wor k well with my crappy mobile connection. It loads 3 paragraphs and strangely the 4th paragraph is cut in half. I waited a minute for it to load.
opvasgerover 8 years ago
I feel like commenting on the callbacks and variable-bound-contexts, and saying something about the glorious wonder of modern JavaScripts... better not, tho :)
cryptarchover 8 years ago
How about offering a HTML experience in the first place?<p>I&#x27;m really tired of &quot;HTML&quot; websites that are actually a heap of Javascript writing to a virtual DOM, with only a few references to scripts, most of it being for surveillance (e.g. ad trackers, surveillance for profit with the side effect of it being available to intelligence services via subpoenas, gag orders and secret courts).
BorisMelnikover 8 years ago
very interesting - I&#x27;d love to see a feature like &quot;queue &#x2F; share later&quot; (maybe Buffer etc) to share when you get back online again.
评论 #13246501 未加载
foxhopover 8 years ago
Offline First versus Resilient Web
ebbvover 8 years ago
I get where this movement is coming from but I just fundamentally disagree with it. Making offline first web apps may make sense for certain applications where you expect you users may need to use it offline, but it doesn&#x27;t make sense for all apps and it can require a fundamentally different way of writing your application which is a waste of time and effort if it&#x27;s not a likely use case for your users.
评论 #13247375 未加载
评论 #13246716 未加载
mozumderover 8 years ago
That&#x27;s nice and all, but no one is going to have all the images&#x2F;videos&#x2F;audio available offline for their media sharing social network app.<p>Web servers exist so you don&#x27;t have to have all that data locally.
diminoover 8 years ago
How do you write unit tests for something like this? Are there frameworks that make this kind of model easier to deal with?
ebbvover 8 years ago
Dude, I get it. Stop being so condescending. Maybe you&#x27;re trying to be helpful by including those links but it comes off as incredibly insulting and pedantic.<p>I was talking about a real-time communication application.<p>You can make the argument that the application should try to cope with a sporadic connection, but in the real world this is inefficient for both the user and the staff if the connection is going in and out and they are trying to hold a conversation it might be better to show the user a message that says hey your connection is down try later when you have a better connection or try an asynchronous support method like submitting a ticket.<p>Regardless that was just an example off the top of my head. My point still stands; web applications are web applications for a reason, and &quot;offline first&quot; doesn&#x27;t make sense for most of them.
评论 #13250957 未加载
评论 #13247223 未加载
CorvusCryptoover 8 years ago
I see the author&#x27;s vision, but I have to say that I still favor progressive enhancement over this. Using javascript only assumes a lot about the user even in this modern age of web development.<p>Also as a nitpick, I would say don&#x27;t make your data objects SINGLETONS, make them SINGLE INSTANCE.<p>EDIT: someone deleted their comment but brought up a good point that you can have progressive enhancement with this. Yes, however the author made it clear he favors the JS or nothing approach. And there&#x27;s even this snippet:<p>&gt;An offline first approach would be to move the entire MVC stack into client side code (aka our app) and to turn our server side component into a data only JSON API.
评论 #13246166 未加载
评论 #13246081 未加载
yellowappleover 8 years ago
I disagree rather strongly with the implicit advice here: that HTML5 &quot;applications&quot; ought to rely on client-side processing first and foremost. This usually translates to &quot;shove more Javascript down the user&#x27;s throat&quot;, which is the opposite of &quot;a better HTML5 user experience&quot; IMO. If I trust you enough to want to run arbitrary Turing-complete code of your making, I&#x27;d be more inclined to download a native implementation for my platform; otherwise, I generally have better things for my CPU to do than burn cycles on things that should be done on your own servers.<p>I do agree, however, that the HTML &quot;app&quot; ought to be treated equivalently to native implementations -- that is, it should communicate over the same API (or perhaps an internal equivalent) as native apps. I like to think of this as &quot;middle-end&quot; web development, with the HTML-generating server working to mediate the browser&#x27;s expectations (HTML and CSS and <i>maybe</i> some Javascript if it&#x27;s really needed) with the actual business logic provided by the API.