提供 css 和 js 服务的 cookieless 域 - 组织代码的方式?
我想将我的环境配置为从 static.example.com 而不是 example.com/static 提供 css 和 javascript 文件。
第二个很方便,我可以编写我的 html 页面来相对加载 css 和 javascript 文件,例如独立于实际域的“static/reset.css”。
时,是否有一个好的做法可以避免更改所有源文件,
每当我将static.example.com 更改为 static.otherexample.com
因为我必须重写所有导入 css 和 javascript 的 HTML 源文件?
I would like to configure my environment to serve css and javascript files from static.example.com instead of example.com/static.
The second has the convenience that I can code my html pages to load the css and javascript files relatively eg "static/reset.css" independent of the actual domain.
Is there a good practice to avoid altering all my source files whenever I change
static.example.com to static.otherexample.com
as I would have to rewrite all my HTML source files importing css and javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用某种可配置的前缀来包含 js 和 css 文件。根据您使用的技术,为此使用某种辅助方法会很有帮助。在 ASP.NET MVC 中,我使用一些自定义方法,例如
CSS.Add("reset.css")
,它知道路径和 URL。js 文件不应该真正关心它们是从哪里加载的。对于 CSS,重要的是要知道 CSS 中的相对 URL 被解释为相对于加载 CSS 的 URL,而不是相对于加载页面的 URL。因此,请确保您了解
background-image: url('/images/img1.png')
也会从静态页面加载(这通常是一件好事)。更好的方法
最佳实践是压缩、缩小和合并所有 CSS / js 文件。因此,您应该只拥有非常少量的文件(一个 js,一个 css),以保持较低的请求数量。这些文件的包含将发生在服务器上,因此 URL 并不重要。要实现这一点,您将需要某种辅助方法(以及大量压缩逻辑,但有所有这些的库)。
对于 ASP.NET MVC,有 SquishIt,但我确信有很多工具适用于各种环境。
Use some kind of configurable prefix to include js and css files. Depending on the technology you're using, it is helpful to have some kind of helper method for this. In ASP.NET MVC, I use some custom method like
CSS.Add("reset.css")
, which knows the path and URL.The js files should not really care where they are loaded from. As for css, it's important to know that relative URLs in CSS are interpreted as relative to the URL the CSS was loaded from, not relative to the URL the page was loaded from. So make sure you understand that
background-image: url('/images/img1.png')
would also load from the static page (which is usually a good thing).The better way
It's best practice to compress, minify and merge all of the CSS / js files. Therefore, you should only have a very small number of files (one js, one css) to keep the number of requests low. The inclusion of these files would happen on the server, so the URLs don't matter. To implement this, you will need some kind of helper method (and a lot of compression logic, but there are libraries for all this).
For ASP.NET MVC there is SquishIt, but I'm sure there are plenty of tools for various environments.
html是如何生成的?
您可以使用配置值或常量,并将该值用于任何静态资产的 url 的域部分。
How is the html being generated?
You could use a config value or constant, and use that value for the domain portion of the url for any of the static assets.
你总是可以在服务器上做一些技巧,即 HTML 中的所有 url 都可以相对于
/static
,但是一旦你的服务器收到请求,它就可以“更改”路由并从获取文件>static.current-domain.com
而不是current-domain.com/static
You can always do a trick on the server, i.e. all your urls in HTML could be relative to
/static
, but once your server receive request, it can "change" the route and get files fromstatic.current-domain.com
instead ofcurrent-domain.com/static