有没有办法使用 CDN(用于 jQuery)并拥有离线 Web 应用程序(通过 HTML5 清单)?
我开始研究 HTML5 允许离线 Web 应用程序的能力。
不久前,我发现使用 CDN 对我的应用程序效果很好,所以我一直坚持使用它们,主要只是针对 jQuery。
但是,清单文件似乎不允许缓存跨域资源。
此时,我一直在使用相关的 深入了解 HTML5 教程 中描述的包罗万象的清单。我的 jQuery 的引入类似于 HTML5 Boilerplate 中的定义。
我希望能够继续通过 CDN 为在线用户提供 jQuery,但也许可以缓存一个本地副本以供离线访问。
是否值得尝试采用这条路线,或者我应该切换到仅从我的网站为所有请求提供 jQuery?
谢谢。
I'm beginning to look at HTML5s ability to allow for offline Web applications.
A while back I found that using a CDN worked well for my applications, so I've been sticking with them, mostly just for jQuery.
However, it doesn't appear that manifest files allows cross-domain resources to be cached.
At this point I've been using the catch-all manifest as described by the relevant Dive Into HTML5 tutorial. My jQuery is pulled in similar to what's defined in the HTML5 Boilerplate.
I'd like to be able to continue to serve jQuery from a CDN for online users, but perhaps have a local copy cached for offline access.
Is it worth trying to pursue this route, or should I just switch over to just serving jQuery from my site, for all requests?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,您可以编写包含 jQuery CDN 链接的清单。就像我的
app.manifest
一样:那是 HTML 段:
它对我有用。
Actually, you can write your manifest that has a link to your jQuery CDN. Just like my
app.manifest
:That is HTML segment:
It works to me.
检查在线 jquery 是否存在以及是否从本地加载的路径已在这里回答:
如何在 CDN 被阻止/不可用的情况下加载本地脚本文件作为后备?
其中有一个讨论和几种不同的方法
个人我喜欢 html5boilerplate 方法:
您可以检查浏览器是否支持使用现代化器进行离线缓存
http://www.modernizr.com/
Modernizer 2.0版本支持条件加载,因此您可以使用它通过以下脚本(取自diveintohtml5)来检测和加载所需的资源:
但正如我之前所说,我更喜欢html5boilerplate方法
the path of checking if online jquery exists and if not loading from local was answered here:
How to load local script files as fallback in cases where CDN are blocked/unavailable?
where there is a discussion and a couple of different approaches
personally I like the html5boilerplate approach:
you can check if the browser supports offline caching with modernizer
http://www.modernizr.com/
the modernizer 2.0 version supports conditional loading , so you can use it to detect and load the needed resource with the following script (taken from diveintohtml5):
but as I said before, I prefer the html5boilerplate method
经过几周的思考,我今晚开车回家时终于有了一个主意。
可以检查浏览器是否支持离线。然后,您可以有一个区域询问用户是否愿意为应用程序启用离线支持。如果这样做,您将加载 jQuery 的本地副本。如果没有,或者不提供离线支持,那么您只需从 CDN 引用 jQuery。
或者它可能是应用程序中的一个单独页面。如果他们访问它,它只是有一个脚本元素,可以拉入您的本地 jQuery 文件。然后页面返回上一页或关闭窗口,具体取决于应用程序。 (我假设添加 jQuery 的本地副本在主页上不起作用,除非您从现有页面中删除 jQuery(覆盖变量或其他方式?)。)
取决于浏览器决定如何保留离线内容(文件),如果本地 jQuery 文件仅在一页上加载(而且这种情况很少见),则可能会出现问题。当然,直到用户离线,此时(可能)每个页面都会调用它。
After weeks of thought I finally had an idea while driving home tonight.
It's possible to check if a browser has offline support. You could then have an area that would ask a user if they'd like to enable offline support for an application. If they do, you load in your local copy of jQuery. If not, or offline support isn't available, then you just reference jQuery from a CDN.
Or perhaps it's a separate page in the application. If they visit it it simply has a script element that pulls in your local jQuery file. Then the page either goes back one or the window is closed, depending upon the application. (I assume adding your local copy of jQuery wouldn't work on the main page, unless you removed jQuery from the existing page (overwriting the variables or otherwise?).)
Depending upon how the browser(s) decide to hold onto offline content (files), there may potentially be an issue if the local jQuery file is only loaded on one page, and very rarely. Until the user is offline, of course, at which point it is called on (potentially) every page.