在 localhost mac os x 上测试时未清除缓存

Cache not clearing when testing on localhost mac os x

我的 symfony 3 项目发生了一个非常奇怪的错误。在做了一些研究之后,我将错误缩小到与我的系统范围缓存有关。

为什么它与 symfony 无关...

我会解释原因,但在您认为它与 symfony 或资产缓存问题有关之前,请阅读以下内容:

我每次部署时都运行以下shell脚本,以确保生产和开发环境的一切都是干净的。

chown -R distribution:distribution .
rm -rf var/cache/dev/*
rm -rf var/cache/prod/*
rm -rf web/css/*
rm -rf web/js/*
bin/console cache:clear --env=dev
bin/console cache:clear --env=prod --no-debug
bin/console assets:install web --symlink
bin/console assetic:dump --env=dev
bin/console assetic:dump --env=prod --no-debug
bin/console doctrine:schema:update --force --dump-sql
bin/console doctrine:schema:update --force --dump-sql --env=prod
chown -R distribution:distribution .
bin/console server:run

我通过 SSH 隧道转到计算机上的本地主机来测试开发环境。

sudo ssh -L 3306:127.0.0.1:3306 -L 80:127.0.0.1:80 -L 8000:127.0.0.1:8000 -p 25000 fake_super_user_name@(server ip address here)

我可以随时访问我的 symfony 开发版本,现在打开我的浏览器并输入 localhost:8000

一切都在进行中,直到我做了一些关于添加捆绑包以及更改路由和文件的名称以及位置等的重大更改...

为什么它的系统相关...

在 google chrome 中,当我转到我的页面并打开检查器时,我可以看到它正在尝试访问已编译的文件 - js_part_1_admin_1.js。这是我拥有的 javascript 文件的旧版本。但是,当我转到服务器并查看我的 FTP 程序并通过控制台在 web/js 目录中使用 ls -al 时,该文件不存在!正确的文件是 compiled-js_part_2_admin_1.js,但引用的文件绝不在我的服务器上。因此,它从我计算机上的某处抓取了一个缓存副本。

所以删除我浏览器中的缓存...

这适用于我在实际域名上的生产站点,但由于某种原因,当我尝试使用本地主机或完全从 Google Chrome 删除缓存时,似乎没有任何内容被删除并且它一直在引用旧文件。通过在 chrome 中的 url 中键入 about:cache,我仍然可以在缓存中看到它。如果您好奇我是如何尝试删除缓存的,我会打开检查器并按住刷新按钮和 select 'empty cache and hard reload'.

好吧,换个浏览器...

我使用过 Firefox、Safari,甚至 Opera 以及所有这些浏览器,即使我之前从未使用过该浏览器,当我通过本地主机访问时仍然请求旧文件...

我猜它存储在某种超级缓存中,因为它是本地主机...我不知道这个缓存在系统上的什么位置以及如何删除它。

万一这很重要,我在服务器上的 .htaccess 文件确实为文件设置了过期和缓存规则...我实际上是在尝试测试它,所以我不想禁用它,但我我确定这是缓存的原因,我只是想弄清楚如何清除本地主机的缓存,这样我就可以再次开发,知道我看到了正确的资产。

.htaccess

# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex app.php

# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
Options FollowSymlinks

# Disabling MultiViews prevents unwanted negotiation, e.g. "/app" should not resolve
# to the front controller "/app.php" but be rewritten to "/app.php/app".
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the app.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by Apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/ [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

# These rules need to be at the bottom!
# Make it aware of True Type Fonts for Web Fonts.
AddType application/x-font-ttf .ttf

Header unset Pragma
FileETag None
Header unset ETag 

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/x-font-ttf "access plus 1 year"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/js "access 1 week"
ExpiresByType application/javascript "access 1 week"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##

精简版

如果我遗漏了解决此问题所需的任何信息,请告诉我,我会立即 post。简而言之,我的本地系统以某种方式存储了我无法删除的缓存。如何在所有浏览器中删除此特定于 localhost 的缓存?

好的,我找到了解决方案...但只是有点。 Assetic 给出了一个真正的错误,修复该错误使缓存开始修复它自己。

None少,在这种情况下这是非常奇怪的缓存行为。