更改 css 中的图像域/路径以进行生产?

发布于 2024-08-31 13:06:25 字数 330 浏览 3 评论 0原文

目前,对于背景图像之类的内容,我们的 css 文件没有指定域。这在我们的开发和生产环境中都有效。

background-image: url(/images/bg.png);

出于性能原因(无 cookie 域),我们希望对此进行切换:

background-image: url(http://staticimagedomain.com/images/bg.png);

理想情况下,我们不会对这些进行硬编码,因此我们的开发环境仍然可以在本地进行拉取。

关于如何最好地实现这一目标有什么想法吗?

Currently, for things like background images, our css files have no domain specified. This works both in our development and production environments.

background-image: url(/images/bg.png);

For performance reasons (cookie-less domain), we'd like to switch this:

background-image: url(http://staticimagedomain.com/images/bg.png);

Ideally, we don't hard code those, so our development environments can still pull locally.

Any thoughts on how to best achieve this?

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

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

发布评论

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

评论(3

何其悲哀 2024-09-07 13:06:25

我建议将 CSS 文件也托管在无 cookie 的域中,而不仅仅是图像。然后您将能够使用相对路径,这在生产和任何其他环境中都适用。

如果您将 CSS 文件部署到无 cookie 的域:

http://staticimagedomain.com/main.css

那么您可以保留相对 URL /images/bg.png,这在两种环境中都可以正常工作。

CSS 文件无论如何都不需要cookie。

I would suggest hosting the CSS files in the cookie-less domain as well, and not just the images. Then you would be able to use relative paths, which will work both in production and in any other environment.

If you deploy your CSS file to the cookie-less domain:

http://staticimagedomain.com/main.css

Then you can leave the relative URLs /images/bg.png, which will work fine from both environments.

CSS files don't need cookies anyway.

心是晴朗的。 2024-09-07 13:06:25

主机文件中的条目将 staticimagedomain.com 重定向到 localhost 或测试页面提供的任何位置?

这不是一个很好的解决方案,但又快速又简单。

Entry in the hosts file to redirect staticimagedomain.com to localhost or wherever your test pages serve from?

Not a great solution, but quick and easy.

过度放纵 2024-09-07 13:06:25

添加到其他明智的答案:有时拥有两个样式表很有用,一个用于生产,另一个用于开发,只有一些差异(例如:一些背景颜色),以帮助直观地识别实例并防止混淆。您的构建部署脚本 (Ant) 将在投入生产时负责移动 css 文件。

如果您正在这样做(或者认为这样做会很好),那么您的问题就有一个简单的答案。

To add to the other sensible answers: Sometimes is useful to have two stylesheets, one for production other for development, with just a few differences (eg: some background-colors) to help identify visually the instances and prevent confusions. Your build-deployment script (Ant) would take care of moving the css files when going to production.

If you are doing this (or think it would be good to do this), then your question has a trivial answer.

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