使用image_tag的相对路径时,查看时间过长

问题描述 投票:1回答:1

我有一些关于电子邮件发送的观点。当我使用类似的东西:

<%= 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

应用程序中的其他所有内容都相同。考虑到这一点,渲染时间差异的原因可能是什么?这是预期的吗?

ruby-on-rails erb actionmailer
1个回答
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

如果传递完整的绝对URL,此函数将快速返回

return source if URI_REGEXP.match?(source)

如果你看一下函数定义上面的注释,他们也说过了

立即返回所有完全限定的URL


您的rails服务器不需要修改绝对图像路径,因为浏览器/电子邮件客户端将直接从源中获取它。在相对路径的情况下,rails在呈现之前需要为其形成绝对URL。

检查他们的整个评论部分here - 他们详细解释了每个场景中会发生什么。

© www.soinside.com 2019 - 2024. All rights reserved.