xp-forge / frontend

Web frontends
1 stars 1 forks source link

Set Cache-Control to no-cache & allow overwriting via View::cache() #14

Closed thekid closed 3 years ago

thekid commented 3 years ago

See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

Example

use web\frontend\{View, Handler, Get};

#[Handler]
class Blog {

  public function __construct(private Database $database) { }

  #[Get]
  public function index() {
    return $this->database->newest();
  }

  #[Get('/{id}')]
  public function article($id) {
    $article= $this->database->article($id);
    return View::named('article')->with($article)->cache('max-age=604800, must-revalidate');
  }
}

The index handler will use a Cache-Control: no-cache (which is the default), while the article handler will overwrite this according to the recommendation from the Cache-Control issue:

For dynamic pages which are unfrequently updated (say, a blog), we can cache a little bit more using Cache-Control: max-age=604800, must-revalidate (quote: This tells the browser to cache the HTML page for one week (604,800 seconds), and once that week is up, we need to check with the server for updates.)