diff --git a/CHANGELOG.md b/CHANGELOG.md index 7860520..49e1086 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,44 @@ +## [0.2.4](https://github.com/serverless-components/tencent-website/compare/v0.2.3...v0.2.4) (2022-03-18) + + +### Bug Fixes + +* 修改文件过多上传失败 ([#46](https://github.com/serverless-components/tencent-website/issues/46)) ([6fa6d0b](https://github.com/serverless-components/tencent-website/commit/6fa6d0bc17242f8eaff6153696302bf46e7ad3a7)) + +## [0.2.2](https://github.com/serverless-components/tencent-website/compare/v0.2.1...v0.2.2) (2021-07-29) + + +### Bug Fixes + +* update deps ([f266c80](https://github.com/serverless-components/tencent-website/commit/f266c800ef675e138e5fb0882fb0d3bc60ba571f)) + +## [0.2.1](https://github.com/serverless-components/tencent-website/compare/v0.2.0...v0.2.1) (2021-04-26) + + +### Bug Fixes + +* support tag config ([a391ff6](https://github.com/serverless-components/tencent-website/commit/a391ff6a37c0f29e9945ccb81a4f4bfe68c4d965)) + +# [0.2.0](https://github.com/serverless-components/tencent-website/compare/v0.1.4...v0.2.0) (2021-03-31) + + +### Bug Fixes + +* change bucketName to bucket ([fa0674a](https://github.com/serverless-components/tencent-website/commit/fa0674a145674da42e6d93b565b489cb16dced2b)) +* update deps ([22d59d3](https://github.com/serverless-components/tencent-website/commit/22d59d3b8c9172b283c9001d67bf9ec85c12b9dc)) + + +### Features + +* suppport cache bucket name in state ([968b74b](https://github.com/serverless-components/tencent-website/commit/968b74b00896b2c998f5f5670a47062da88d38e6)) + +## [0.1.4](https://github.com/serverless-components/tencent-website/compare/v0.1.3...v0.1.4) (2021-03-26) + + +### Bug Fixes + +* support ignoreHtmlExt, close [#36](https://github.com/serverless-components/tencent-website/issues/36) ([c86009d](https://github.com/serverless-components/tencent-website/commit/c86009d849bd28acd865a907496372d47e086813)) + ## [0.1.3](https://github.com/serverless-components/tencent-website/compare/v0.1.2...v0.1.3) (2021-03-24) diff --git a/README.md b/README.md index 05398ad..3f6fb63 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ inputs: # hook: npm run build # websitePath: ./ region: ap-guangzhou - bucketName: my-bucket + bucket: my-bucket protocol: https ``` @@ -146,6 +146,10 @@ TENCENT_SECRET_ID=123 TENCENT_SECRET_KEY=123 ``` +## 使用文档 + +[使用文档](./docs/README.md) + ## License MIT License diff --git a/__tests__/index.test.js b/__tests__/index.test.js index 41771f0..fe7d133 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -22,7 +22,7 @@ const instanceYaml = { index: 'index.html', error: 'index.html' }, - bucketName: 'my-bucket', + bucket: 'website-integration-test', region: 'ap-guangzhou', } } diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..b78a4e1 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,7 @@ +# 文档 + +## 目录 + +- [全量配置](./configure.md) +- [高级配置](./advance.md) +- [常见问题](./faq.md) diff --git a/docs/advance.md b/docs/advance.md new file mode 100644 index 0000000..ef3670f --- /dev/null +++ b/docs/advance.md @@ -0,0 +1,63 @@ +## 高级配置 + +### env + +serverless 部署时会将 `env` 下配置的参数写入 `env.js` 文件中,将该文件打包上传到相对于 `src` 的 `envPath` 目录,默认为 `src` 指定目录。 + +比如配置了: + +```yaml +app: appDemo +stage: dev + +component: website +name: websiteDemo + +inputs: + src: + src: ./src + index: index.html + error: index.html + region: ap-guangzhou + bucket: website-demo + protocol: https + # env 配置 + env: + API_URL: https://api.com +``` + +那么静态项目根目录下生成的 `env.js` 文件内容如下: + +```js +window.env = {} +window.env.API_URL = 'https://api.com' +``` + +然后,我们可以在前端项目中给所有的请求 URL 添加 `window.env.API_URL` 前缀,通常在全栈应用中,会使用到。比如在部署完后端服务后会生产后端服务网关 `url`,然后我们将上面的的 `API_URL` 赋值为后端服务的 `url`,就可以做到无需手动引入修改接口链接了。具体使用请参考 [全栈应用案例](https://github.com/serverless-components/tencent-examples/tree/master/fullstack) + +### hook & dist + +serverless cli 支持配置 `hook` 配置来帮用户执行部署前的命令,比如前端项目需要通过 `npm run build` 命令构建,然后才会部署,那么可以配置如下: + +```yaml +app: appDemo +stage: dev + +component: website +name: websiteDemo + +inputs: + src: + dist: ./dist + hook: npm run build + index: index.html + error: index.html + region: ap-guangzhou + bucket: website-demo + protocol: https +``` + +- **hook**: 指定部署前需要执行的命令 +- **dist**: 指定执行 `hook` 命令后,需要部署到云端的代码目录 + +> 注意:`hook` 必须和 `dist` 一起用。 diff --git a/docs/configure.md b/docs/configure.md index 30368a5..2278b76 100644 --- a/docs/configure.md +++ b/docs/configure.md @@ -19,9 +19,10 @@ inputs: index: index.html error: index.html region: ap-guangzhou - bucketName: my-bucket + bucket: my-bucket protocol: http replace: false # 是否覆盖式部署 + ignoreHtmlExt: false # 是否是否忽略 html 扩展名,默认 false disableErrorStatus: false # 是否禁用错误码,默认 false autoSetupAcl: true # 自动配置 bucket 访问权限为 ”公有读私有写“ autoSetupPolicy: false # 自动配置 bucket 的 Policy 权限为 ”所有用户资源可读“ @@ -92,19 +93,20 @@ inputs: 主要参数描述 -| 参数名称 | 必选 | 类型 | 默认值 | 描述 | -| ------------------ | :--: | :-------------- | :------------: | :---------------------------------------------------------------------------------------- | -| src | 是 | [Src](#Src)[] | | 该项目的代码信息,参数参考执行目录 | -| bucketName | 是 | string | | Bucket 名称。 不允许大写字母。如果你不加 AppId 后缀,则默认自动会为你加上。 | -| region | 否 | string | `ap-guangzhou` | 代码上传所在的 cos 区域。区。 | -| replace | 否 | boolean | `false` | 是否是替换式部署,如果为 `true`,部署时将 `先删除对应 bucket 的所有旧文件`。 | -| protocol | 否 | string | `https` | 请求协议。`https` 或 `http` | +| 参数名称 | 必选 | 类型 | 默认值 | 描述 | +| ------------------ | :--: | :-------------- | :------------: | :----------------------------------------------------------- | +| src | 是 | [Src](#Src) | | 该项目的代码信息,参数参考执行目录 | +| bucket | 是 | string | | Bucket 名称。 不允许大写字母。如果你不加 AppId 后缀,则默认自动会为你加上。 | +| region | 否 | string | `ap-guangzhou` | 代码上传所在的 cos 区域。区。 | +| replace | 否 | boolean | `false` | 是否是替换式部署,如果为 `true`,部署时将 `先删除对应 bucket 的所有旧文件`。 | +| protocol | 否 | string | `https` | 请求协议。`https` 或 `http` | | env | 否 | [Env](#Env) | | 环境变量参数文件。会将 env 下配置的参数写入 env.js 文件中,将该文件打包上传到你的代码里。 | -| cors | 否 | [Cors](#Cors)[] | | 跨域访问配置 | -| hosts | 否 | [Cdn](#Cdn)[] | | CND 加速域名配置 | -| autoSetupAcl | 否 | boolean | `true` | 自动配置 bucket 访问权限为 ”公有读私有写“ | -| autoSetupPolicy | 否 | boolean | `false` | 自动配置 bucket 的 Policy 权限为 ”所有用户资源可读“ | -| disableErrorStatus | 否 | boolean | `false` | 是否禁用错误码,默认 false,不存在文件会返回 404;如果禁用,就会返回 200 | +| cors | 否 | [Cors](#Cors)[] | | 跨域访问配置 | +| hosts | 否 | [Cdn](#Cdn)[] | | CND 加速域名配置 | +| autoSetupAcl | 否 | boolean | `true` | 自动配置 bucket 访问权限为 ”公有读私有写“ | +| autoSetupPolicy | 否 | boolean | `false` | 自动配置 bucket 的 Policy 权限为 ”所有用户资源可读“ | +| disableErrorStatus | 否 | boolean | `false` | 是否禁用错误码,默认 false,不存在文件会返回 404;如果禁用,就会返回 200 | +| ignoreHtmlExt | 否 | boolean | `false` | 是否忽略 html 扩展名,默认 false | > 针对 COS 静态资源托管,通常需要配置所有用户公有读私有写,website 组件因此默认通过配置 `autoSetupAcl` 为 `true`,来帮助用户自动配置访问权限为 `公有读私有写`,由于 COS 针对账号的 ACL 配置条数有 1000 限制,当子账号很多的情况下,通过 `autoSetupAcl` 来配置 ACL 可能超过上限。此时用户可以配置 `autoSetupAcl` 为 `false`,同时配置 `autoSetupPolicy` 为 `true`,来解决此问题。 @@ -127,13 +129,13 @@ inputs: 参考: https://cloud.tencent.com/document/product/436/8279 -| 参数 | 必选 | 类型 | Description | -| -------------- | :--: | :------- | :--------------------------------------------------------------------------------------------- | -| id | 否 | string | 规则 ID | -| allowedMethods | 是 | string[] | 允许的 HTTP 操作,枚举值:GET,PUT,HEAD,POST,DELETE | -| allowedOrigins | 是 | string[] | 允许的访问来源,支持通配符`*`,格式为:`协议://域名[:端口]`,例如:`http://www.qq.com` | -| allowedHeaders | 是 | | 在发送 OPTIONS 请求时告知服务端,接下来的请求可以使用哪些自定义的 HTTP 请求头部,支持通配符`*` | -| maxAgeSeconds | 是 | | 设置 OPTIONS 请求得到结果的有效期 | +| 参数 | 必选 | 类型 | Description | +| -------------- | :--: | :------- | :----------------------------------------------------------- | +| id | 否 | string | 规则 ID | +| allowedMethods | 是 | string[] | 允许的 HTTP 操作,枚举值:GET,PUT,HEAD,POST,DELETE | +| allowedOrigins | 是 | string[] | 允许的访问来源,支持通配符`*`,格式为:`协议://域名[:端口]`,例如:`http://www.qq.com` | +| allowedHeaders | 是 | string[] | 在发送 OPTIONS 请求时告知服务端,接下来的请求可以使用哪些自定义的 HTTP 请求头部,支持通配符`*` | +| maxAgeSeconds | 是 | number | 设置 OPTIONS 请求得到结果的有效期 | ### Cdn @@ -148,11 +150,11 @@ CDN 配置 | refreshCdn | 否 | [RefreshCdn](#RefreshCdn) | | 刷新 CDN 相关配置 | | pushCdn | 否 | [PushCdn](#PushCdn) | | 预热 CDN 相关配置(#pushCdn) | | host | 是 | string | | 需要接入的 CDN 域名。 | -| https | 否 | [Https](https://cloud.tencent.com/document/api/228/30987#Https) | | Https 加速配置 | -| cacheKey | 否 | [CacheKey](https://cloud.tencent.com/document/api/228/30987#CacheKey) | | 节点缓存键配置 | -| cache | 否 | [Cache](https://cloud.tencent.com/document/api/228/30987#Cache) | | 缓存过期时间配置 | -| referer | 否 | [Referer](https://cloud.tencent.com/document/api/228/30987#Referer) | '' | 防盗链设置 | -| ipFilter | 否 | [IpFilter](https://cloud.tencent.com/document/api/228/30987#IpFilter) | '' | IP 黑白名单配置 | +| https | 否 | [Https](#Https) | | Https 加速配置 | +| cacheKey | 否 | [CacheKey](#CacheKey) | | 节点缓存键配置 | +| cache | 否 | [Cache](#Cache) | | 缓存过期时间配置 | +| referer | 否 | [Referer](#Referer) | '' | 防盗链设置 | +| ipFilter | 否 | [IpFilter](#IpFilter) | '' | IP 黑白名单配置 | 使用中国境外加速、全球加速时,需要先开通中国境外加速服务。 @@ -166,7 +168,7 @@ global:全球加速 > 注意:`async` 参数对于配置多个 CDN 域名需求,或者在 CI 流程中时,建议配置成 `true`,不然会导致 serverless cli 执行超时,或者 CI 流程超时。 -更多配置,请移至官方云 API 文档:https://cloud.tencent.com/document/product/228/41123 +更多配置详情,请移至官方云 API 文档:https://cloud.tencent.com/document/product/228/41123 #### RefreshCdn @@ -180,9 +182,279 @@ global:全球加速 | -------- | :--: | :------- | ---- | ------------------- | | urls | 否 | string[] | [] | 需要预热的 CDN URLs | +#### Https + +域名 https 加速配置 + +| 参数名称 | 必选 | 类型 | 默认 | 描述 | +| -------------- | :--: | ------------------------- | ---- | ------------------------------------------------------------ | +| switch | 是 | string | | https 配置开关。on: 开启;off:关闭;此字段可能返回 null,表示取不到有效值 | +| http2 | 否 | string | | http2 配置开关。on: 开启;off:关闭;初次启用 https 加速会默认开启 http2 配置 | +| ocspStapling | 否 | string | off | OCSP 配置开关。on: 开启;off:关闭;此字段可能返回 null,表示取不到有效值 | +| verifyClient | 否 | string | off | 客户端证书校验功能。on: 开启;off:关闭;此字段可能返回 null,表示取不到有效值 | +| certInfo | 否 | [ServerCert](#ServerCert) | | 服务端证书配置信息 | +| clientCertInfo | 否 | [ClientCert](#ClientCert) | | 客户端证书配置信息 | +| spdy | 否 | string | off | Spdy 配置开关。on: 开启;off:关闭 | +| sslStatus | 否 | string | | https 证书部署状态。closed:已关闭;deploying:部署中;deployed:部署成功;failed:部署失败 | +| hsts | 否 | [Hsts](#Hsts) | | Hsts配置 | +| tlsVersion | 否 | string[] | | Tls版本设置,仅支持部分Advance域名,支持设置 TLSv1, TLSv1.1, TLSv1.2, TLSv1.3,修改时必须开启连续的版本 | + +#### ServerCert + +https 加速服务端证书配置 + +| 参数名称 | 必选 | 类型 | 默认 | 描述 | +| :---------- | ---- | :-------- | ---- | :------------------------------------------------------ | +| certId | 否 | string | | 服务器证书 ID 在 SSL 证书管理进行证书托管时自动生成 | +| certName | 否 | string | | 服务器证书名称 在 SSL 证书管理进行证书托管时自动生成 | +| certificate | 否 | string | | 服务器证书信息 上传自有证书时必填,需要包含完整的证书链 | +| privateKey | 否 | string | | 服务器密钥信息 上传自有证书时必填 | +| expireTime | 否 | timestamp | | 证书过期时间 作为入参配置时无需填充 | +| deployTime | 否 | timestamp | | 证书颁发时间 作为入参配置时无需填充 | +| message | 否 | string | | 证书备注信息 | + +#### ClientCert + +https 客户端证书配置 + +| 参数名称 | 必选 | 类型 | 默认 | 描述 | +| :---------- | ---- | :-------- | ---- | :----------------------------------------- | +| certificate | 是 | string | | 客户端证书 PEM 格式,需要进行 Base 64 编码 | +| certName | 否 | string | | 客户端证书名称 | +| expireTime | 否 | timestamp | | 证书过期时间 作为入参时无需填充 | +| deployTime | 否 | timestamp | | 证书颁发时间 作为入参时无需填充 | + +#### Hsts + +HSTS 配置 + +| 参数名称 | 必选 | 类型 | 默认 | 描述 | +| :---------------- | ---- | :----- | ---- | :---------------------- | +| switch | 是 | string | | 是否开启,on或off。 | +| maxAge | 否 | number | | MaxAge数值 | +| includeSubDomains | 否 | string | | 是否包含子域名,on或off | + +#### CacheKey + +缓存键配置 + +| 参数名称 | 必选 | 类型 | 默认 | 描述 | +| :----------- | ---- | :-------------------------------- | ---- | :----------------------------------------------------------- | +| fullUrlCache | 否 | string | | 是否开启全路径缓存 on:开启全路径缓存(即关闭参数忽略) off:关闭全路径缓存(即开启参数忽略) | +| ignoreCase | 否 | string | | 是否忽略大小写缓存 | +| querystring | 否 | [QuerystringKey](#QuerystringKey) | | CacheKey中包含请求参数 | +| cookie | 否 | [CookieKey](#CookieKey) | | CacheKey中包含Cookie | +| header | 否 | [HeaderKey](#HeaderKey) | | CacheKey中包含请求头部 | +| cacheTag | 否 | [CacheTagKey](#CacheTagKey) | | CacheKey中包含自定义字符串 | +| scheme | 否 | [SchemeKey](#SchemeKey) | | CacheKey中包含请求协议 | +| keyRules | 否 | [KeyRule](#KeyRule)[] | | 分路径缓存键配置 | + +#### QuerystringKey + +组成CacheKey的一部分 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :------ | ---- | :----- | :--- | :----------------------------------------------------------- | +| switch | 是 | string | | on \| off CacheKey是否由Querystring组成 | +| reorder | 否 | string | | 是否重新排序 | +| action | 否 | string | | includeAll \| excludeAll \| includeCustom \| excludeAll 使用/排除部分url参数 | +| value | 否 | string | | 使用/排除的url参数数组,';' 分割 | + +#### CookieKey + +组成CacheKey的一部分 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----- | ---- | :----- | :--- | :---------------------------------------- | +| switch | 是 | string | | on \| off 是否使用Cookie作为Cache的一部分 | +| value | 否 | string | | 使用的cookie,';' 分割 | + +#### HeaderKey + +组成CacheKey + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----- | ---- | :----- | :--- | :--------------------------------- | +| switch | 否 | string | | 是否组成Cachekey | +| value | 否 | string | | 组成CacheKey的header数组,';' 分割 | + +#### CacheTagKey + +组成CacheKey的一部分 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----- | ---- | :----- | :--- | :----------------------------------- | +| switch | 是 | string | | 是否使用CacheTag作为CacheKey的一部分 | +| value | 否 | string | | 自定义CacheTag的值 | + +#### SchemeKey + +作为CacheKey的一部分 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----- | ---- | :----- | :--- | :-------------------------------------------- | +| switch | 是 | string | | on \| off 是否使用scheme作为cache key的一部分 | + +#### KeyRule + +缓存键分路径配置 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----------- | ---- | :---------------------------------- | :--- | :----------------------------------------------------------- | +| rulePaths | 是 | string[] | | CacheType 对应类型下的匹配内容: file 时填充后缀名,如 jpg、txt directory 时填充路径,如 /xxx/test path 时填充绝对路径,如 /xxx/test.html index 时填充 / | +| ruleType | 是 | string | | 规则类型: file:指定文件后缀生效 directory:指定路径生效 path:指定绝对路径生效 index:首页 | +| fullUrlCache | 是 | string | | 是否开启全路径缓存 on:开启全路径缓存(即关闭参数忽略) off:关闭全路径缓存(即开启参数忽略) | +| ignoreCase | 是 | string | | 是否忽略大小写缓存 | +| querystring | 是 | [RuleQuerystring](#RuleQuerystring) | | CacheKey中包含请求参数 | +| ruleTag | 是 | string | | 路径缓存键标签,传 user | + +#### RuleQuerystring + +路径保留参数配置 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----- | ---- | :----- | :--- | :-------------------------------------- | +| switch | 是 | string | | on \| off CacheKey是否由Querystring组成 | +| action | 是 | string | | includeCustom 包含部分url参数 | +| value | 是 | string | | 使用/排除的url参数数组,';' 分割 | + +#### Cache + +节点缓存过期时间配置 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :---------- | ---- | :----------------------------------------------------------- | :--- | :------------------- | +| simpleCache | 否 | [SimpleCache](#SimpleCache) | | 基础缓存过期时间配置 | +| ruleCache | 否 | [RuleCache](#RuleCache)[] | | 高级路径缓存配置 | + +#### SimpleCache + +缓存配置基础版本 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----------------- | ---- | :------------------------------------ | :--- | :----------------------------------------------------------- | +| cacheRules | 是 | [SimpleCacheRule](#SimpleCacheRule)[] | | 缓存过期时间规则 | +| followOrigin | 是 | string | | 遵循源站 Cache-Control: max-age 配置 on:开启 off:关闭 | +| ignoreCacheControl | 是 | string | | 强制缓存 on:开启 off:关闭,开启后,源站返回的 no-store、no-cache 资源,也将按照 CacheRules 规则进行缓存 | +| ignoreSetCookie | 是 | string | | 忽略源站的Set-Cookie头部 on:开启 off:关闭 | +| compareMaxAge | 是 | string | | 高级缓存过期配置,开启时会对比源站返回的 max-age 值与 CacheRules 中设置的缓存过期时间,取最小值在节点进行缓存 on:开启 off:关闭 | +| revalidate | 否 | [Revalidate](#Revalidate) | | 总是回源站校验 | + +#### SimpleCacheRule + +缓存过期规则配置 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :------------ | ---- | :------- | :--- | :----------------------------------------------------------- | +| cacheType | 是 | string | | 规则类型: all:所有文件生效 file:指定文件后缀生效 directory:指定路径生效 path:指定绝对路径生效 index:首页 | +| cacheContents | 是 | string[] | | CacheType 对应类型下的匹配内容: all 时填充 * file 时填充后缀名,如 jpg、txt directory 时填充路径,如 /xxx/test path 时填充绝对路径,如 /xxx/test.html index 时填充 / | +| cacheTime | 是 | number | | 缓存过期时间设置 单位为秒,最大可设置为 365 天 | + +#### Revalidate + +是否回源站校验 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----- | ---- | :----- | :--- | :------------------------- | +| switch | 是 | String | | on \| off 是否总是回源校验 | +| path | 否 | String | | 只在特定请求路径回源站校验 | + +#### RuleCache + +缓存配置分路径版本 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :---------- | ---- | :---------------------------------- | :--- | :----------------------------------------------------------- | +| rulePaths | 是 | string[] | | CacheType 对应类型下的匹配内容: all 时填充 * file 时填充后缀名,如 jpg、txt directory 时填充路径,如 /xxx/test path 时填充绝对路径,如 /xxx/test.html index 时填充 / | +| ruleType | 是 | string | | 规则类型: all:所有文件生效 file:指定文件后缀生效 directory:指定路径生效 path:指定绝对路径生效 index:首页 | +| cacheConfig | 是 | [RuleCacheConfig](#RuleCacheConfig) | | 缓存配置 | + +#### RuleCacheConfig + +路径缓存缓存配置(三种缓存模式中选取一种) + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----------- | ---- | :-------------------------------------------------- | :--- | :----------- | +| cache | 是 | [CacheConfigCache](#CacheConfigCache) | | 缓存配置 | +| noCache | 是 | [CacheConfigNoCache](#CacheConfigNoCache) | | 不缓存配置 | +| followOrigin | 是 | [CacheConfigFollowOrigin](#CacheConfigFollowOrigin) | | 遵循源站配置 | + +#### CacheConfigCache + +路径缓存缓存配置 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----------------- | ---- | :----- | :--- | :----------------------------------------------------------- | +| switch | 是 | string | | 缓存配置开关 on:开启 off:关闭 | +| cacheTime | 是 | number | | 缓存过期时间设置 单位为秒,最大可设置为 365 天 | +| compareMaxAge | 是 | string | | 高级缓存过期配置,开启时会对比源站返回的 max-age 值与 CacheRules 中设置的缓存过期时间,取最小值在节点进行缓存 on:开启 off:关闭 | +| ignoreCacheControl | 是 | string | | 强制缓存 on:开启 off:关闭 默认为关闭状态,开启后,源站返回的 no-store、no-cache 资源,也将按照 CacheRules 规则进行缓存 | +| ignoreSetCookie | 是 | string | | 当源站返回Set-Cookie头部时,节点是否缓存该头部及body on:开启,不缓存该头部及body off:关闭,遵循用户自定义的节点缓存规则 | + +#### CacheConfigNoCache + +路径缓存不缓存配置 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :--------- | ---- | :----- | :--- | :-------------------------------- | +| switch | 是 | string | | 不缓存配置开关 on:开启 off:关闭 | +| revalidate | 是 | string | | 总是回源站校验 on:开启 off:关闭 | + +#### CacheConfigFollowOrigin + +路径缓存遵循源站配置 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----- | ---- | :----- | :--- | :---------------------------------- | +| switch | 是 | string | | 遵循源站配置开关 on:开启 off:关闭 | + +#### Referer + +Referer 黑白名单配置,默认为关闭状态 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :----------- | ---- | :---------------------------- | :--- | :------------------------------------------ | +| switch | 是 | string | | referer 黑白名单配置开关 on:开启 off:关闭 | +| refererRules | 否 | [RefererRule](#RefererRule)[] | | referer 黑白名单配置规则 | + +#### RefererRule + +Referer 黑白名单配置规则,针对特定资源生效 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :---------- | ---- | :------- | :--- | :----------------------------------------------------------- | +| ruleType | 是 | string | | 规则类型: all:所有文件生效 file:指定文件后缀生效 directory:指定路径生效 path:指定绝对路径生效 | +| rulePaths | 是 | string[] | | RuleType 对应类型下的匹配内容: all 时填充 * file 时填充后缀名,如 jpg、txt directory 时填充路径,如 /xxx/test/ path 时填充绝对路径,如 /xxx/test.html | +| refererType | 是 | string | | referer 配置类型 whitelist:白名单 blacklist:黑名单 | +| referers | 是 | string[] | | referer 内容列表列表 | +| allowEmpty | 是 | Boolean | | 是否允许空 referer true:允许空 referer false:不允许空 referer | + +#### IpFilter + +IP 黑白名单配置,默认为关闭状态 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :---------- | ---- | :-------------------------------------- | :--- | :----------------------------------------------------------- | +| switch | 是 | string | | IP 黑白名单配置开关 on:开启 off:关闭 | +| filterType | 否 | string | | IP 黑白名单类型 whitelist:白名单 blacklist:黑名单 | +| filters | 否 | string[] | | IP 黑白名单列表 支持 X.X.X.X 形式 IP,或 /8、 /16、/24 形式网段 最多可填充 50 个白名单或 50 个黑名单 | +| filterRules | 否 | [IpFilterPathRule](#IpFilterPathRule)[] | | IP 黑白名单分路径配置,白名单功能 | +| returnCode | 否 | number | | IP 黑白名单验证失败时返回的 HTTP Code 合法值: 400~499 | + +#### IpFilterPathRule + +IP黑白名单分路径配置 + +| 名称 | 必选 | 类型 | 默认 | 描述 | +| :--------- | ---- | :------- | :--- | :----------------------------------------------------------- | +| filterType | 是 | string | | IP 黑白名单类型 whitelist:白名单 blacklist:黑名单 | +| filters | 是 | string[] | | IP 黑白名单列表 支持 X.X.X.X 形式 IP,或 /8、 /16、/24 形式网段 最多可填充 50 个白名单或 50 个黑名单 | +| ruleType | 是 | string | | 规则类型: all:所有文件生效 file:指定文件后缀生效 directory:指定路径生效 path:指定绝对路径生效 | +| rulePaths | 是 | string[] | | RuleType 对应类型下的匹配内容: all 时填充 * file 时填充后缀名,如 jpg、txt directory 时填充路径,如 /xxx/test/ path 时填充绝对路径,如 /xxx/test.html | + ### Env -环境变量参数文件。会将 env 下配置的参数写入 env.js 文件中,将该文件打包上传到你的代码里。 +环境变量参数。serverless 部署时会将 `env` 下配置的参数写入 `env.js` 文件中,将该文件打包上传到相对于 `src` 的 `envPath` 目录,默认为 `src` 指定目录。 比如配置了: @@ -198,4 +470,4 @@ window.env = {} window.env.API_URL = 'https://api.com' ``` -让后我们可以在前端项目中给所有的请求 URL 添加 `window.env.API_URL` 前缀,通常在全栈应用中,会使用到。比如在部署完后端服务后会生产后端服务网关 `url`,然后我们将上面的的 `API_URL` 赋值为后端服务的 `url`,就可以做到无需手动引入修改接口链接了。具体使用请参考 [全栈应用案例](https://github.com/serverless-components/tencent-examples/tree/master/fullstack) +然后,我们可以在前端项目中给所有的请求 URL 添加 `window.env.API_URL` 前缀,通常在全栈应用中,会使用到。比如在部署完后端服务后会生产后端服务网关 `url`,然后我们将上面的的 `API_URL` 赋值为后端服务的 `url`,就可以做到无需手动引入修改接口链接了。具体使用请参考 [全栈应用案例](https://github.com/serverless-components/tencent-examples/tree/master/fullstack) diff --git a/docs/faq.md b/docs/faq.md new file mode 100644 index 0000000..9526e0e --- /dev/null +++ b/docs/faq.md @@ -0,0 +1,33 @@ +# FAQ + +## COS 桶 ACL 策略超限 + +通常报错如下: + +``` +6s > websiteDemo > Your policy or acl has reached the limit +//... +``` + +针对 COS 静态资源托管,通常需要配置所有用户公有读私有写,website 组件因此默认通过配置 `autoSetupAcl` 为 `true`,来帮助用户自动配置访问权限为 `公有读私有写`, +由于 COS 针对账号的 `ACL` 配置条数有 `1000` 限制,当子账号很多的情况下,通过 `autoSetupAcl` 来配置 ACL 可能超过上限。 +此时用户可以配置 `autoSetupAcl` 为 `false`,同时配置 `autoSetupPolicy` 为 `true`,来解决此问题。如下: + +```yaml +app: appDemo +stage: dev +component: website +name: websiteDemo + +inputs: + src: + src: ./src + index: index.html + error: index.html + region: ap-guangzhou + bucket: my-bucket + protocol: https + # 添加以下配置 + autoSetupAcl: false + autoSetupPolicy: true +``` diff --git a/example/serverless.yml b/example/serverless.yml index 1dee9db..7fccd6d 100644 --- a/example/serverless.yml +++ b/example/serverless.yml @@ -1,6 +1,6 @@ -org: orgDemo app: appDemo stage: dev + component: website name: websiteDemo @@ -10,5 +10,5 @@ inputs: index: index.html error: index.html region: ap-guangzhou - bucketName: my-bucket + bucket: website-demo protocol: https diff --git a/serverless.component.yml b/serverless.component.yml index 8b15ff7..65ee942 100644 --- a/serverless.component.yml +++ b/serverless.component.yml @@ -1,8 +1,8 @@ name: website -version: 0.1.3 +version: 0.2.6 author: 'Tencent Cloud, Inc.' org: 'Tencent Cloud, Inc.' -description: Deploy a static website on Tencent Cloud. +description: 静态网站组件,允许用户构建部署静态站点到腾讯 COS。适合用来构建部署静态网站。 keywords: 'tencent, serverless, website' repo: 'https://github.com/serverless-components/tencent-website' readme: 'https://github.com/serverless-components/tencent-website/tree/master/README.md' diff --git a/src/package.json b/src/package.json index e8c0bf0..bcc427a 100644 --- a/src/package.json +++ b/src/package.json @@ -1,7 +1,6 @@ { "dependencies": { "download": "^8.0.0", - "tencent-component-toolkit": "^2.5.3", - "type": "^2.1.0" + "tencent-component-toolkit": "^2.23.3" } } diff --git a/src/serverless.js b/src/serverless.js index c166de4..214e0c8 100644 --- a/src/serverless.js +++ b/src/serverless.js @@ -22,6 +22,10 @@ class ServerlessComponent extends Component { } } + getAppId() { + return this.credentials.tencent.tmpSecrets.appId + } + getDefaultProtocol(protocols) { if (String(protocols).includes('https')) { return 'https' @@ -87,6 +91,7 @@ class ServerlessComponent extends Component { } this.state = { region, + bucket: websiteInputs.bucket, website: websiteInputs } diff --git a/src/utils.js b/src/utils.js index 9318a33..63dd77c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -41,6 +41,7 @@ const removeAppid = (str, appid) => { } const prepareInputs = async (instance, inputs) => { + const { state } = instance let code = inputs.src || '' code = typeof code === 'string' @@ -51,8 +52,8 @@ const prepareInputs = async (instance, inputs) => { ...code } - const zipPath = await getCodeZipPath(this, code) - const { appId } = instance.credentials.tencent.tmpSecrets + const zipPath = await getCodeZipPath(instance, code) + const appId = instance.getAppId() const envPath = (inputs.srcOriginal && inputs.srcOriginal.envPath) || './' let sourceDirectory = await instance.unzip(zipPath) if (!code.src) { @@ -62,7 +63,8 @@ const prepareInputs = async (instance, inputs) => { const region = inputs.region || CONFIGS.region const bucketName = - removeAppid(inputs.bucketName, appId) || `sls-website-${region}-${generateId()}` + removeAppid(inputs.bucketName || inputs.bucket || state.bucket, appId) || + `sls-website-${region}-${generateId()}` const websiteInputs = Object.assign(inputs, { replace: inputs.replace,