简体   繁体   English

如何在服务器中正确显示我的项目?

[英]How do I display my project correctly in a server?

I sent my project to my server but no one cant see changes what i did in local mode(i have index.html and other js and php).我将我的项目发送到我的服务器,但没有人能看到我在本地模式下所做的更改(我有 index.html 和其他 js 和 php)。 I had the same problem with another project with index.php but soved adding this <?php time();?> at the end of scrip.我在 index.php 的另一个项目中遇到了同样的问题,但很喜欢在 scrip 的末尾添加这个<?php time();?> Is there any similar solution for javascript? javascript有没有类似的解决方案?

This is what i did这就是我所做的

<script src="assets/js/funciones.js?<?php time();?>"></script>

The problem is that you're changing static files, but not their filenames.问题是您正在更改 static 个文件,而不是它们的文件名。

By default apache/nginx/etc serve static content with headers that say "cache this for a very long time" because it's static content, why would you not?默认情况下,apache/nginx/etc 服务 static 内容,标头上写着“缓存这个很长时间”,因为它是static内容,你为什么不呢?

Tacking on random trash to the URL like you're doing with you JS is a kludge that permanently breaks all caching and ensures that users will repeatedly download the exact same static file every time they request a page.像对待 JS 一样,将随机垃圾添加到 URL 是一种永久破坏所有缓存并确保用户每次请求页面时都会重复下载完全相同的 static 文件的组合。 You can make the trash less random to break the cache less frequently, but it's still an inefficient kludge.您可以减少垃圾的随机性以减少破坏缓存的频率,但这仍然是一种低效的拼凑。 [Albeit a popular one, to my immense annoyance.] [虽然很受欢迎,但令我非常烦恼。]

Ideally for resource bundles like JS and CSS, you make a new resource bundle file every time you change it, eg: somefile-v1234.js or somefile-20211007.js and update the reference in your HTML source.理想情况下,对于像 JS 和 CSS 这样的资源包,每次更改时都会创建一个新的资源包文件,例如: somefile-v1234.jssomefile-20211007.js并更新 HTML 源中的引用。 This has the side-benefit of ensuring that the versions of your resource bundles always match.这具有确保资源包的版本始终匹配的附带好处。

The same goes for any other static file: images, CSV, etc.这同样适用于任何其他 static 文件:图像、CSV 等。

The trouble you're having now is that you've updated some HTML pages and the only way to break the cache is to have the user perform an action, like hitting CTRL+F5 to force a refresh.您现在遇到的问题是您已经更新了一些 HTML 页,而打破缓存的唯一方法是让用户执行一个操作,例如按 CTRL+F5 以强制刷新。

There are a couple ways around this:有几种方法可以解决这个问题:

  1. Change the Apache/Nginx/etc settings to set shorter expiries for static file cache headers.更改 Apache/Nginx/etc 设置,为 static 文件缓存标头设置更短的到期时间。 You may be able to target specific files like index.html , but YMMV.您可以定位特定文件,例如index.html ,但 YMMV。
  2. Serve the content with PHP. Anything served via a PHP script will not have any cache headers set by default, as the assumption is that the output of a script is dynamic.使用 PHP 提供内容。默认情况下,通过 PHP 脚本提供的任何内容都不会设置任何缓存标头,因为假设脚本的 output 是动态的。 You can also issue the caching headers yourself in PHP to control what gets cached for how long.您还可以在 PHP 中自己发出缓存标头,以控制缓存内容的时间。

Lastly, you cannot solve this problem retroactively.最后,您无法追溯解决此问题。 If a user has a cached version of the HTML page that has not yet reached its expiration, the user MUST take action to break that cache.如果用户有 HTML 页面的缓存版本尚未过期,则用户必须采取措施打破该缓存。 There's nothing that can be done server side because the valid cache tells the client that it doesn't have to ask the server.服务器端没有什么可以做的,因为有效的缓存告诉客户端它不必询问服务器。

Once you get to the point of your application being popular enough to warrant putting a CDN in front of it this problem gets much worse as now there's a cache in the middle that the user doesn't have control of, and it's potentially an expensive problem because some CDN providers charge a fee for forcing CDN cache invalidations.一旦你的应用程序流行到足以保证在它前面放置 CDN,这个问题就会变得更糟,因为现在中间有一个用户无法控制的缓存,这可能是一个代价高昂的问题因为一些 CDN 提供商会收取强制 CDN 缓存失效的费用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM