Module: Turbo::DriveHelper

Defined in:
app/helpers/turbo/drive_helper.rb

Instance Method Summary collapse

Instance Method Details

#turbo_exempts_page_from_cacheObject

Pages that are more likely than not to be a cache miss can skip turbo cache to avoid visual jitter. Cannot be used along with turbo_exempts_page_from_preview.



22
23
24
# File 'app/helpers/turbo/drive_helper.rb', line 22

def turbo_exempts_page_from_cache
  provide :head, turbo_exempts_page_from_cache_tag
end

#turbo_exempts_page_from_cache_tagObject

See turbo_exempts_page_from_cache.



27
28
29
# File 'app/helpers/turbo/drive_helper.rb', line 27

def turbo_exempts_page_from_cache_tag
  tag.meta(name: "turbo-cache-control", content: "no-cache")
end

#turbo_exempts_page_from_previewObject

Specify that a cached version of the page should not be shown as a preview during an application visit. Cannot be used along with turbo_exempts_page_from_cache.



33
34
35
# File 'app/helpers/turbo/drive_helper.rb', line 33

def turbo_exempts_page_from_preview
  provide :head, turbo_exempts_page_from_preview_tag
end

#turbo_exempts_page_from_preview_tagObject

See turbo_exempts_page_from_preview.



38
39
40
# File 'app/helpers/turbo/drive_helper.rb', line 38

def turbo_exempts_page_from_preview_tag
  tag.meta(name: "turbo-cache-control", content: "no-preview")
end

#turbo_page_requires_reloadObject

Force the page, when loaded by Turbo, to be cause a full page reload.



43
44
45
# File 'app/helpers/turbo/drive_helper.rb', line 43

def turbo_page_requires_reload
  provide :head, turbo_page_requires_reload_tag
end

#turbo_page_requires_reload_tagObject

See turbo_page_requires_reload.



48
49
50
# File 'app/helpers/turbo/drive_helper.rb', line 48

def turbo_page_requires_reload_tag
  tag.meta(name: "turbo-visit-control", content: "reload")
end

#turbo_refresh_method_tag(method = :replace) ⇒ Object

Configure method to perform page refreshes. See turbo_refreshes_with.

Raises:

  • (ArgumentError)


77
78
79
80
# File 'app/helpers/turbo/drive_helper.rb', line 77

def turbo_refresh_method_tag(method = :replace)
  raise ArgumentError, "Invalid refresh option '#{method}'" unless method.in?(i[ replace morph ])
  tag.meta(name: "turbo-refresh-method", content: method)
end

#turbo_refresh_scroll_tag(scroll = :reset) ⇒ Object

Configure scroll strategy for page refreshes. See turbo_refreshes_with.

Raises:

  • (ArgumentError)


83
84
85
86
# File 'app/helpers/turbo/drive_helper.rb', line 83

def turbo_refresh_scroll_tag(scroll = :reset)
  raise ArgumentError, "Invalid scroll option '#{scroll}'" unless scroll.in?(i[ reset preserve ])
  tag.meta(name: "turbo-refresh-scroll", content: scroll)
end

#turbo_refreshes_with(method: :replace, scroll: :reset) ⇒ Object

Configure how to handle page refreshes. A page refresh happens when Turbo loads the current page again with a replace visit:

Parameters:

  • method - Method to update the <body> of the page during a page refresh. It can be one of:

    • replace:: Replaces the existing <body> with the new one. This is the

    default behavior.

    • morph:: Morphs the existing <body> into the new one.

  • scroll - Controls the scroll behavior when a page refresh happens. It can be one of:

    • reset:: Resets scroll to the top, left corner. This is the default.

    • preserve:: Keeps the scroll.

Example Usage:

turbo_refreshes_with(method: :morph, scroll: :preserve)


71
72
73
74
# File 'app/helpers/turbo/drive_helper.rb', line 71

def turbo_refreshes_with(method: :replace, scroll: :reset)
  provide :head, turbo_refresh_method_tag(method)
  provide :head, turbo_refresh_scroll_tag(scroll)
end