生产模式下的 Rails 从 public/assets
文件夹中提供资源,如下所示:
/assets/application-c3967f04f81628153a30120e3f5b8695.js
在开发模式下,从 app/assets
文件夹中提供服务,如下所示
/assets/application.js?body=1
或
/assets/application-c3967f04f81628153a30120e3f5b8695.js
我想知道 Rails 是如何解决这个问题的。因为我试图在 Nodejs 中做同样的事情,所以我认为一种方法可能是这样的:
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
app.use(express.static(path.join(config.root, 'app')));
} else if ('production' == env) {
app.use(express.static(path.join(config.root, 'public')));
};
在 NodeJS 中实现这一目标的替代方案是什么?
最佳答案
指纹识别
您所指的被称为 asset fingerprinting
这是 Rails 确定生产中 Assets 的唯一 URL 的一种方式,允许您使用类似 precompilation
的内容。以静态方式为您的 Assets 提供服务,从而更快
如 Rails 文档中所述:
Fingerprinting is a technique that makes the name of a file dependent on the contents of the file. When the file contents change, the filename is also changed. For content that is static or infrequently changed, this provides an easy way to tell whether two versions of a file are identical, even across different servers or deployment dates.
--
路径助手
您遇到的问题是,当您在开发中使用 Rails 时,它将提供 app/assets
目录中的资源。但是,当您将应用投入生产时,应用将改为从 public
目录提供资源
这是一个问题,因为这意味着您必须进行一些条件路径加载才能使其正常工作。解决这个问题的方法是使用 asset path helpers
这些仅适用于 .erb
等预处理器,允许您调用以下内容:
asset_url("your_asset_url")
这将独立加载资源路径,无论您处于生产模式还是开发模式。
<小时/>NodeJS
也许您可以通过查看 source for asset_url
path helper 获得更好的见解在 Rails 中
关于javascript - 为开发与生产中的 Assets 提供服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24472480/