【问题标题】:Laravel/blade caching css filesLaravel/blade 缓存 css 文件
【发布时间】:2014-06-18 03:41:09
【问题描述】:

我正在使用 PHP-FPM 开发 Nginx 服务器。我安装了Laravel 4.1bootstrap v3.1.1.,这就是问题所在。在过去的 30 分钟里,我一直在尝试更改我首先声明检查 boostrap 的 CSS 规则。

.jumbotron{
   background: red; 
}

第一次成功。 jumbotron 容器是红色的。所以,我删除了那个 css 值并开始工作,但无论我使用哪个浏览器,容器都是红色的。我什至通过谷歌浏览器检查工具检查了 css 文件,它向我展示了当 jumbotron 有 background:red 时的第一个值。我删除了css文件并将其重命名并添加了新样式,我将chrome配置为不缓存页面。但仍然是相同的值。我现在确信,Laravel 保留了第一个样式声明的缓存。

有没有办法完全禁用它?

【问题讨论】:

  • 您是否尝试过从 `app\storage\views` 目录中删除所有内容?
  • @totymedli 我应该吗?我不知道这是必需的。我没有删除任何文件。那我怎么继续工作呢?

标签: css twitter-bootstrap caching laravel laravel-4


【解决方案1】:

Laravel 5.8+ 中你可以这样使用:

version 方法会自动为所有编译文件的文件名附加一个唯一的哈希值,从而更方便地清除缓存:

mix.js('resources/js/app.js', 'public/js').version();

生成版本化文件后,您将不知道确切的文件名。因此,您应该在视图中使用 Laravel 的全局混合功能来加载适当的散列资产。混合函数会自动确定哈希文件的当前名称:

<script src="{{ mix('/js/app.js') }}"></script>

完整文档:https://laravel.com/docs/5.8/mix

【讨论】:

    【解决方案2】:

    这里也是同样的问题。我正在使用带有指向我的文档根目录的共享文件夹的 VirtualBox。

    这为我指明了正确的方向: https://stackoverflow.com/a/26583609/1036602

    这导致我这样做: http://www.danhart.co.uk/blog/vagrant-virtualbox-modified-files-not-updating-via-nginx-apache

    还有这个: https://forums.virtualbox.org/viewtopic.php?f=1&t=24905

    如果您通过 vboxsf 共享文件夹挂载本地开发根目录,请在 apache2.conf 中设置 EnableSendFile Off(如果使用 Nginx,则设置 sendfile 关闭)。

    【讨论】:

      【解决方案3】:

      为了它的价值,因为这个答案首先出现在我的谷歌搜索中......

      我遇到了同样的问题。 CSS 和 JS 文件不会更新。删除缓存文件不起作用。时间戳不是问题。我可以更新它们的唯一方法是更改​​文件名,直接加载它以获取 404 错误,然后将名称更改回原始名称。

      最终问题与 Laravel 或浏览器缓存无关。问题是由于 NginX 使用了不能与远程文件系统一起使用的 sendfile。在我的例子中,我使用 VirtualBox 作为操作系统,远程文件系统是通过 Guest Additions 实现的 vboxsf。

      我希望这可以节省其他人一些时间。

      【讨论】:

      • 谢谢!由于我使用相同的配置,它对我帮助很大。我也通过改名解决了。
      【解决方案4】:

      一般说明

      当您访问 Laravel Blade 视图时,它会将其生成到一个临时文件中,因此它不必在您每次访问视图时都处理 Blade 语法。这些文件存储在app/storage/view 中,文件名是文件路径的MD5 哈希。

      通常当您更改视图时,Laravel 会在下一次视图访问时自动重新生成这些文件,然后一切都会继续。这是通过filemtime() 函数比较生成文件和视图源文件的修改时间来完成的。可能在您的情况下存在问题并且临时文件没有重新生成。在这种情况下,您必须删除这些文件,以便重新生成它们。它不会伤害任何东西,因为它们是从您的视图中自动生成的,并且可以随时重新生成。它们仅用于缓存目的。

      通常,它们应该会自动刷新,但是如果它们被卡住并且遇到此类问题,您可以随时删除这些文件,但正如我所说,这些应该只是极少数例外。

      代码分解

      以下所有代码均来自laravel/framerok/src/Illuminate/View/。我在原件上添加了一些额外的 cmets。

      获取视图

      Engines/CompilerEngine.php 开始,我们有了理解机制所需的主要代码。

      public function get($path, array $data = array())
      {
          // Push the path to the stack of the last compiled templates.
          $this->lastCompiled[] = $path;
      
          // If this given view has expired, which means it has simply been edited since
          // it was last compiled, we will re-compile the views so we can evaluate a
          // fresh copy of the view. We'll pass the compiler the path of the view.
          if ($this->compiler->isExpired($path))
          {
              $this->compiler->compile($path);
          }
      
          // Return the MD5 hash of the path concatenated
          // to the app's view storage folder path.
          $compiled = $this->compiler->getCompiledPath($path);
      
          // Once we have the path to the compiled file, we will evaluate the paths with
          // typical PHP just like any other templates. We also keep a stack of views
          // which have been rendered for right exception messages to be generated.
          $results = $this->evaluatePath($compiled, $data);
      
          // Remove last compiled path.
          array_pop($this->lastCompiled);
      
          return $results;
      }
      

      检查是否需要再生

      这将在Compilers/Compiler.php 中完成。这是一个重要的功能。根据结果​​,将决定是否应该重新编译视图。如果这返回 false 而不是 true,则可能是视图未重新生成的原因。

      public function isExpired($path)
      {
          $compiled = $this->getCompiledPath($path);
      
          // If the compiled file doesn't exist we will indicate that the view is expired
          // so that it can be re-compiled. Else, we will verify the last modification
          // of the views is less than the modification times of the compiled views.
          if ( ! $this->cachePath || ! $this->files->exists($compiled))
          {
              return true;
          }
      
          $lastModified = $this->files->lastModified($path);
      
          return $lastModified >= $this->files->lastModified($compiled);
      }
      

      重新生成视图

      如果视图过期,它将重新生成。在Compilers\BladeCompiler.php 中,我们看到编译器将遍历所有 Blade 关键字,最后返回一个包含已编译 PHP 代码的字符串。然后它将检查是否设置了视图存储路径并将文件保存在那里,文件名是视图文件名的 MD5 哈希。

      public function compile($path)
      {
          $contents = $this->compileString($this->files->get($path));
      
          if ( ! is_null($this->cachePath))
          {
              $this->files->put($this->getCompiledPath($path), $contents);
          }
      }
      

      评估

      最终在Engines/PhpEngine.php 中评估视图。它使用extract()include 将传递给视图的数据导入具有trycatch 中传递路径的文件的所有异常handleViewException() 再次引发异常。还有一些输出缓冲。

      【讨论】:

      • 每次更改我的 css 文件时都必须删除它们吗?
      • @iOi 不,应该自动完成,但有时会卡住。
      • file_put_contents(/var/www/public/xxx.com/app/storage/views/eca408f750b0ca‌​85bd80f6271c1771b6):无法打开流:没有这样的文件或目录
      • 很好的解释,但这不是问题所在。我相信this might be 是我的问题。
      • @iOi 该线程是关于文件修改时间的奇怪问题。我也写过这个:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2014-12-10
      • 2013-09-10
      • 2020-04-23
      • 2021-03-07
      • 1970-01-01
      • 2011-11-01
      相关资源
      最近更新 更多