我有一些关于电子邮件发送的观点。当我使用类似的东西时:
<%= image_tag('http://example.com/assets/image.png') %>
电子邮件正在拍摄这些内容:
Rendering mailers/user_mailer/verification_code_email.html.erb within layouts/mailer
Rendered mailers/_header.html.erb (6.4ms)
Rendered mailers/_footer.html.erb (0.6ms)
Rendered mailers/_email_style.html.erb (0.3ms)
Rendered mailers/user_mailer/verification_code_email.html.erb within layouts/mailer (17.3ms)
UserMailer#verification_code_email: processed outbound mail in 395.8ms
另一方面,当我使用相对路径时:
<%= image_tag('image.png') %>
电子邮件的渲染时间要长得多:
Rendering mailers/user_mailer/verification_code_email.html.erb within layouts/mailer
Rendered mailers/_header.html.erb (1222.9ms)
Rendered mailers/_footer.html.erb (3.0ms)
Rendered mailers/_email_style.html.erb (0.3ms)
Rendered mailers/user_mailer/verification_code_email.html.erb within layouts/mailer (1244.4ms)
UserMailer#verification_code_email: processed outbound mail in 1626.5ms
应用程序中的其他所有内容都相同。考虑到这一点,渲染时间差异的原因可能是什么?这是预期的吗?
答案 0 :(得分:1)
查看image_tag的rails源代码。它最终调用了def asset_path
def asset_path(source, options = {})
raise ArgumentError, "nil is not a valid asset source" if source.nil?
source = source.to_s
return "" if source.blank?
return source if URI_REGEXP.match?(source)
tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, "".freeze)
if extname = compute_asset_extname(source, options)
source = "#{source}#{extname}"
end
if source[0] != ?/
if options[:skip_pipeline]
source = public_compute_asset_path(source, options)
else
source = compute_asset_path(source, options)
end
end
relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
if relative_url_root
source = File.join(relative_url_root, source) unless source.starts_with?("#{relative_url_root}/")
end
if host = compute_asset_host(source, options)
source = File.join(host, source)
end
"#{source}#{tail}"
end
如果您传递完整的绝对网址
,此功能将很快返回return source if URI_REGEXP.match?(source)
如果你看一下功能定义上面的评论,他们也说了
立即返回所有完全限定的网址
您的rails服务器无需修改绝对图像路径,因为浏览器/电子邮件客户端将直接从源中获取它。在相对路径的情况下,rails在呈现之前需要为其形成绝对URL。
检查他们的整个评论部分here - 他们会详细解释每种情况会发生什么。