require.js - 如何在URL中设置所需模块的版本? [英] require.js - How can I set a version on required modules as part of the URL?

查看:86
本文介绍了require.js - 如何在URL中设置所需模块的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用require.js在我的应用程序中需要JS模块。

I am using require.js to require JS modules in my application.

我需要一种方法来破坏新JS模块上的客户端缓存,通过不同的方式请求的URL。

I need a way to bust client cache on new JS modules, by way of a different requested URL.

即如果文件 hello / there.js 已经缓存在客户端上,我可以更改文件名以强制浏览器获取新文件。

i.e., if the file hello/there.js has already been cached on the client, I can change the file name to force the browser to get the new file.

换句话说,对于模块 hello / there ,我想要require.js来请求url 你好/那里___ v1234 ___。js (文件名看起来不一样,这只是一个例子),根据a可在客户端访问的版本字符串。

In other words, for the module hello/there, I'd like require.js to request the url hello/there___v1234___.js (the file name can look different, it's just an example), according to a version string which is accessible on the client.

实现这一目标的最佳方法是什么?

What is the best way to achieve that?

推荐答案

好的,我为你搜索了requirejs cache bust,找到了这个现有的SO答案,这表示你可以使用 urlArgs 参数配置requireJS,这只是一个部分解决方案,但它可能是足以满足您的直接需求。

OK, I googled "requirejs cache bust" for you and found this existing SO answer, which says you can configure requireJS with an urlArgs parameter, which is only a partial solution, but it might be enough to meet your immediate needs.

也就是说,缓存问题充满挑战,许多解决方案实际上并没有完全解决问题。唯一可维护的方法(目前的恕我直言)是一个全面的资产管理系统,如Ruby on Rails资产管道或连接资产或等效的服务器端框架。这些可以纠正计算每个文件内容的校验和(通常是MD5或SHA1),并为您提供在HTML 脚本标记中作为URL放置的文件名。因此,不要费心根据版本号手动更改文件名,只需使用校验和,因为它们很容易实现自动化和万无一失。

That said, the cachebusting problem is full of challenges and many "solutions" don't actually solve the problem in its entirety. The only maintainable way to do this (IMHO as of now) is with a full-on asset management system like the Ruby on Rails asset pipeline or connect-assets or the equivalent for your server side framework of choice. Those can correct compute a checksum (usually MD5 or SHA1) of the content of each file and give you the file names you need to put as URLs in your HTML script tags. So, don't bother with manually changing filenames based on version numbers, just use checksums since they are easily automated and foolproof.

据我所知,从box requirejs无法为您执行cachebusting方面。您可能需要阅读此google论坛主题。否则,您可能需要将requirejs与其他工具/脚本配对,以获得良好的缓存校验和。

From what I can tell, out of the box requirejs can't do the cachebusting aspect for you. You might want to read this google groups thread. Otherwise, you may need to pair requirejs with an additional tool/script to get you good cachebuster checksums.

这篇关于require.js - 如何在URL中设置所需模块的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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