将 .css 和 .js 文件放在 CDN 上是否明智?

发布于 2024-09-08 12:28:32 字数 57 浏览 3 评论 0原文

似乎大多数人都使用 CDN 来放置图像和/或视频。将 js 和 css 也放在 CDN 上是否明智?

It seems like most people use a CDN for placing images and/or videos. Is it wise to place your js and css on a cdn as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心凉怎暖 2024-09-15 12:28:32

虽然原则上这是个好主意,但始终要有备份

使用 Javascript,如果 CDN 出现故障或客户端由于某种原因无法访问该 js,那么拥有本地副本作为备份将使您的网站保持灵活性。

例如,使用 jQuery,您可以通过以下代码段获得两全其美的效果:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined')
    document.write(unescape("%3Cscript src='/scripts/jquery-1.4.2.min.js' type='text/javascript'%3E%3C/script%3E"));
</script>

来源和更多信息

While it's a good idea in principle, always have a backup.

With Javascript, if the CDN goes down or the client can't access that js for some reason, then having a local copy as backup will keep your site flexible.

For example, with jQuery, you can have the best of both worlds with this snippet:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined')
    document.write(unescape("%3Cscript src='/scripts/jquery-1.4.2.min.js' type='text/javascript'%3E%3C/script%3E"));
</script>

source and more info

孤独患者 2024-09-15 12:28:32

我相信是这样。但是,如果其中一个是动态生成的,您将需要在文件上设置相对较低的 ttl(生存时间),以便 CDN 知道提取新副本。但是,如果不是,我认为将它们转储到 Amazon S3 上就可以了。

需要注意的是 - 确保您有一种简单的方法来测试和开发而不使用 CDN,因为在此过程中缓存可能是一个令人头痛的问题。

[编辑]
经过更多经验后,我相信最好对这些文件进行版本控制,然后使用生成视图的任何内容更新它们的引用。

例如:

style.20111112.css
Shoppingcart.20111113.js

这消除了超低 ttl 和清除缓存副本的 CDN 的需要。

I believe so. However, if either is dynamically generated you will want to place a relatively low ttl (time to live) on the files so the CDN knows to pull fresh copies. However, if they are not, I would think that dumping them onto Amazon's S3 would be fine.

One caveat - make sure you have an easy way to test and develop without using the CDN as cacheing can be a headache during this process.

[edit]
After some more experience, I believe it's best to version these files and then update their references w/i whatever generates the views.

For example:

style.20111112.css
shoppingcart.20111113.js

This clears the need to have super low ttl's and purge the CDN of the cached copies.

锦欢 2024-09-15 12:28:32

是的,但是...在将 CSS 和 JS 放在 CDN 上之前,

请合并所有单独的 CSS 文件并最小化它们 - 在全部下载完毕之前,浏览器不会开始渲染页面。

计算出页面需要下载哪些 JS 文件以及稍后可以下载哪些文件,然后将需要的文件放在页眉中,将其他文件放在页面底部或异步加载它们(不要忘记缩小它们) CDN

给您带来了一些好处

  1. 减少延迟
  2. 可以从其中下载额外的主机名
  3. 如果您正确使用它,则可以使用无 cookie 域来存储静态内容。

在前几天进行的演示中,有一些关于不同优化性能的真实世界示例 - https://docs.google.com/present/view?id=0ASIJhpwrlBx6ZHA5emJmcF81MmM5Zzk3NmM0&authkey=COe29aAD&hl=en_GB(不是我的)

Yes but... before you place the CSS and JS on a CDN

Merge any separate CSS files and minimise them - the browser won't start rendering the page until they're all downloaded.

Work out which JS files you need for the page to download and which ones can be downloaded later, then place the ones needed in the header, and the others at the foot of the page or load them asyncronously (don't forget to minify them too)

A CDN gives you a couple of wins

  1. Reduced latency
  2. Extra hostnames to download from
  3. If you use it right a cookieless domain for static content.

There's some real world examples on the performance of different optimisations in this presentation that was doing the rounds the other day - https://docs.google.com/present/view?id=0ASIJhpwrlBx6ZHA5emJmcF81MmM5Zzk3NmM0&authkey=COe29aAD&hl=en_GB (it's not mine)

怪我入戏太深 2024-09-15 12:28:32

我不认为它有什么问题(特别是对于 javascript,如果你有多个网站使用相同的 javascript 代码)。

I don't think there is anything wrong with it (especially for javascript if you have multiple websites using the same javascript code).

温暖的光 2024-09-15 12:28:32

这里的大部分答案都是由没有 CDN 经验的人写的。

如果您使用 Akamai 或其他大型 CDN 提供商之一,您会发现 CDN 会访问您的网站获取内容(因此您不会“备份”数据),然后缓存数据,直到您使其失效。

像 Akamai 这样的 CDN 将产生巨大的影响 - 除非您的边缘服务器分布在全球各地。我们在德国的一些客户的页面加载时间从 15 秒缩短到不到 3 秒。

我建议阅读 wikipedia 的好处部分以了解更多信息。

Most of the answers here are written from people without CDN experience.

If you use Akamai or one of the other large CDN providers you'll find that CDNs hit your site for the content (thus you don't "backup" your data), and then cache the data until you invalidate it.

A CDN like Akamai will make a huge difference -- unless you have edge servers distributed around the globe. Some of our customers in Germany went from 15 second page load times to less than 3 seconds.

I suggest reading the benefits section of wikipedia to learn more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文