[{"data":1,"prerenderedAt":737},["ShallowReactive",2],{"\u002Fblog\u002Fone-line-of-rails-config-cut-our-page-load-time-by-65-percent":3},{"path":4,"data":5,"meta":17,"nodes":68},"\u002Fblog\u002Fone-line-of-rails-config-cut-our-page-load-time-by-65-percent",{"title":6,"description":7,"date":8,"tags":9,"authors":11,"image":13,"seo":14},"One Line of Rails Config Cut Our Page Load Time by 65%","Our Rails app was shipping uncompressed payloads to every user. One line in an initializer fixed it. Here's why the obvious version of the fix silently fails.","2026-04-15",[10],"engineering",[12],"Ibraheem Tuffaha","\u002Fimages\u002Fblog\u002Fone-line-of-rails-config-cut-our-page-load-time-by-65-percent.webp",{"title":15,"description":16},"One Line of Rails Config Cut Our Page Load by 65%","Our Lighthouse Performance score jumped from 61 to 92 after enabling gzip in Rails. The fix is one line, but the obvious placement silently fails. Here's why.",{"toc":18,"key":60,"source":61,"stem":62,"extension":63,"kind":64,"type":65,"partial":66,"hash":67},{"title":19,"searchDepth":20,"depth":20,"links":21},"",2,[22,25,28,31,45,48,51,54,57],{"id":23,"depth":20,"text":24},"some-context","Some context",{"id":26,"depth":20,"text":27},"the-suspicious-download","The suspicious download",{"id":29,"depth":20,"text":30},"why-our-app-wasnt-compressing","Why our app wasn't compressing",{"id":32,"depth":20,"text":33,"children":34},"the-one-line-fix-and-the-trap-when-you-verify-it","The one-line fix (and the trap when you verify it)",[35,39,42],{"id":36,"depth":37,"text":38},"the-one-line-fix-and-the-trap-when-you-verify-it-why-the-obvious-placement-looks-broken",3,"Why the obvious placement looks broken",{"id":40,"depth":37,"text":41},"the-one-line-fix-and-the-trap-when-you-verify-it-trying-to-fix-it-in-applicationrb","Trying to fix it in application.rb",{"id":43,"depth":37,"text":44},"the-one-line-fix-and-the-trap-when-you-verify-it-the-real-fix-use-an-initializer","The real fix: use an initializer",{"id":46,"depth":20,"text":47},"the-14-kb-cliff","The 14 KB cliff",{"id":49,"depth":20,"text":50},"the-numbers","The numbers",{"id":52,"depth":20,"text":53},"one-line-whole-app-effect","One line, whole-app effect",{"id":55,"depth":20,"text":56},"a-note-for-server-rendered-rails-apps","A note for server-rendered Rails apps",{"id":58,"depth":20,"text":59},"check-your-own-rails-app","Check your own Rails app","blog\u002Fone-line-of-rails-config-cut-our-page-load-time-by-65-percent.md","blog","one-line-of-rails-config-cut-our-page-load-time-by-65-percent",".md","document","text\u002Fmarkdown",false,"6288f48a6e11129d",[69,79,85,88,91,113,116,118,121,134,137,145,150,152,164,167,169,176,212,218,221,230,239,242,244,249,277,282,299,301,321,324,376,385,390,400,402,405,418,421,435,438,445,452,454,457,633,641,644,647,650,652,655,671,674,676,679,681,687,690,692,734],[70,71,72,73,78],"p",{},"Our inventory page took 4 seconds to start showing content. After one line of Rails config, it takes 1.4. Our ",[74,75,77],"a",{"href":76},"https:\u002F\u002Fdeveloper.chrome.com\u002Fdocs\u002Flighthouse\u002Foverview","Lighthouse"," Performance score jumped from 61 to 92.",[70,80,81],{},[82,83],"img",{"src":84},"\u002Fimages\u002Fblog\u002Fone-line-of-rails-config-cut-our-page-load-time-by-65-percent-1.webp",[70,86,87],{},"The fix was one line in an initializer file. We didn't touch any controller, any view, any Vue component, or any database query. We turned on gzip.",[89,90,24],"h2",{"id":23},[70,92,93,97,98,102,103,107,108,112],{},[74,94,96],{"href":95},"https:\u002F\u002Fmilkstraw.ai\u002F","MilkStraw AI"," is an AWS cost optimization and observability platform. Customers connect their AWS accounts, and we show them saving opportunities alongside dashboards for resources, costs, and metrics. Our web app is Rails 8 with ",[74,99,101],{"href":100},"https:\u002F\u002Finertiajs.com\u002F","Inertia.js"," and ",[74,104,106],{"href":105},"https:\u002F\u002Fvuejs.org\u002F","Vue 3",", deployed with ",[74,109,111],{"href":110},"https:\u002F\u002Fkamal-deploy.org\u002F","Kamal",".",[70,114,115],{},"I was building new CPU charts for the inventory page. Locally everything was fast. After I deployed, I opened DevTools in production and checked one of the API requests. Something felt off.",[89,117,27],{"id":26},[70,119,120],{},"Here's what the CPU charts API request looked like in DevTools:",[122,123,124,128,131],"ul",{},[125,126,127],"li",{},"Waiting: ~140 ms",[125,129,130],{},"Content Download: ~170 ms",[125,132,133],{},"Response size: 77 KB",[70,135,136],{},"Waiting is fine. But Content Download of 170 ms for a 77 KB response works out to ~450 KB\u002Fs. No real network is that slow. I can download cat videos faster than that.",[70,138,139,140,144],{},"I checked the response headers. No ",[141,142,143],"code",{},"Content-Encoding",". The body came back as plain JSON text. Nothing in the request path was compressing anything.",[70,146,147],{},[82,148],{"src":149},"\u002Fimages\u002Fblog\u002Fone-line-of-rails-config-cut-our-page-load-time-by-65-percent-2.webp",[89,151,30],{"id":29},[70,153,154,155,159,160,163],{},"Fresh Rails 8 apps generate with ",[74,156,158],{"href":157},"https:\u002F\u002Fgithub.com\u002Fbasecamp\u002Fthruster","Thruster"," by default, which handles compression out of the box. We upgraded our app from Rails 7 to 8 without adding Thruster, and Rails itself doesn't compress responses: ",[141,161,162],{},"Rack::Deflater"," isn't in the default middleware stack. You have to add it yourself.",[70,165,166],{},"So every JSON response, every Inertia HTML payload, every static asset served through Rails was going out uncompressed. Every single one.",[89,168,33],{"id":32},[70,170,171,172,175],{},"Every blog post on this topic tells you to add this line to ",[141,173,174],{},"config\u002Fapplication.rb",":",[177,178,181],"pre",{"language":179,"class":180},"ruby","shiki shiki-themes github-light-default github-dark-default dark:github-dark-default",[141,182,184],{"class":183},"language-ruby",[185,186,189,193,197,199,202,206,209],"span",{"class":187,"style":188},"line","display: inline",[185,190,192],{"style":191},"color:#1F2328;--shiki-dark:#E6EDF3","config.",[185,194,196],{"style":195},"color:#8250DF;--shiki-dark:#D2A8FF","middleware",[185,198,112],{"style":191},[185,200,201],{"style":195},"use",[185,203,205],{"style":204},"color:#0550AE;--shiki-dark:#79C0FF"," Rack",[185,207,208],{"style":191},"::",[185,210,211],{"style":204},"Deflater",[70,213,214,215,217],{},"I did. Restarted the server. Opened DevTools. Nothing changed. No ",[141,216,143],{}," header.",[219,220,38],"h3",{"id":36},[70,222,223,226,227,229],{},[141,224,225],{},"bin\u002Frails middleware"," showed ",[141,228,162],{}," at the bottom of the stack, innermost next to the app. On the way out, Deflater compresses the body first and everything above it processes the gzipped bytes.",[70,231,232,233,236,237,112],{},"One of those things was ",[141,234,235],{},"Rack::MiniProfiler",", which we only enable for admin users. MiniProfiler tries to parse the HTML body to inject its badge, the parse fails on gzipped content, and the response reaches the browser as plain text with no ",[141,238,143],{},[70,240,241],{},"So the fix would work for normal users. I just couldn't see it as an admin.",[219,243,41],{"id":40},[70,245,246,247,175],{},"I tried forcing Deflater to the front of the stack from ",[141,248,174],{},[177,250,251],{"language":179,"class":180},[141,252,253],{"class":183},[185,254,255,257,259,261,264,267,270,273,275],{"class":187,"style":188},[185,256,192],{"style":191},[185,258,196],{"style":195},[185,260,112],{"style":191},[185,262,263],{"style":195},"insert_before",[185,265,266],{"style":204}," 0",[185,268,269],{"style":191},", ",[185,271,272],{"style":204},"Rack",[185,274,208],{"style":191},[185,276,211],{"style":204},[70,278,279,281],{},[141,280,225],{}," now showed Deflater at position 2, not position 0. Still behind MiniProfiler and ViteRuby.",[70,283,284,285,287,288,291,292,102,295,298],{},"Why? ",[141,286,174],{}," runs before gem railties add their middleware, so my ",[141,289,290],{},"insert_before 0"," slotted into an almost-empty stack. Then ",[141,293,294],{},"rack-mini-profiler",[141,296,297],{},"vite_ruby"," ran their own railties and pushed themselves in front.",[219,300,44],{"id":43},[70,302,303,304,307,308,312,313,316,317,320],{},"Initializer files in ",[141,305,306],{},"config\u002Finitializers\u002F"," run ",[309,310,311],"em",{},"after"," all gem railties have added their middleware. So by the time ",[141,314,315],{},"rack_deflater.rb"," runs, the stack is fully assembled and ",[141,318,319],{},"insert(0, ...)"," lands Deflater at the true outermost position. Nothing runs after it to push it back.",[70,322,323],{},"The fix is one file:",[177,325,326],{"language":179,"class":180},[141,327,328,334,335],{"class":183},[185,329,330],{"class":187,"style":188},[185,331,333],{"style":332},"color:#6E7781;--shiki-dark:#8B949E","# config\u002Finitializers\u002Frack_deflater.rb","\n",[185,336,337,340,342,345,347,350,352,354,356,359,362,365,367,369,371,373],{"class":187,"style":188},[185,338,339],{"style":204},"Rails",[185,341,112],{"style":191},[185,343,344],{"style":195},"application",[185,346,112],{"style":191},[185,348,349],{"style":195},"config",[185,351,112],{"style":191},[185,353,196],{"style":195},[185,355,112],{"style":191},[185,357,358],{"style":195},"insert",[185,360,361],{"style":191},"(",[185,363,364],{"style":204},"0",[185,366,269],{"style":191},[185,368,272],{"style":204},[185,370,208],{"style":191},[185,372,211],{"style":204},[185,374,375],{"style":191},")",[70,377,378,379,381,382,384],{},"Restart the server. ",[141,380,225],{}," now shows ",[141,383,162],{}," as the first middleware in the stack. Open DevTools, reload any page:",[70,386,387],{},[82,388],{"src":389},"\u002Fimages\u002Fblog\u002Fone-line-of-rails-config-cut-our-page-load-time-by-65-percent-3.webp",[70,391,392,395,396,399],{},[141,393,394],{},"Content-Encoding: gzip"," shows up. ",[141,397,398],{},"Content-Length"," disappears because Deflater streams the output in chunks and can't predict the final size upfront.",[89,401,47],{"id":46},[70,403,404],{},"Why does one line of config make such a big difference? Because TCP is lazy on purpose.",[70,406,407,408,412,413,417],{},"When a TCP connection starts sending data, it doesn't blast everything at once. It starts with a small initial burst, waits for the client to acknowledge, then doubles the allowed burst, waits again, doubles again. This is called ",[74,409,411],{"href":410},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FTCP_congestion_control#Slow_start","slow start",". On modern Linux, the initial burst is 10 packets, which works out to about ",[414,415,416],"strong",{},"14 KB of TCP payload"," in the first round trip. TLS framing eats into that further.",[70,419,420],{},"The consequence:",[122,422,423,426,429,432],{},[125,424,425],{},"Up to 14 KB: 1 round trip. Fast.",[125,427,428],{},"Up to ~43 KB: 2 round trips. ~50 ms extra on a decent connection.",[125,430,431],{},"Up to ~100 KB: 3 round trips. ~100 ms extra.",[125,433,434],{},"Bigger than that: more round trips, more waiting.",[70,436,437],{},"On localhost you never notice. On mobile or satellite you feel every round trip.",[70,439,440,441,444],{},"Here's the catch: ",[414,442,443],{},"the 14 KB budget is post-compression",". Raw bytes on the wire. Our 77 KB CPU charts response was three rounds of slow start. After gzip it was 9 KB, one round trip. Sudden cliff, not gentle slope.",[70,446,447,451],{},[74,448,450],{"href":449},"https:\u002F\u002Fendtimes.dev\u002Fwhy-your-website-should-be-under-14kb-in-size\u002F","endtimes.dev has the full version"," with satellite-internet math and diagrams if you want to go deeper.",[89,453,50],{"id":49},[70,455,456],{},"Medians across three runs, before and after the fix:",[458,459,460,479],"table",{},[461,462,463],"thead",{},[464,465,466,470,473,476],"tr",{},[467,468,469],"th",{},"Measurement",[467,471,472],{},"Before",[467,474,475],{},"After",[467,477,478],{},"Delta",[480,481,482,497,511,525,538,552,563,577,591,605,619],"tbody",{},[464,483,484,488,491,494],{},[485,486,487],"td",{},"CPU charts API, response size",[485,489,490],{},"77 KB",[485,492,493],{},"9 KB",[485,495,496],{},"-88%",[464,498,499,502,505,508],{},[485,500,501],{},"CPU charts API, Content Download",[485,503,504],{},"~170 ms",[485,506,507],{},"\u003C1 ms",[485,509,510],{},"-99%",[464,512,513,516,519,522],{},[485,514,515],{},"Inertia request (inventory), transferred",[485,517,518],{},"98 KB",[485,520,521],{},"17 KB",[485,523,524],{},"-82%",[464,526,527,530,533,536],{},[485,528,529],{},"Inertia request (observability), transferred",[485,531,532],{},"105 KB",[485,534,535],{},"19 KB",[485,537,524],{},[464,539,540,543,546,549],{},[485,541,542],{},"Lighthouse Performance (inventory)",[485,544,545],{},"61",[485,547,548],{},"92",[485,550,551],{},"+31",[464,553,554,557,559,561],{},[485,555,556],{},"Lighthouse Performance (observability)",[485,558,545],{},[485,560,548],{},[485,562,551],{},[464,564,565,568,571,574],{},[485,566,567],{},"Lighthouse FCP (inventory)",[485,569,570],{},"4,011 ms",[485,572,573],{},"1,375 ms",[485,575,576],{},"-66%",[464,578,579,582,585,588],{},[485,580,581],{},"Lighthouse LCP (inventory)",[485,583,584],{},"4,510 ms",[485,586,587],{},"1,612 ms",[485,589,590],{},"-64%",[464,592,593,596,599,602],{},[485,594,595],{},"Lighthouse FCP (observability)",[485,597,598],{},"4,030 ms",[485,600,601],{},"1,412 ms",[485,603,604],{},"-65%",[464,606,607,610,613,616],{},[485,608,609],{},"Lighthouse LCP (observability)",[485,611,612],{},"4,406 ms",[485,614,615],{},"1,632 ms",[485,617,618],{},"-63%",[464,620,621,624,627,630],{},[485,622,623],{},"Total page weight (both pages)",[485,625,626],{},"~5.1 MB",[485,628,629],{},"~1.4 MB",[485,631,632],{},"-73%",[70,634,635,636,640],{},"FCP (First Contentful Paint) is the moment the user sees anything on the page. LCP (Largest Contentful Paint) is the moment the main content is visible. LCP is also one of Google's ",[74,637,639],{"href":638},"https:\u002F\u002Fweb.dev\u002Farticles\u002Fvitals","Core Web Vitals",", so it directly affects search ranking.",[70,642,643],{},"Lighthouse was running in desktop mode with simulated throttling at 40 ms network latency and 10 Mbps bandwidth. On mobile the gains would look even bigger, since throttled bandwidth makes compression matter more.",[70,645,646],{},"One more thing on measurement: I also captured Inertia navigation timings from my own browser on a warm HTTP\u002F2 connection, and the receive times barely moved (3.5 to 2.2 ms on inventory, 4.9 to 0.5 ms on observability). Past slow-start, a 100 KB payload arrives in a few milliseconds regardless. There's no wire time left to save.",[70,648,649],{},"The real wins show up in Lighthouse, which simulates a first-time visitor on a throttled network. That's the cold load where slow-start actually bites. And in bandwidth bills you can't see in milliseconds but can see on the invoice.",[89,651,53],{"id":52},[70,653,654],{},"The part that surprised us most: total page weight dropped by roughly 3.7 MB per page, not 85 KB.",[70,656,657,658,662,663,667,668,670],{},"The HTML document only accounts for ~85 KB of that. The other ~3.6 MB comes from JavaScript chunks, CSS files, ",[74,659,661],{"href":660},"https:\u002F\u002Fgithub.com\u002Frails\u002Fpropshaft","Propshaft"," assets, ",[74,664,666],{"href":665},"https:\u002F\u002Fvite.dev\u002F","Vite"," assets routed through Rails, and JSON API endpoints. Because ",[141,669,162],{}," sits at the outermost position, it wraps every single response Rails produces.",[70,672,673],{},"One line. One initializer file. 73% fewer bytes shipped on average per page load across the entire app.",[89,675,56],{"id":55},[70,677,678],{},"If your app is traditional server-rendered Rails (not Inertia), your compression wins will probably be even bigger than ours. Every navigation returns a full HTML document, which is usually larger than an Inertia response that only carries the new page's state and data. More bytes going out, more bytes for compression to shrink.",[89,680,59],{"id":58},[70,682,683,684,686],{},"Open DevTools on your production site right now. Click any request. Look for ",[141,685,394],{}," in the response headers.",[70,688,689],{},"If it's not there, you're shipping uncompressed payloads to every user, right now. Every large response is falling off the 14 KB cliff.",[70,691,323],{},[177,693,694],{"language":179,"class":180},[141,695,696,334,700],{"class":183},[185,697,698],{"class":187,"style":188},[185,699,333],{"style":332},[185,701,702,704,706,708,710,712,714,716,718,720,722,724,726,728,730,732],{"class":187,"style":188},[185,703,339],{"style":204},[185,705,112],{"style":191},[185,707,344],{"style":195},[185,709,112],{"style":191},[185,711,349],{"style":195},[185,713,112],{"style":191},[185,715,196],{"style":195},[185,717,112],{"style":191},[185,719,358],{"style":195},[185,721,361],{"style":191},[185,723,364],{"style":204},[185,725,269],{"style":191},[185,727,272],{"style":204},[185,729,208],{"style":191},[185,731,211],{"style":204},[185,733,375],{"style":191},[70,735,736],{},"Go.",1795440224787]