AgentSkillsCN

turbo-frames

借助Hotwire,运用Turbo Frames模式,将页面拆分为以领域为核心的多个模块。在实现内容懒加载、打造基于框架的交互体验,或对复杂页面进行合理化布局时,可选用此方法。内容涵盖框架加载策略(立即加载/懒加载)、框架响应结构,以及基于领域的拆分方式。

SKILL.md
--- frontmatter
name: turbo-frames
description: Turbo Frames patterns for decomposing pages into domain-based segments with Hotwire. Use when implementing lazy-loading content, creating frame-based interactions, or organizing complex pages. Covers frame loading strategies (eager/lazy), frame response structure, and domain-driven decomposition.

Turbo Frames (Hotwire Decomposition)

Dependencies

  • hotwire-turbo-rails
  • turbo-rails

Overview

Use Turbo Frames to decompose complex pages into domain-based segments. Frames typically represent meaningful domain concepts rather than atomic UI elements.

Frame Loading Strategies

Eager-Loading Frames

Load immediately when the page renders. Use for above-the-fold content that should be visible on initial page load.

ruby
# app/views/pages/index.rb
turbo_frame_tag("featured_articles", src: featured_articles_path)

When to use:

  • ✓ Content needed immediately
  • ✓ Critical for page layout
  • ✓ Content in viewport on load
  • ✗ Heavy/expensive queries

Lazy-Loading Frames

Load only when the frame becomes visible (scrolls into view). Use for below-the-fold content.

ruby
# app/views/pages/index.rb
turbo_frame_tag("featured_articles", src: featured_articles_path, loading: :lazy)

When to use:

  • ✓ Below-the-fold content
  • ✓ Optional/supplementary sections
  • ✓ Improving initial page load time
  • ✗ Critical above-fold content

Frame Response Structure (Example)

Example response wraps content in a matching turbo-frame tag. See the checklist for requirements.

ruby
# app/views/articles/featured.rb (response)
turbo_frame_tag("featured_articles") do
  # Frame content here
end

Domain-Based Frame Design

Example domain concepts for frames:

code
GOOD (Domain Concepts):
- featured_articles (Articles domain)
- user_notifications (Notifications domain)
- activity_feed (Activity domain)

BAD (Atomic/UI only):
- card_section
- button_group
- header_nav

Controller Structure for Frames

Example: dedicated controller actions for frames using collection routes (custom actions on a resource):

ruby
# config/routes.rb
resources :articles do
  collection do
    get :featured  # Creates GET /articles/featured
  end
end

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  def featured
    render Views::Articles::Featured.new
  end
end

RESTful Pattern with Turbo Frames

Route naming convention example:

ruby
resources :articles do
  collection do
    get :featured      # Articles domain, featured subset
    get :popular       # Articles domain, popular subset
    get :trending      # Articles domain, trending subset
  end
end

Custom collection actions can work well when the action describes a domain subset consumed by a Turbo Frame.

Frame ID and Route Notes

See requirements in:

Component vs Frame Trade-offs

AspectComponentFrame
PurposeReusable UI building blockPage decomposition/domain segment
Locationapp/components/app/views/ with controller
Data SourceConstructor argumentsDatabase query via controller
CachingCan be expensive (per-request)HTTP caching friendly
NavigationNo link/form handlingFull link/form scope
Use CaseArticle card in a listEntire featured articles section

For detailed frontend/backend checklists, see VERIFICATION_CHECKLIST.md