Share
Embed
モバイルページを爆速に ~Laravelで実現するAMP対応の自動化~ #PHPcon2017
PHPcon2017で発表を行ったスライドです。
#PHPcon2017
#PHPカンファレンス2017
yaizuuuu
October 08, 2017
Transcript
54 slides
-
自己紹介 名前: 小柳津 裕真(オヤイヅ ユウマ) 所属: 株式会社イノベーション 好きな技術: PHP, Laravel,
AMP 普段はITトレンドというサービスを開発しています AWS re:Invent 2017参加予定 -
アジェンダ 1. AMPってなに?? a. AMPとは何か b. なぜ早いのか c. どんな対応が必要なのか 2.
自動化の方法 a. LaravelのMiddlewareを活用 b. 画像サイズを動的に取得 -
アジェンダ 1. AMPってなに?? a. AMPとは何か b. なぜ早いのか c. どんな対応が必要なのか 2.
自動化の方法 a. LaravelのMiddlewareを活用 b. 画像サイズを動的に取得 -
なぜ早いのか 1. 非同期スクリプトのみを許可 2. すべてのリソースサイズを静的に決定 3. サードパーティJavaScriptをクリティカルパスから外す 4. CSSはすべてインラインスタイルにしてサイズを固定 5.
ページが開かれる前に読み込む https://www.ampproject.org/ja/learn/about-how/ <script async src="~"></script> height="400" width="800" ex. -
<!doctype html> <html amp> <head> <meta charset="utf-8"> <link rel="canonical" href="hello-world.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"> <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style> </noscript> <script async src="https://cdn.ampproject.org/v0.js"></script> </head> <body>Hello World!</body> </html> -
どんな対応が必要なのか 一部のタグをAMP HTMLタグに置き換える https://www.ampproject.org/ja/docs/tutorials/create/include_image ほとんどの HTML タグは、そのまま AMP HTML でも使用できますが、
中には <img> タグのように同等か少し高機能のカスタム AMP HTML タグに置き換えなくてはいけないものがあります -
どんな対応が必要なのか 一部のタグをAMP HTMLタグに置き換える 特定のタグはそのまま活用できない 下のようなimgタグ をAMP HTMLでは、height・width属性を必ず指定する必要がある ※video, audio,
iframeタグもそのままでは使用不可 <img src="welcome.jpg"> <amp-img src="welcome.jpg" height="400" width="800"></amp-img> -
どんな対応が必要なのか CSSを一つのインライン要素にする https://www.ampproject.org/ja/docs/tutorials/create/presentation_layout <style amp-custom> body { background-color: white;} amp-img
{ background-color: gray; border: 1px solid black;} </style> 各 AMP ページで使える埋め込みスタイルシートは 1 つだけで、使用で きないセレクターもいくつかあります。 -
どんな対応が必要なのか CSSを一つのインライン要素にする CSSにもいくつかの制限 1. l 2. ! 3. 4. CSSは50KBまで
<link rel="stylesheet" href="~"> .mt10 {margin-top: 10px;!important} <div style="~"></div> -
どんな対応が必要なのか CSSを一つのインライン要素にする CSSにもいくつかの制限 1. l 2. ! 3. 4. CSSは50KBまで
<link rel="stylesheet" href="~"> .mt10 {margin-top: 10px;!important} <div style="~"></div> -
アジェンダ 1. AMPってなに?? a. AMPとは何か b. なぜ早いのか c. どんな対応が必要なのか 2.
自動化の方法 a. LaravelのMiddlewareを活用 b. 画像サイズを動的に取得 -
LaravelのMiddlewareを活用 <?php namespace App\Http\Middleware; use Closure; class Middleware { public
function handle($request, Closure $next) { $response = $next($request); return $response; } } -
LaravelのMiddlewareを活用 <?php namespace App\Http\Middleware; use Closure; class Middleware { public
function handle($request, Closure $next) { $response = $next($request); return $response; } } // この記述でレスポンスの内容が取得できる -
LaravelのMiddlewareを活用 public function handle($request, Closure $next) { $response = $next($request);
$content = $response->getContent(); return $response; } // この記述でレスポンスの // コンテンツ(HTML)を取得できる -
LaravelのMiddlewareを活用 public function handle($request, Closure $next) { $response = $next($request);
$html = '<!DOCTYPE> <html> <head> <title>PHPcon2017</title> </head> <body><h1>PHPcon2017</h1></body> </html>'; $response->setContent($html); return $response; } // この記述でレスポンスの内容を変更できる -
LaravelのMiddlewareを活用 $response = $next($request); $content = $response->getContent(); $pattern = '/<img
(\s|\w|[\^!"#$%&@\'()*+,.\/~;?:=\\-]|[^\x01-\x7E])+>/'; $replacement = '$0</amp-img>'; $content = preg_replace($pattern, $replacement, $content); $pattern = '/<img/'; $replacement = '<amp-img'; $content = preg_replace($pattern, $replacement, $content); $response->setContent($content); return $response; -
LaravelのMiddlewareを活用 $response = $next($request); $content = $response->getContent(); $pattern = '/<img
(\s|\w|[\^!"#$%&@\'()*+,.\/~;?:=\\-]|[^\x01-\x7E])+>/'; $replacement = '$0</amp-img>'; $content = preg_replace($pattern, $replacement, $content); $pattern = '/<img/'; $replacement = '<amp-img'; $content = preg_replace($pattern, $replacement, $content); $response->setContent($content); return $response; //レスポンス内容のHTMLを取得 -
LaravelのMiddlewareを活用 $response = $next($request); $content = $response->getContent(); $pattern = '/<img
(\s|\w|[\^!"#$%&@\'()*+,.\/~;?:=\\-]|[^\x01-\x7E])+>/'; $replacement = '$0</amp-img>'; $content = preg_replace($pattern, $replacement, $content); $pattern = '/<img/'; $replacement = '<amp-img'; $content = preg_replace($pattern, $replacement, $content); $response->setContent($content); return $response; <img src=”~”> => <img src”~”></amp-img> -
LaravelのMiddlewareを活用 $response = $next($request); $content = $response->getContent(); $pattern = '/<img
(\s|\w|[\^!"#$%&@\'()*+,.\/~;?:=\\-]|[^\x01-\x7E])+>/'; $replacement = '$0</amp-img>'; $content = preg_replace($pattern, $replacement, $content); $pattern = '/<img/'; $replacement = '<amp-img'; $content = preg_replace($pattern, $replacement, $content); $response->setContent($content); return $response; <img src”~”></amp-img> => <amp-img src”~”></amp-img> -
LaravelのMiddlewareを活用 $response = $next($request); $content = $response->getContent(); $pattern = '/<img
(\s|\w|[\^!"#$%&@\'()*+,.\/~;?:=\\-]|[^\x01-\x7E])+>/'; $replacement = '$0</amp-img>'; $content = preg_replace($pattern, $replacement, $content); $pattern = '/<img/'; $replacement = '<amp-img'; $content = preg_replace($pattern, $replacement, $content); $response->setContent($content); return $response; //変更したHTMLをレスポンスの内容としてセット -
LaravelのMiddlewareを活用 //CSSのAMP対応 //AMPではCSSの!importantが使用できないので削除する $pattern = '/!important/'; $replacement = ''; $content
= preg_replace($pattern, $replacement, $content); //AMPではタグ内のstyle属性が使用できないので削除する $pattern = '/style="*(\s|\w|[\^!#$%&@\'()*+,.\/~;?:=\\-])*"/'; $replacement = ''; $content = preg_replace($pattern, $replacement, $content); -
LaravelのMiddlewareを活用 $dom = new \DOMDocument(); @$dom->loadHTML($response->getContent()); $img_tag_list = $dom->getElementsByTagName("img"); foreach
($img_tag_list as $img_tag) { } //取得したレスポンスのHTMLをDOMにパース -
LaravelのMiddlewareを活用 $dom = new \DOMDocument(); @$dom->loadHTML($response->getContent()); $img_tag_list = $dom->getElementsByTagName("img"); foreach
($img_tag_list as $img_tag) { } //imgタグを全て取得 -
LaravelのMiddlewareを活用 $dom = new \DOMDocument(); @$dom->loadHTML($response->getContent()); $img_tag_list = $dom->getElementsByTagName("img"); foreach
($img_tag_list as $img_tag) { } //imgタグにheight・width属性をセットする処理を記述する -
LaravelのMiddlewareを活用 if ($img_tag->getAttribute('height') == "" && $img_tag->getAttribute('width') == "") {
$filepath = $img_tag->getAttribute('src'); if (strpos($filepath, '//') === 0) { //CDNの場合 $filepath = 'https:' . $filepath; } elseif (strpos($filepath, '/') === 0) { //ローカルの場合 $filepath = ltrim($filepath, '/'); } @list($width, $height) = getimagesize($filepath); if ($width != "" && $height != "") { $img_tag->setAttribute('width', $width); $img_tag->setAttribute('height', $height); $img_tag->setAttribute('layout', 'responsive'); } } //高さ・幅が指定されていない場合は、 //画像サイズを取得して設定する ※foreach文の処理 -
LaravelのMiddlewareを活用 if ($img_tag->getAttribute('height') == "" && $img_tag->getAttribute('width') == "") {
$filepath = $img_tag->getAttribute('src'); if (strpos($filepath, '//') === 0) { //CDNの場合 $filepath = 'https:' . $filepath; } elseif (strpos($filepath, '/') === 0) { //ローカルの場合 $filepath = ltrim($filepath, '/'); } @list($width, $height) = getimagesize($filepath); if ($width != "" && $height != "") { $img_tag->setAttribute('width', $width); $img_tag->setAttribute('height', $height); $img_tag->setAttribute('layout', 'responsive'); } } //画像のパスを取得 ※foreach文の処理 -
LaravelのMiddlewareを活用 if ($img_tag->getAttribute('height') == "" && $img_tag->getAttribute('width') == "") {
$filepath = $img_tag->getAttribute('src'); if (strpos($filepath, '//') === 0) { //CDNの場合 $filepath = 'https:' . $filepath; } elseif (strpos($filepath, '/') === 0) { //ローカルの場合 $filepath = ltrim($filepath, '/'); } @list($width, $height) = getimagesize($filepath); if ($width != "" && $height != "") { $img_tag->setAttribute('width', $width); $img_tag->setAttribute('height', $height); $img_tag->setAttribute('layout', 'responsive'); } } //画像のサイズをセットする ※foreach文の処理