Simplest way for Cache Busting in NodeJS/Express

Caching in Web browsers is important. Without caching page performance is hurt as each page navigation requires the same set of JavaScripts and CSS files to be re-fetched. We can achieve caching by setting Cache-Control header’s max-age in the HTTP response headers. In NodeJS/Express stack, you do this done by passing options map to express.static() middle-ware.

However, the challenge is to discard or bust this client cache for new code deployments. As HTTP is a stateless protocol, there is no way server can tell clients to ignore old caches and request again. If you do set max-age:0 then we shall not arrive in this situation at all. However, we want to achieve caching and refresh this cache with new code deployments.

Read more

Singleton Toast

On Android you may have experienced delayed toasts or overlapping toasts. This causes the toasts being displayed with irrelevant context/activity (e.g. user may have pressed backed, or previous toast is still overriding new one, or when too many toasts are displayed on screen). Consider the code below. It defines a single function for toast, that makes use of context object of Application. The class MyApplication needs to be declared in manifest.xml. The main advantage of such design is reduced footprint for activity’s context object. at any moment only one toast remains on screen reusing single toast object, rather than creating…

Read more

Handicapped Software

[ This post is about software’s disability to meet users’ expectations. I know you have developed lots of software/applications over your career! Have you ever evaluated them on disability index? No, then read on. “You” = refers to our typical “software engineer” ] Ever heard someone saying “this software sucks”… I love such moments. It makes me laugh, not for the user, but for the poor developer! Users are innocent, they are not concerned about technical details. A software is there to make life simpler and hide complications of a task. It’s your responsibility to make it intuitive and meet…

Read more

Unreachable code Error or Warning?

In Java, unreachable code is treated as compilation error. What do you think? Isn’t making it warning would have made developers’ life simpler? Of course you can write – if( true )    return; What if you could simply write return. This helps in testing a function quickly. As a good programming practice we always remove all warnings from code, and such unconditional return statements can be rectified later.

Read more

Eating Exceptions… eeehhhh!

Following are few points you need to take care when you decide to eat an exception. These are very much specific to C# language. a. Limit the code block. Attempt to wrap one or two statements within try. b. If an exception is eaten, log details to some logging mechanism. In short never write empty catch catch(Exception) {}. c. Never eat exception that indicates some bad behavior of code execution like – ArgumentNullException, NullReferenceException, ArgumentNullException, InvalidCastException, InvalidOperationException, AccessViolationException, etc; d. There is a big difference between throw and throw exeception statements. This is in terms of resetting stack trace which…

Read more