Performance: Route constraints
Route constraints affect the server-side routing speed, impacting how quickly the server matches URLs to controllers before rendering the page.
Rails.application.routes.draw do get '/products/:id', to: 'products#show', constraints: { id: /\A\d{1,10}\z/ } end
Rails.application.routes.draw do get '/products/:id', to: 'products#show', constraints: lambda { |req| req.params[:id] =~ /\A\d{1,10}\z/ && req.params[:id].to_i < 10000000000 } end
| Pattern | Route Matching Time | Server CPU Load | Impact on LCP | Verdict |
|---|---|---|---|---|
| Complex lambda constraints with regex and logic | High (5-10ms per request) | Higher CPU usage | Delays LCP | [X] Bad |
| Simple regex constraints | Low (1-2ms per request) | Lower CPU usage | Faster LCP | [OK] Good |
route constraints in a Rails application?:id that only allows digits?get '/profile/:username', to: 'users#show', constraints: { username: /[a-z]{3,}/ }get '/orders/:order_id', to: 'orders#show', constraints: { order_id: '\\d+' }Pass the test and this pattern moves unknown