Wednesday, June 24, 2020

preventing viewers from seeing stale static pages

Until recently, the front page of amecy.com was generated by a CGI script.  There was a program, in a file named index.cgi, that ran every time you visited www.amecy.com.  The output of this program was sent to your browser, and this was the page that you saw.  While this setup worked fine, it was a little slow, so I started thinking about ways to speed it up.

Soon, I realized that the front page didn't need any of the dynamic behavior that a CGI script allows.  I decided to just run the script and save the output in a file named index.html.  In this way, I was able to speed up the site without doing a lot of work.  To make changes in the future, I can edit the CGI script, test it in the browser to make sure it works, re-run it on the server, and save the output to index.html.

Now, if you visit the site, the front page loads lightning-fast.  If you reload the page by clicking the logo in the top-left, the reload will be even faster: the server returns 304 Not Modified, and your browser can use the version of the page that is sitting in your browser's cache.

This is all straightforward, right?  Well, not exactly.  Some browsers -- Firefox and Chrome, at least -- will serve a static page directly from the browser cache, without checking with the server first.  If you update the page in between a viewer's visits to the site, the viewer may end up seeing the old version of the page, not the new one.  To circumvent this behavior, the server must explicitly tell the browser to check with the server before showing the cached version of the page.  This can be accomplished by sending the following header with the response:

        Cache-Control: must-revalidate, max-age=0

must-revalidate tells the browser to check before displaying the page.  However, the "check" here is not exactly checking the server; the browsers that I tested will often perform this "check" by checking against the browser cache.  To force the browser to check against the server, I had to set an expiry on the cached page.  That is what max-age=0 is doing there.

There is also a middle ground between "always check with the server" and "never check with the server."  If you are fine with letting someone see a page that is slightly out of date -- let's say an hour -- you can send the header

        Cache-Control: must-revalidate, max-age=3600

This way, someone viewing your site will only see a stale page for an hour.  The upside is that this will be faster, both for the browser (it can often show the page without having to use the network) and for your server (you won't have browsers connecting to your site as often).  The benefit is especially significant if the page in question is a "hub" page that you expect people to visit often while navigating your site.

If you have been on the Internet for a while, you have probably encountered this problem once or twice, when visiting a static site repeatedly.  You will see the latest version of the site the first time you visit, but on later visits, you may have to refresh to see updates. Many web pages tell you to refresh them; this is another manifestation of the problem.  The problem can be avoided, as I just explained, but it is not obvious to everyone that there is a problem (unless you go looking for it!).

That's about it.  If everything in this post was already obvious to you, then good... I am happy for you!  But when I started seeing unexpected behavior around static pages, I had to piece the solution (and the explanation) together from a few different places around the web.  I have written this post in the hopes that it will help someone who encountered the same problem as I did.

9 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. There is an additional bet, which want to|you must} place find a way to} win the progressive jackpot. Let me determine the proper primary technique for you, under nearly any algorithm, using my primary technique calculator. The sport of blackjack dates to the middle of the 18th 배당사이트 century. Like many other playing pastimes , it originated in France, although Americans gave the sport its present type. Many guests have been eager to plunk their cash into slot machines and blackjack tables. The player may double down on any preliminary two card hand.

    ReplyDelete
  6. Betting on staff sports activities has become an important service business plenty of} international locations. Before the arrival of the internet, hundreds of thousands of people played the soccer swimming pools every week within the United Kingdom. Charities argued at a public hearing final 인터넷카지노 month that permitting tribes to host internet gambling and sports activities betting in the state – a monopoly worth hundreds of thousands – may effectively finish charitable gambling in North Dakota. The focus is on affiliate companies that promote and drive traffic to unlawful on-line casinos.

    ReplyDelete
  7. He encounters extraordinary luck sm카지노 taking part in} chemin de fer, which leads him to lose observe of time. Accounts of the scandal in newspapers also included the foundations for the sport. The scandal turned the topic of music corridor songs and a stage play. Should the stakes of the punters exceed the amount in the meanwhile within the financial institution, the banker is not responsible for the amount of such extra. In the event of their dropping, the croupier pays the punters in order of rotation, as far as the funds within the financial institution will extend; past this, they have no declare. The banker could, however, in such a case, as an alternative of resting on his right, declare the stakes accepted, putting up the needed funds to meet them.

    ReplyDelete
  8. You can’t win these gadgets, but have the ability to|you possibly can} land a giant money prize by triggering the Mini Wheel Bonus. There’s no mistaking the Asian theme when you hearth up this slot from IGT. A soothing soundtrack and Far East symbols adorn the game the place you hope to land Fortune Coins and trigger the bonus characteristic. Each coin can award money prizes of a lot as} 1,000 your stake – and trigger the Jackpot Bonus for prime rewards. You might have come across the word “play responsibly” or “responsible gambling” quickly as} in your lifetime. A small group of individuals permits playing to become a massive a part of} their life, expecting 1xbet more winning on their facet.

    ReplyDelete
  9. Whilst this certainly one of the|is among the|is probably one of the} greatest promos, it’s additionally the toughest to clear, so bear this in mind. Each weekend you’re going to be able to|to have the ability to} declare two 메리트카지노 extra Big Spin Casino reload bonuses. You can declare one on Saturday and one on Sunday, for two in complete. It’s price noting that there is a couple of promotion that you can declare.

    ReplyDelete