在生产中角禁用缓存谐音 [英] Angular disable cached partials in production

查看:189
本文介绍了在生产中角禁用缓存谐音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发网站的proccess,但每次我上传新版本我的服务器的用户需要清除缓存有看到的变化,解决这个问题是什么?

I'm in proccess of development site, but every time i upload a new version to my server the users need to clean there cache to see the changes, what is the solution to this problem?

我尝试两种方法可以解决从回答这件事的问题在这里:

I try two ways to solve this thing from answers to questions here:

第一个的方式:

myApp.run(function($rootScope, $templateCache) {
   $rootScope.$on('$viewContentLoaded', function() {
      $templateCache.removeAll();
   });
});

但这种方式有一个东西像问题 ui.bootstrap 和它扔错误。

第二的方式:

$rootScope.$on("$stateChangeStart", function( event, toState, current ) {

    if (typeof(current) !== 'undefined'){
      $templateCache.remove(current.templateUrl);
    }
})

这不会引发错误,但我不知道,如果这个解决方案是正确的,如果它的工作,我需要做什么?

This not throw error, but i dont know if this solution is the right one and if it work, what i need to do?

推荐答案

这是与服务器配置有问题,以及它如何告诉浏览器缓存 HTML 文件作为模板的谐音。

This is a problem with the server configuration, and how it tells the browser to cache the HTML files used as template partials.

您正在尝试做的 $ templateCache.remove(..) $ templateCache.removeAll()这是行不通的,因为模板尚未装入的是,或使用缓存版本浏览器只会重新加载它们。

You are attempting to do $templateCache.remove(..) and $templateCache.removeAll() which is not going to work, because the templates haven't been loaded yet, or the browser will just reload them using the cached version.

您需要检查网络历史中的浏览器,以确保服务器正确处理HTML请求。发送标题设置为你希望缓存。对于每个部分,应该有一个 302 (未改性的)的反应。当HTML文件已经改变,则服务器将发送新修改的一个浏览器。

You need to check the network history in the browser to ensure that the server is handling HTML requests correctly. Send the header settings for caching that you expected. For each partial, there should be a 302 (not modified) response. When the HTML file has changed then the server should send the new modified one to the browser.

另一个问题是在服务器上默认的缓存持续时间可能太长。这告诉浏览器应该多长时间检查一次服务器发送HTML文件的新版本。你可能想这个时间缩短至1小时。

The other problem is your default cache duration on the server might be too long. This tells the browser how often it should check the server to send new versions of the HTML file. You might want to reduce this duration to 1 hour.

大多数Web服务器是pre-配置为承担具有扩展名的文件的.html 将永远不会改变,是静态的。因此,默认设置是让浏览器缓存这些文件为长的时间。

Most web servers are pre-configured to assume a file with the extension .html will never change and is static. So the default settings are to have the browser cache those files for as a long as possible.

您可能希望将这些文件重命名为类似 .ahtml 并使用它作为你的部分文件名。不应该有任何pre定义的配置,迫使这些文件类型的兑现。

You might want to rename those files to something like .ahtml and use that as your partial file names. There shouldn't be any pre-defined configuration to force cashing of those file types.

这是AngularJS一个非常简单的缓存服务。如果在存储器中没有找到该URL是从服务器加载。正在使用的部分,而应用程序运行内存版本,需要下一次。此服务器的使用无关,与服务器/浏览器缓存。

This is a very simple cache service in AngularJS. If the URL is not found in memory it is loaded from the server. The next time that partial is needed while the app is running the memory version is used. The usage of this server has nothing to do with server/browser caching.

这是创建一个包含所有的部分模板的应用程序将需要一个单独的JavaScript文件的最佳实践。此JS文件将调用 $ templateCache 服务,并添加这些模板。这提高了应用程序的启动性能,并删除处理的服务器/浏览器缓存的问题。

It's a best practice to create a single JavaScript file that contains all the partial templates the application will need. This JS file will call the $templateCache service and add those templates. This improves the startup performance of your application, and removes the problem of dealing with server/browser caching.

有可以做这件事的几个咕噜任务。

There are several grunt tasks that can do this.

https://www.npmjs.com/package/grunt-angular-templates

https://github.com/karlgoldstein/grunt-html2js

联合,这将一个JavaScript mi​​nifier,你所有的JS Concat的到一个单一的文件,并且你对你的方式。

Combine this will a JavaScript minifier and concat all your JS into a single file, and you're well on your way.

这篇关于在生产中角禁用缓存谐音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆