Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

as — the intended use of a preloaded resource

`as` on a preload link identifies how a resource will be used, not its filename or MIME type.

Version: HTML Living Standard

Syntax

<link rel="preload" href="/feature.js" as="script">

For preload the attribute is required; choose the correct destination, such as script, style, image, font, or fetch. It affects fetching and applicable policies. For modulepreload the default destination is script; do not copy arbitrary values from another category. Do not add it to other link types.

Preloading executes no script and applies no stylesheet. The resource needs a later consumer. Mismatched URLs, access modes, or credentials can prevent reuse and cause another fetch. Fonts and CORS requests need matching configuration; MIME type belongs to a separate attribute.

The example fetches a local file as script, then creates the element that executes it. The execution counter stays zero after preloading and becomes one after execution. The test does not promise identical cache behavior in every browser.

Examples

<!doctype html><meta charset="utf-8"><output id="result"></output>
<script>
// Local /feature.js contains: window.executions = (window.executions || 0) + 1;
window.executions = 0;
const preload = document.createElement('link');
preload.rel = 'preload'; preload.as = 'script'; preload.href = '/feature.js';
preload.addEventListener('load', () => {
  const before = window.executions;
  const script = document.createElement('script'); script.src = preload.href;
  script.addEventListener('load', () => {
    document.querySelector('#result').textContent = 'before=' + before + '; after=' + window.executions;
  });
  document.head.append(script);
});
document.head.append(preload);
</script>

Result

before=0; after=1

The fixture file increments a counter. Fetching and one execution were verified, not a guaranteed single transfer.

Sources