From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/ja/web/html/preloading_content/index.html | 243 ++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 files/ja/web/html/preloading_content/index.html (limited to 'files/ja/web/html/preloading_content') diff --git a/files/ja/web/html/preloading_content/index.html b/files/ja/web/html/preloading_content/index.html new file mode 100644 index 0000000000..71b388638b --- /dev/null +++ b/files/ja/web/html/preloading_content/index.html @@ -0,0 +1,243 @@ +--- +title: rel="preload" によるコンテンツの先読み +slug: Web/HTML/Preloading_content +tags: + - Guide + - HTML + - JavaScript + - Link + - Media + - Performance + - Web Performance + - as + - preload + - rel +translation_of: Web/HTML/Preloading_content +--- +

{{htmlelement("link")}} 要素の {{htmlattrxref("rel", "link")}} 属性で preload を指定すると、 HTML の {{htmlelement("head")}} 要素内で読み込みリクエストを宣言し、ページのライフサイクルの早期の、ブラウザーの主なレンダリング機構が起動する前に読み込みを始めたい、すぐに必要なリソースを指定することができます。これにより、そのリソースがより早く利用でき、ページのレンダリングがブロックされにくくなり、性能が向上します。

+ +

この記事では <link rel="preload"> がどのように動作するのかについての基本的なガイドを提供します。

+ +

基本概念

+ +

多くの場合は以下のように、 <link> を使用して CSS ファイルを読み込み、ページにスタイルを適用します。

+ +
<link rel="stylesheet" href="styles/main.css">
+ +

しかしここで、 rel の値に preload を使用すると、 <link> 要素は利用したいあらゆるリソースの先読み指示になります。以下のものも指定する必要があります。

+ + + +

簡単な例は以下のようになります (JS と CSS のサンプルコード および デモ) も参照してください)。

+ +
<head>
+  <meta charset="utf-8">
+  <title>JS and CSS preload example</title>
+
+  <link rel="preload" href="style.css" as="style">
+  <link rel="preload" href="main.js" as="script">
+
+  <link rel="stylesheet" href="style.css">
+</head>
+
+<body>
+  <h1>bouncing balls</h1>
+  <canvas></canvas>
+
+  <script src="main.js" defer></script>
+</body>
+ +

ここで CSS ファイルと JavaScript ファイルを先読みするので、その後のページのレンダリングで必要な時には、すぐに利用できるようになります。ブラウザーはおそらく、 <link rel="stylesheet"><script> 要素を HTML 内の同じチャンクで見つけるので、この例は極端ですが、後に現れるリソースであるほど、また大きいリソースであるほど効果が見られる可能性があります。例えば以下の場合です。

+ + + +

preload には他の利点もあります。 as によって先読みされるコンテンツの種類をブラウザーに指示することで、以下のようなことが実現できます。

+ + + +

先読みできるコンテンツの種類

+ +

さまざまな種類のコンテンツが先読みできます。 as 属性で使用できる値は以下の通りです。

+ + + +
+

メモ: 使用されると予想されるこれらの値やウェブ機能について、もっと詳細は Preload の仕様書にあります。 — link element extensions を参照してください。また、フェッチの仕様書で管理されている as 属性の値の完全な一覧は、 request destinations を参照してください。

+
+ +

MIME タイプを含める

+ +

<link> 要素は {{htmlattrxref("type", "link")}} 要素を受け付け、要素が指す先のリソースの MIME タイプを指定することができます。これは特にリソースの先読み時に便利です。 — ブラウザーは type 属性の値を使用して対応しているリソースであるかどうかを確認し、その場合だけダウンロードを開始し、そうでない場合は開始しないようにすることができます。

+ +

この例を動画のデモで見ることができます (ソースコード全体デモ版もご覧ください。)。

+ +
<head>
+  <meta charset="utf-8">
+  <title>Video preload example</title>
+
+  <link rel="preload" href="sintel-short.mp4" as="video" type="video/mp4">
+</head>
+<body>
+  <video controls>
+    <source src="sintel-short.mp4" type="video/mp4">
+    <source src="sintel-short.webm" type="video/webm">
+    <p>Your browser doesn't support HTML5 video. Here is a <a href="sintel-short.mp4">link to the video</a> instead.</p>
+  </video>
+</body>
+ +

この場合、MP4 をサポートしているブラウザーは MP4 を先読みして使用し、ユーザーにとって動画プレイヤーをよりスムーズまたはレスポンシブにできるでしょう。ブラウザーが MP4 をサポートしていない場合は WebM バージョンを読み込みますが、先読みの利点は得られません。これは、どのように先読みコンテンツがプログレッシブエンハンスメントの哲学と結ばれているかを示しています。

+ +

オリジンをまたいだフェッチ

+ +

If you've got your sites' CORS settings worked out properly, you can successfully preload cross-origin resources as long as you set a {{htmlattrxref("crossorigin","link")}} attribute on your <link> element.

+ +

One interesting case where this applies, even if the fetch is not cross-origin, is font files. Because of various reasons, these have to be fetched using anonymous mode CORS (see Font fetching requirements).

+ +

Let's use this case as an example. You can see the full example source code on GitHub (also see it live):

+ +
<head>
+  <meta charset="utf-8">
+  <title>Web font example</title>
+
+  <link rel="preload" href="fonts/cicle_fina-webfont.woff2" as="font" type="font/woff2" crossorigin>
+  <link rel="preload" href="fonts/zantroke-webfont.woff2" as="font" type="font/woff2" crossorigin>
+
+  <link href="style.css" rel="stylesheet" type="text/css">
+</head>
+<body>
+  …
+</body>
+ +

Not only are we providing the MIME type hints in the type attributes, but we are also providing the crossorigin attribute to handle the CORS issue.

+ +

media を含める

+ +

One nice feature of <link> elements is their ability to accept {{htmlattrxref("media", "link")}} attributes. These can accept media types or full-blown media queries, allowing you to do responsive preloading!

+ +

Let's look at an example (see it on GitHub — source code, live example):

+ +
<head>
+  <meta charset="utf-8">
+  <title>Responsive preload example</title>
+
+  <link rel="preload" href="bg-image-narrow.png" as="image" media="(max-width: 600px)">
+  <link rel="preload" href="bg-image-wide.png" as="image" media="(min-width: 601px)">
+
+  <link rel="stylesheet" href="main.css">
+</head>
+<body>
+  <header>
+    <h1>My site</h1>
+  </header>
+
+  <script>
+    var mediaQueryList = window.matchMedia("(max-width: 600px)");
+    var header = document.querySelector('header');
+
+    if (mediaQueryList.matches) {
+      header.style.backgroundImage = 'url(bg-image-narrow.png)';
+    } else {
+      header.style.backgroundImage = 'url(bg-image-wide.png)';
+    }
+  </script>
+</body>
+ +

We include media attributes on our <link> elements so that a narrow image is preloaded if the user has a narrow viewport, and a wider image is loaded if they have a wide viewport. We use {{domxref("Window.matchMedia")}} / {{domxref("MediaQueryList")}} to do this (see Testing media queries for more).

+ +

This makes it much more likely that the font will be available for the page render, cutting down on FOUT (flash of unstyled text).

+ +

This doesn't have to be limited to images, or even files of the same type — think big! You could perhaps preload and display a simple SVG diagram if the user is on a narrow screen where bandwidth and CPU is potentially more limited, or preload a complex chunk of JavaScript then use it to render an interactive 3D model if the user's resources are more plentiful.

+ +

スクリプトと先読み

+ +

これらのプリロードに関するもう一つの良い点として、スクリプトを使って実行できることが挙げられます。例えば、ここでは {{domxref("HTMLLinkElement")}} インスタンスを作成し、それを DOM に適用させています。

+ +
var preloadLink = document.createElement("link");
+preloadLink.href = "myscript.js";
+preloadLink.rel = "preload";
+preloadLink.as = "script";
+document.head.appendChild(preloadLink);
+
+ +

これは、ブラウザは myscript.js ファイルをプリロードしますが、実際はまだ使用されていません。これを使用するには、以下のようにします。

+ +
var preloadedScript = document.createElement("script");
+preloadedScript.src = "myscript.js";
+document.body.appendChild(preloadedScript);
+
+ +

これは、スクリプトをプリロードしたいが、必要なときまで実行を延期する場合に便利です。

+ +

他のリソースの先読み機構

+ +

Other preloading features exist, but none are quite as fit for purpose as <link rel="preload">:

+ + + +

仕様書

+ + + + + + + + + + + + + + + + + + + + + +
仕様書状態備考
{{SpecName('Preload','#x2.link-type-preload','preload')}}{{Spec2('Preload')}}preload の詳細
{{SpecName('HTML WHATWG', '#link-type-preload', 'rel=preload')}}{{Spec2('HTML WHATWG')}}preload の定義
+ +

ブラウザーの対応

+ + + +

{{Compat("html.elements.link.rel.preload")}}

+ +

関連情報

+ + + +

{{QuickLinksWithSubpages("/ja/docs/Web/HTML")}}

-- cgit v1.2.3-54-g00ecf