facile-it/laminas-link-headers-module
Composer 安装命令:
composer require facile-it/laminas-link-headers-module
包简介
Laminas module to push Link headers for resourcers added with HeadLink view helper
README 文档
README
This module will automatically sends Link HTTP headers supporting
resource hints
and preload, via HTTP headers.
Currently supported hints
From OptionsInterface:
public const MODE_PRELOAD = 'preload'; public const MODE_PREFETCH = 'prefetch'; public const MODE_DNS_PREFETCH = 'dns-prefetch'; public const MODE_PRECONNECT = 'preconnect'; public const MODE_PRERENDER = 'prerender';
Configuration
This is the default configuration and options can be overridden in your configuration.
return [ 'facile' => [ 'laminas_link_headers_module' => [ 'stylesheet_enabled' => false, // send link headers for stylesheet links 'stylesheet_mode' => 'preload', // resource hint for stylesheet links 'script_enabled' => false, // send link headers for scripts 'script_mode' => 'preload', // resource hint for script links 'http2_push_enabled' => true, // if disabled, a "nopush" attributed will be added to disable HTTP/2 push ], ], ];
Example
Configuration:
Default configuration:
return [ 'facile' => [ 'laminas_link_headers_module' => [ 'stylesheet_enabled' => false, // send link headers for stylesheet links 'stylesheet_mode' => 'preload', // resource hint for stylesheet links 'script_enabled' => false, // send link headers for scripts 'script_mode' => 'preload', // resource hint for script links 'http2_push_enabled' => true, // if disabled, a "nopush" attributed will be added to disable HTTP/2 push ], ], ];
Template
In your template:
<!DOCTYPE html> <html lang="it"> <head> <?= $this->headLink(['rel' => 'preload', 'as' => 'image', 'href' => $this->basePath() . $this->asset('assets/images/logo.png'), 'media' => 'image/png') ->headLink(['rel' => 'preload', 'as' => 'style', 'href' => $this->basePath() . $this->asset('assets/vendor.css')]) ->headLink(['rel' => 'preload', 'as' => 'script', 'href' => $this->basePath() . $this->asset('assets/vendor.js')]) // prefetch (low priority) resources required in th next pages ->headLink(['rel' => 'prefetch', 'as' => 'style', 'href' => $this->basePath() . $this->asset('assets/next.css')]) // do not send preload headers ->prependStylesheet($this->basePath() . $this->asset('assets/vendor.css')) ->prependStylesheet($this->basePath() . $this->asset('assets/vendor.js')) ?> <?= $this->headScript() ->prependFile('/script/foo.js') ?> </head> <body> <!-- your content here --> <script type="text/javascript" src="<?= $this->basePath() . $this->asset('assets/vendor.js') ?>"></script> </body> </html>
Response headers
The module will automatically add a Link header to the response:
Link: </assets/images/logo.png>; rel="preload"; as="image"; media="image/png",
</assets/vendor.css>; rel="preload"; as="style",
</assets/vendor.js>; rel="preload"; as="script",
</assets/next.css>; rel="prefetch"; as="style"
You should notice that resource /script/foo.js is not in headers, because it wasn't
included in preload head links.
Automatically sends stylesheets preload
Enabling stylesheet_enabled mode in your configuration, you can avoid inserting preload links
for all your styles.
Configuration:
return [ 'facile' => [ 'laminas_link_headers_module' => [ 'stylesheet_enabled' => true, // send link headers for stylesheet links ], ], ];
You can optionally change the stylesheet_mode
(supported modes are vailable as constants in OptionsInterface)
to use on stylesheets.
Template
In your template:
<!DOCTYPE html> <html lang="it"> <head> <?= $this->prependStylesheet($this->basePath() . $this->asset('assets/vendor.css')) ?> </head> <body> <!-- your content here --> </body> </html>
Response headers
The module will automatically add a Link header to the response:
Link: </assets/vendor.css>; rel="preload"; as="style"; type="text/css"; media="screen"
Automatically sends scripts preload
Enabling script_enabled mode in your configuration, you can avoid inserting preload links
for all your scripts.
Configuration:
return [ 'facile' => [ 'laminas_link_headers_module' => [ 'script_enabled' => true, // send link headers for script links ], ], ];
You can optionally change the script_mode
(supported modes are vailable as constants in OptionsInterface)
to use on stylesheets.
Template
In your template:
<!DOCTYPE html> <html lang="it"> <head> <?= $this->headScript() ->prependFile('/script/foo.js') ->prependFile('/script/bar.js', 'text/foo') ?> </head> <body> <!-- your content here --> </body> </html>
Response headers
The module will automatically add a Link header to the response:
Link: </script/foo.js>; rel="preload"; as="script"; type="text/javascript",
</script/bar.js>; rel="preload"; as="script"; type="text/foo"
Disable HTTP/2 push
Using HTTP/2, sending preload link headers the web server will push contents to the browser when the page is requested.
This isn't always necessary, because browsers can cache the contents and pushing them can increase bandwidth usage with no significant performance.
You can disable push setting http2_push_enabled configuration option to false.
This will add a nopush attribute
that indicates to an HTTP/2 push capable server that the resource hould not be pushed
(e.g. the origin may have additional information indicating that it may already be in cache).
facile-it/laminas-link-headers-module 适用场景与选型建议
facile-it/laminas-link-headers-module 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 48.15k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「performance」 「assets」 「zend」 「Zend Framework」 「link」 「header」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 facile-it/laminas-link-headers-module 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 facile-it/laminas-link-headers-module 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 facile-it/laminas-link-headers-module 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Asset Management for PHP
g4 application profiler package
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
WordPress mu-plugin to remove jQuery Migrate from the list of jQuery dependencies and to allow jQuery to enqueue before </body> instead of in the <head>.
Caching and compression for Twig assets (JavaScript and CSS).
Create link to static resources with cache-breaking segment based on md5 of the file
统计信息
- 总下载量: 48.15k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-23