pug-php / pug

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

pugjs + cache: upToDateCheck not supported? #141

Closed Spone closed 7 years ago

Spone commented 7 years ago

Hello,

I switched to using pugjs mode for rendering, but I'd like to keep using the cache feature. Right now, even with the upToDateCheck set to true, the js files in my cache folder are refreshed every time I reload the page.

Here is the config I use:

$pug = new \Pug\Pug([
    'pugjs' => true,
    'cache' => get_template_directory() . '/cache/pug',
    'upToDateCheck' => false,
]);

Is this the expected behavior?

kylekatarnls commented 7 years ago

This unit test: https://github.com/pug-php/pug/blob/master/tests/features/pugjs.php#L47 is supposed to check that the cached file is not recreated if the last modified timestamp of the file is newer that the source file. Are filemtime('yourpugfile.pug') and filemtime('yourcachefile.js') accurate?

Spone commented 7 years ago

They seem to be, when I check them in the PHP interactive shell:

$ php -a
Interactive mode enabled

php > echo filemtime('cache/pug/icon.js') . ' / ' . filemtime('modules/icon/icon.pug');
1496404382 / 1496404357
kylekatarnls commented 7 years ago

And if you reload the page icon.pug is still 357 and icon.js changed?

Spone commented 7 years ago

After reloading the page I get:

php > echo filemtime('cache/pug/icon.js') . ' / ' . filemtime('modules/icon/icon.pug');
1496404834 / 1496404357
Spone commented 7 years ago

I don't see the option upToDateCheck being used in PugJsEngine.php, maybe that's the reason?

kylekatarnls commented 7 years ago

upToDateCheck is not the point, the behavior of this option is:

false: never refresh at all (assume you cached all the templates on deploy) true: refresh only if cacheTime < sourceTime

This check is done here when you use pugjs engine: https://github.com/pug-php/pug/blob/master/src/Jade/Engine/PugJsEngine.php#L146

Spone commented 7 years ago

So there are actually two different issues:

kylekatarnls commented 7 years ago

Hi, I reproduce the bug and will work on it.

kylekatarnls commented 7 years ago

It seems, pug-cli just touch the cached file but does not edit it. If you try the following:

use Pug\Pug;

require 'vendor/autoload.php';

$pug = new Pug([
  'cache' => 'cache',
  'pugjs' => true,
]);

echo $pug->render('view.pug')."\n";

view.pug contains p Hello

You will obtain <p>Hello</p>

Then if you replace "Hello" with "Bye" in cache/view.js and re-run the php file, you will obtain <p>Bye</p>, then disable the cache option and re-run again, you get <p>Hello</p>, it's a proof the cache works, but the last modified time on cache/view.js is not accurate because pug-cli change this timestamp when it run the file.

So the lonely that could be needed would to restore the original timestamp after render.

kylekatarnls commented 7 years ago

Hi, please update to 2.6.3, it allow upToDateCheck to work with pugjs and provide reliable modified timestamp for cached files. If you get troubles please re-open this issue.