属性とメタタグ
# データ属性
次のデータ属性を要素に対して利用することで、Turbo の挙動をカスタマイズできます。
data-turbo="false"は、リンクやフォーム、その子孫要素に対しても Turbo ドライブを無効にします。Turbo ドライブを有効にしたい場合は、data-turbo="true"を使います。Turbo ドライブを無効にする場合、振る舞いの違いに注意してください。ブラウザではリンクをクリックした時の振る舞いが通常通りになるだけですが、native adaptersではアプリが終了するかもしれません。また、Turbo が無視するパスとして扱っている URL については、data-turbo="true"を付けても Turbo ドライブを有効にはできません。data-turbo-track="reload"はHTML要素を追跡し、それが変わったときに全ページをリロードします。通常、scriptやCSSのリンクを最新の状態に保つために使われます。data-turbo-track="dynamic"は、要素のHTMLを監視し、HTMLレスポンスに含まれていなければその要素を削除します。主に、ナビゲーション中にstyleやlink要素を削除するために使われます。data-turbo-frameは、ナビゲートするための Turbo フレームを識別します。詳細は、フレームのドキュメントを参照してください。data-turbo-preloadは、Turbo ドライブに次のページのコンテンツをプリフェッチさせます。data-turbo-prefetch="false"は、要素にマウスホバーしたときのリンクのプリフェッチを無効にします。data-turbo-actionは、Visitアクションをカスタマイズします。有効な値は、replaceあるいはadvanceのいずれかです。 Turbo フレームと一緒に使うことで、フレームのナビゲーションをページアクセスに昇格できます。data-turbo-permanentは、ページ・ロード間で要素を永続化します。その要素のid属性はユニークでないといけません。モーフィングによるページ更新から要素を除外したい場合も、data-turbo-permanentを使います。data-turbo-temporaryは、ドキュメントがキャッシュされる前に要素を削除します。これにより、data-turbo-temporaryがある要素をキャッシュから復元しません。data-turbo-eval="false"を指定することで、ページを再表示したときにインラインのscript要素を再評価しません。data-turbo-methodで、リンクのリクエストタイプをデフォルトのGETから変更できます。理想的には、GETではないリクエストはフォームで発行されるべきですが、data-turbo-methodはフォームが使えない場合に便利かもしれません。data-turbo-streamにより、リンクまたはフォームが Turbo ストリームのレスポンスを受け付けられます。Turbo は、GETでないメソッドによるフォーム送信に対して、自動的にストリーム・レスポンスを要求します。data-turbo-streamにより、GETリクエストに対しても Turbo ストリームを使えます。data-turbo-confirmは、指定された値で確認ダイアログを表示します。data-turbo-methodを持つform要素またはリンクで使えます。data-turbo-submits-withにより、フォーム送信時に表示するテキストを指定できます。この属性は、input要素またはbutton要素で使えます。フォーム送信中に、要素のテキストはdata-turbo-submits-withの値を表示します。送信後は、元のテキストに戻ります。操作の進行中に「保存中…」のようなメッセージを表示すれば、ユーザーにフィードバックを与えられるので便利です。download属性が付いたリンクは、ファイルをダウンロードするためのものでページ遷移ではないため、Turbo の処理から除外されます。
原文
The following data attributes can be applied to elements to customize Turbo’s behaviour.
data-turbo="false" disables Turbo Drive on links and forms including descendants. To reenable when an ancestor has opted out, use data-turbo="true". Be careful: when Turbo Drive is disabled, browsers treat link clicks as normal, but native adapters may exit the app. Note that if Turbo ignores a path, then setting data-turbo="true" will not force/override it to enable.
data-turbo-track="reload"tracks the element’s HTML and performs a full page reload when it changes. Typically used to keepscriptand CSSlinkelements up-to-date.data-turbo-track="dynamic"tracks the element’s HTML and removes the element when it is absent from an HTML response. Typically used to removestyleandlinkelements during navigation.data-turbo-frameidentifies the Turbo Frame to navigate. Refer to the Frames documentation for further details.data-turbo-preloadsignals to Drive to pre-fetch the next page’s contentdata-turbo-prefetch="false"disables prefetching links when the element is hovered.data-turbo-actioncustomizes the Visit action. Valid values arereplaceoradvance. Can also be used with Turbo Frames to promote frame navigations to page visits.data-turbo-permanentpersists the element between page loads. The element must have a uniqueidattribute. It also serves to exclude elements from being morphed when using page refreshes with morphingdata-turbo-temporaryremoves the element before the document is cached, preventing it from reappearing when restored.data-turbo-eval="false"prevents inlinescriptelements from being re-evaluated on Visits.data-turbo-methodchanges the link request type from the defaultGET. Ideally, non-GETrequests should be triggered with forms, butdata-turbo-methodmight be useful where a form is not possible.data-turbo-streamspecifies that a link or form can accept a Turbo Streams response. Turbo automatically requests stream responses for form submissions with non-GETmethods;data-turbo-streamallows Turbo Streams to be used withGETrequests as well.data-turbo-confirmpresents a confirm dialog with the given value. Can be used onformelements or links withdata-turbo-method.data-turbo-submits-withspecifies text to display when submitting a form. Can be used oninputorbuttonelements. While the form is submitting the text of the element will show the value ofdata-turbo-submits-with. After the submission, the original text will be restored. Useful for giving user feedback by showing a message like “Saving…” while an operation is in progress.downloadopts out of Turbo since the link is for downloading files and not navigation.
# 自動的に追加される属性
次の属性は Turbo が自動的に追加します。ある瞬間の Visit の状態を判断するのに便利です。
disabledは、フォーム送信中、フォームの送信ボタンに追加されます。これは、フォームの再送信を防ぐためです。data-turbo-previewは、Visit 中にプレビューを表示しているときに、html要素に追加されます。data-turbo-visit-directionは、Visit 中にhtml要素に追加されます。この属性の値には、forward、back、noneのいずれかが入ります。これらは、Visitの方向を表しています。aria-busyは、ページ遷移中、html要素とturbo-frame要素に追加されます。aria-busyは、処理中であることを示すために次の要素に追加されます。- ページ遷移中には
html要素 - フォーム送信中には
form要素 - フレーム内で遷移またはフォーム送信が行われているときには
turbo-frame要素
- ページ遷移中には
busyは、フレーム内でページ遷移やフォーム送信が行われている間、turbo-frame要素に付与されます。
原文
The following attributes are automatically added by Turbo and are useful to determine the Visit state at a given moment.
disabledis added to the form submitter while the form request is in progress, to prevent repeat submissions.data-turbo-previewis added to thehtmlelement when displaying a preview during a Visit.data-turbo-visit-directionis added to thehtmlelement during a visit, with a value offorwardorbackornone, to indicate its direction.aria-busyis added to:- the
htmlelement when a visit is in progress - the
formelement when a submission is in progress. - the
turbo-frameelement when a visit or form submission is in progress within the frame.
- the
busyis added to theturbo-frameelement when a navigation or form submission is in progress within the frame.
# メタタグ
次のmeta要素をhead要素に追加することで、キャッシュや Visit の振る舞いをカスタマイズできます。
<meta name="turbo-cache-control">で、キャッシュのオプトアウトができます。<meta name="turbo-visit-control" content="reload">で、Turbo でページ遷移するたびに、全ページリロードします。これは、<turbo-frame>からリクエストした時も同様です。<meta name="turbo-root">で、Turbo ドライブを適用するルートロケーションを設定できます。<meta name="view-transition" content="same-origin">により、View Transition APIをサポートしているブラウザでビュートランジションが動きます。<meta name="turbo-refresh-method" content="morph">で、モーフィングによるページ更新が使えます。<meta name="turbo-refresh-scroll" content="preserve">で、ページ更新時にスクロールの位置を保存します。<meta name="turbo-prefetch" content="false">を指定すると、マウスホバーしたときのリンクのプリフェッチを無効にします。
原文
The following meta elements, added to the head, can be used to customize caching and Visit behavior.
<meta name="turbo-cache-control">to opt out of caching.<meta name="turbo-visit-control" content="reload">will perform a full page reload whenever Turbo navigates to the page, including when the request originates from a<turbo-frame>.<meta name="turbo-root">to scope Turbo Drive to a particular root location.<meta name="view-transition" content="same-origin">to trigger view transitions on browsers that support the View Transition API.<meta name="turbo-refresh-method" content="morph">will configure page refreshes with morphing.<meta name="turbo-refresh-scroll" content="preserve">will enable scroll preservation during page refreshes.<meta name="turbo-prefetch" content="false">will disable prefetching links on hover.