tomaj / nette-api

API solution for Nette framework with interactive generated console for each api
MIT License
36 stars 20 forks source link

expiration to false #127

Closed raky2702 closed 1 year ago

lulco commented 1 year ago

@tomaj our use case is we want to add header like:

public cache-control: public, max-age=600, stale-while-revalidate=3600, stale-if-error=3600

This was not possible, because Response::setExpiration() in nette sets no cache header if we send null from our response.

public function setExpiration(?string $expire)
{
    $this->setHeader('Pragma', null);
    if (!$expire) { // no cache
        $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
        $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
        return $this;
    }

    $expire = DateTime::from($expire);
    $this->setHeader('Cache-Control', 'max-age=' . ($expire->format('U') - time()));
    $this->setHeader('Expires', Helpers::formatDate($expire));
    return $this;
}

So we created special value false for NetteApi's response classes which can help us to handle it.