newism/craft3-asset-rev

使用时间戳重写资产URL

安装数量: 7,689

依赖项: 0

建议者: 0

安全: 0

星标: 5

关注者: 2

分支: 0

开放问题: 2

类型:craft插件

0.0.7 2020-10-12 12:17 UTC

This package is auto-updated.

Last update: 2024-09-12 20:09:19 UTC


README

资产模型

通过日期修改时间戳重写Asset模型的URL。很简单……只需调用函数并传递资产和一个可选的转换。

之前

Template: {{ asset.url(transform) }}
Output: https://local.craft3/uploads/biggie.jpg

之后

Template: {{ nsm_rev_asset_url(asset, transform) }}
Output: https://local.craft3/uploads/biggie.1496670969.jpg

清单文件

此外,NSM Asset Rev还可以检查清单文件中的URL并返回相应的重写URL。

清单

{ "app.css": "app.1e9915c1398b2ba2fcc2.css" }

之前

Template: {{ url(app.css) }}
Output: https://local.craft3/app.css

之后

Template: {{ nsm_rev_manifest_url('app.css') }}
Output: https://local.craft3/app.1e9915c1398b2ba2fcc2.css

清单文件很可能是从构建过程中创建的。

以下是一些示例

注意:NSM Asset Rev只支持一级键:值对。

安装

步骤1:下载包

打开命令行,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本

$ composer require newism/craft3-asset-rev

此命令需要您全局安装了Composer,如Composer文档中的安装章节所述。

步骤2:安装

在Craft控制面板的设置>插件下安装插件。

使用方法

Twig模板函数

此插件提供了两个twig函数。

资产模型

{{ nsm_rev_asset_url(asset, transform) }}

清单文件

{{ nsm_rev_manifest_url(url) }}

助手

实际上有三个twig函数 :). 第三个是一个助手,根据参数调用nsm_rev_asset_urlnsm_rev_manifest_url

如果第一个参数是Assetl,则内部调用nsm_rev_asset_url

Template: {{ nsm_rev_url(asset, transform) }}
Output: https://local.craft3/uploads/biggie.1496670969.jpg

如果第一个参数是string模型,则内部调用nsm_rev_manifest_url

Template: {{ nsm_rev_url('app.css') }}
Output: https://local.craft3/app.1e9915c1398b2ba2fcc2.css

服务器配置

重要:此插件实际上不会更改服务器上的文件名。您需要在Apache conf或Nginx访问中实现重写规则。

Apache

见:https://github.com/h5bp/server-configs-apache/blob/master/dist/.htaccess#L968-L984

# ----------------------------------------------------------------------
# | Filename-based cache busting                                       |
# ----------------------------------------------------------------------

# If you're not using a build process to manage your filename version
# revving, you might want to consider enabling the following directives
# to route all requests such as `/style.12345.css` to `/style.css`.
#
# To understand why this is important and even a better solution than
# using something like `*.css?v231`, please see:
# http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/

# <IfModule mod_rewrite.c>
#     RewriteEngine On
#     RewriteCond %{REQUEST_FILENAME} !-f
#     RewriteRule ^(.+)\.(\d+)\.(bmp|css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest)$ $1.$3 [L]
# </IfModule>

NGINX

见:https://github.com/h5bp/server-configs-nginx/blob/master/h5bp/location/cache-busting.conf#L1-L10

# Built-in filename-based cache busting

# This will route all requests for /css/style.20120716.css to /css/style.css
# Read also this: github.com/h5bp/html5-boilerplate/wiki/cachebusting
# This is not included by default, because it'd be better if you use the build
# script to manage the file names.
location ~* (.+)\.(?:\d+)\.(bmp|css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest)$ {
  try_files $uri $1.$2;
}

插件配置

./src/config.php

路线图

一些要完成的事情和潜在功能的想法

1.0

  • 发布

未来

致谢

Newism提供