Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Railsのエラーページ運用について本気出して考えてみた

B8e501d93b98a553abf0b5cee0c33503?s=47 yasaichi
August 29, 2015

 Railsのエラーページ運用について本気出して考えてみた

TokyuRuby会議09(2015/08/29)

B8e501d93b98a553abf0b5cee0c33503?s=128

yasaichi

August 29, 2015
Tweet

Transcript

  1. RailsͷΤϥʔϖʔδӡ༻ʹ
 ͍ͭͯຊؾग़ͯ͠ߟ͑ͯΈͨ TokyuRubyKaigiɹ2015/08/29 @yasaichi

  2. About me [1] pry(main)> me => { twitter:'@_yasaichi', github: 'yasaichi',

    splatoon: nil, blog: 'http://web-salad.hateblo.jp' }
  3. Outline 1. എܠ 2. ຊؾग़ͯ͠ߟ͑ͯΈͨ݁Ռ 3. ޙ೔ஊ 4. ·ͱΊ

  4. Background

  5. Motivation • ٱʑʹΤϥʔϖʔδΛ͍ͬͨ͡ΒͭΒ͔ͬͨ • ςϯϓϨʔτΤϯδϯ͕࢖͑ͳ͍ • public/assetsҎԼΛࢀরͰ͖ͳ͍ • ෦෼ςϯϓϨʔτ͕ར༻Ͱ͖ͳ͍

  6. Previous works • Rails.application.config.exceptions_appΛ
 ࢖ͬͯಈతʹΤϥʔϖʔδΛੜ੒͢Δ • RailsͰΤϥʔϖʔδΛಈతʹ
 http://qiita.com/r7kamura/items/1435823b1703df0402ee • yuki24/rambulance

    (Gem)
 https://github.com/yuki24/rambulance
  7. Limitations Railsͱؔ܎ͳ͍੩త ϖʔδ͸Ͳ͏͢Δͷʁ

  8. ੩తϑΝΠϧ͕ඞཁʁ
 assetͷϦϯΫ੾Ε໰୊ʁ

  9. ɾɾɾ

  10. (ƅшƅ)Űŕ!

  11. Τϥʔϖʔδ΋Asset PipelineͰ؅ཧ͢Ε͹
 ͍͍ͷͰ͸ʁ

  12. Proposed method

  13. Summary 1. ࢖༻͍ͨ͠ςϯϓϨʔτΤϯδϯΛొ࿥ 2. app/assets/templatesʹΤϥʔϖʔδΛॻ͖ɺ
 Asset Pipelineͷ؅ཧର৅ͱ͢Δ 3. assets:precompileΛhookͯ͠ΤϥʔϖʔδΛ
 publicҎԼʹҠ͢ॲཧΛॻ͘

    4. σϓϩΠ࣌ʹrake assets:precompileΛ࣮ߦ͢Δ
  14. 1. Register template engine # config/initializers/assets.rb # Use Haml Rails.application.assets.register_engine('.haml',

    Tilt::HamlTemplate) # Use Slim # slim-rails 3.0.1 Ҏ߱Ͱ͸ෆཁ Rails.application.assets.register_engine('.slim', Slim::Template)
  15. 2. Write error pages / app/assets/templates/404.html.slim doctype html html head

    title TestApp = stylesheet_link_tag 'error', media: 'all' body p 404.html # config/initializers/assets.rb Rails.application.config.assets.precompile << 'error.css'
  16. 3. After hook for precompile # lib/tasks/after_precompile.rb Rake::Task['assets:precompile'].enhance do logger

    = Logger.new(STDOUT) %w(404 422 500 503).each do |status_code| pattern = Rails.public_path.join("assets/#{status_code}-*.html") src = Pathname.glob(pattern).sort_by { |path| path.mtime }.last next if src.nil? dest = Rails.public_path.join("#{status_code}.html") logger.info("mv #{src} to #{dest}") FileUtils.mv(src, dest) end end digestΛऔΓআ͍ͯ
 public/ҎԼʹҠ͢
  17. 4. Precompile on deploy % RAILS_ENV=production rake assets:precompile I, [2015-08-28T01:07:37.256609

    #39050] INFO -- : mv /path/to/public /assets/404-f26c195d5a2d8b404e020dc08dcfc5856940832aff1906b2eadc584a cbbc8735.html to /path/to/public/404.html <!-- public/404.html —> <!DOCTYPE html> <html> <head> <title>TestApp</title><link rel="stylesheet" media="all" href="/assets/ error-d46af6b953e1af51c1cc79e736904476e9c33f651b6264f2973ec7690b894112.css" /> </head> <body> <p>404.html</p> </body> </html>
  18. Strong points • ςϯϓϨʔτΤϯδϯ͕࢖͑Δ • Sprockets::Rails::HelperͰ࣮૷͞Ε͍ͯΔ
 assetؔ࿈ͷhelper͕࢖͑ΔʢҎԼൈਮʣ asset_path, asset_url, audio_path,

    audio_tag, audio_url, content_tag, favicon_link_tag, font_path, font_url, image_path, image_tag, image_url, javascript_include_tag, javascript_path, javascript_url, path_to_asset, stylesheet_link_tag, stylesheet_path, stylesheet_url, tag, video_path, video_tag, video_url
  19. Weak points • renderϝιου͕࢖͑ͳ͍ͷͰɺϔομʔ
 ͳͲڞ௨ͷςϯϓϨʔτΛಡΈࠐΊͳ͍ • मਖ਼࣌ʹදࣔΛ֬ೝ͢Δͷ͕΍΍໘౗ʢ˞ʣ 
 ※ railsΛ։ൃ؀ڥͰ্ཱͪ͛ͯ/assetsҎԼʹΞΫηε͢Δ͚ͩ


    ɹ͕ͩɺͦΕΛσβΠφʔ͞Μʹ΍ͬͯ΋Β͏৔߹Λߟ͑Δͱ…
  20. After a few days …

  21. ͜Ε͸͍͍ઢ͍ͬͯΔͷͰ͸ʁ ͱࢥͬͯϒϩάΛॻ͍ͨ http://web-salad.hateblo.jp/entry/2015/07/11/130500

  22. ݫ͍͠ݱ࣮ Effective RubyΛಡΜͩͷͰײ૝Λॻ͍ͯ͘ɺͱ͔ͷํ͕΢έ͕ྑ͍ ձࣾͷઌഐ͕ελʔΛ ͚ͭͯ͘Εͨʢ਎಺ײʣ

  23. ԶͷΤϯτϦ͕͜Μͳʹ
 όζΒͳ͍Θ͚͕ͳ͍ʂ

  24. ΧοͱͳͬͯgemΛ࡞ͬͨ

  25. None
  26. gakubuchi • https://github.com/yasaichi/gakubuchi • جຊతͳ࢓૊Έ͸ࠓ·Ͱઆ໌ͨ͠௨Γ • ੩తϖʔδશൠʹద༻Ͱ͖ΔΑ͏ʹͨ͠ • Τϯδϯ΍ϑοΫ͸ࣗಈͰઃఆ͞ΕΔͷͰ ੩తϖʔδͷ࣮૷ʹूதͰ͖Δ

  27. Overview gem 'gakubuchi' gem 'haml-rails' % rails generate gakubuchi:install create

    config/initializers/gakubuchi.rb create app/assets/templates % touch app/assets/templates/foo.html.haml % rake assets:precompile % cd public && ls *.html 404.html 422.html 500.html foo.html Կ΋ઃఆͯ͠ͳ͍ ͷʹͰ͖ͨʂ
  28. ৄ͘͠͸ҎԼͷΤϯτϦͰ http://web-salad.hateblo.jp/entry/2015/08/17/090000

  29. Conclusion

  30. Conclusion 1. RailsͰΤϥʔϖʔδΛॻ͘ͷ͕ͭΒ͔ͬͨ 2. ΤϥʔϖʔδΛAsset PipelineͰ؅ཧͯ͠
 ָΛ͢Δख๏ΛఏҊͨ͠ 3. ఏҊख๏ΛΑΓҰൠతͳέʔεʹద༻Ͱ͖Δ
 gakubuchiͱ͍͏gemΛ࡞ͬͨ

  31. Happy coding on Rails!