pug-php / pug

Pug template engine for PHP
https://www.phug-lang.com
MIT License
391 stars 42 forks source link

[Bug] Cache Doesn't Update When Included Files Changed #174

Closed MikeDombo closed 6 years ago

MikeDombo commented 7 years ago

Hi, I have code like

extends template.pug
include mixins.pug
block after_head
    +MyMixin()
...

I also have caching enabled using the pugjs renderer. When I change mixins.pug the change does not propagate to my main file that includes the mixins.

kylekatarnls commented 7 years ago

Not sure we will be able to fix that. Mostly with pugjs option enabled. I recommand you to use the cache only for production environment. And use ->cacheDirectory to cache all templates when you deploy your app in production, then you would be able to set upToDateCheck to false to optimize performance and have no check at all of which files changed.

And in development, if you disable the cache, you're sure what you see is always up to date.

kylekatarnls commented 6 years ago

The renderer version 0.2.17 handle cache extend/include expiration.

So this feature is now available on the default mode (PHP engine). I will check if we can do something for pugjs option too.

kylekatarnls commented 6 years ago

Hi,

I finally decide not to implement a special cache strategy for pugjs but, now you can set up you own strategy:

For example, you can compare cachedFile last modified time against the directory of the sourceFile.

$pug = new Pug([
  'pugjs' => true,
  'pugjs_cache_check' => function ($cachedFile, $sourceFile, Pug $pug) {
    if (!file_exists($cachedFile)) {
      return false; // not up to date
    }
    return filemtime($rcachedFile) > filemtime(dirname($sourceFile));
  },
]);

The default behavior is:

return file_exists($renderFile) && (
    filemtime($renderFile) >= filemtime($filename) ||
    !$pug->getDefaultOption('upToDateCheck')
);

I hope you can work around with this.