ActionView::Helpers::AssetTagHelper

preload_link_tag

API docs (Rails 8.1.4.1) ↗
def preload_link_tag(source, options = {})

Description

Creates a <link> with rel="preload" for loading a file ahead of time.

Arguments

Changing a value updates the ERB and HTML.

Used as the href attribute. Depending on the value:
  • A URL (a string starting with https:// or another …://, //, data: or cid:): used as is.
  • A string starting with / ("/font.woff2"): used as is.
  • Any other string ("application.css"): looks up the asset (a file in app/assets) and uses its path, with a digest added to the file name. If the asset is not found, raises Propshaft::MissingAssetError. The lookup and digest come from Propshaft; in an app without an asset pipeline, it just prepends / (or, if the kind is known, a folder such as /images/) to the path.
The as and type attributes are determined from the extension. When as is font (for fonts such as .woff2), also adds crossorigin="anonymous".
options= {}
"fetch"
Sets the as attribute instead of determining it from the extension.
"module"
Sets the type attribute instead of determining it from the extension. module also changes rel to modulepreload.
true adds crossorigin="anonymous". A string sets the crossorigin attribute.
"high"
Sets the fetchpriority attribute.
true skips the asset lookup and prepends / to a string that is neither a URL nor starts with /.
Common options

Rails handles these options the same way in every helper. Options the HTML spec does not allow on the <link>, and ones this site considers rarely used, are not listed here.

Shared by all helpers

{active: true, hidden: false}
A hash gives the keys whose values are not false or nil, joined with spaces. An array gives its elements joined with spaces, after removing nil, false and empty strings (including whitespace-only strings).
{controller: "tags", tags_suggestions_value: ["ruby", "rails"]}
A hash adds data-* attributes, with _ in the keys converted to -. Array and hash values are converted to JSON strings.
{describedby: ["name_help", "name_error"], invalid: false}
Like :data, adds aria-* attributes. Array values are joined with spaces, leaving out nil and empty strings (nothing is added if none remain). For a hash value, the keys whose values are not false or nil are joined with spaces.
Any other options become HTML attributes of the <link>; those with a nil value are left out.

ERB

Changes from the previous output: Added Removed Changed


          

HTML