[英]AWS CDN + Lambda@Edge MimeType Issue
I've been working lately with AWS CDN + S3 bucket as origin.我最近一直在使用 AWS CDN + S3 存储桶作为来源。
When no lambda@edge function attached into the Origin Request
it works fine but I need the lambda@edge fn for rerouting.当Origin Request
中没有附加 lambda@edge function 时,它工作正常,但我需要 lambda@edge fn 来重新路由。 Some of the js file's mimetype are not text/javascript
instead text/html
.一些 js 文件的 mimetype 不是text/javascript
而不是text/html
。 Any idea?任何的想法?
Lambda@edge function is just a simple rerouting no special feature. Lambda@edge function 只是一个简单的重新路由,没有特殊功能。
Here is the function:这是 function:
'use strict';
exports.handler = (evt, context, cb) => {
const { request } = evt.Records[0].cf;
const uriParts = request.uri.split("/")[1];
const locales = ['en-US', 'ja', 'ms'];
if (!uriParts || !locales.includes(uriParts)) {
request.uri = '/en-US/index.html';
return cb(null, request)
}
request.uri = `/${uriParts}/index.html`;
console.log(`Request Uri: ${request.uri}`);
cb(null, request);
}
Setup:设置:
S3 Bucket
en-US/
index.html
<bunch of js files from angular>
ja/
index.html
<bunch of js files from angular>
ms/
index.html
<bunch of js files from angular>
CDN Distribution + Origin is the S3
Solved.解决了。 First of all, my bad for not understanding well the triggers of Lambda@Edge function. Refer to this article .首先,我不好理解 Lambda@Edge function 的触发器。请参阅这篇文章。
First try, I used origin request
as s trigger and somehow came to understand that it wasn't I need for my case.第一次尝试,我使用origin request
作为 s 触发器,并且以某种方式了解到我的情况不需要它。
After some debug and reading, I used viewer request
this time.经过一些调试和阅读,这次我使用了viewer request
。 Also, alter my Lambda@Edge function to this.此外,将我的 Lambda@Edge function 更改为此。
'use strict';
const path = require("path");
exports.handler = async (evt,context,cb) => {
const { request } = evt.Records[0].cf;
console.log(`Original Uri: ${request.uri}`);
const uriParts = request.uri.split("/");
const locale = uriParts.length > 1 ? uriParts[1] : "";
const locales = ["en-US", "ja", "ms"];
if (locale === "" || locale === "index.html") {
request.uri = "/en-US/index.html";
return cb(null, request);
}
if (!locales.includes(locale)) return cb(null, request);
const lastPartUrl = uriParts[uriParts.length - 1];
const fileExt = path.extname(lastPartUrl);
if (!fileExt) request.uri = `/${locale}/index.html`;
console.log(`New Uri: ${request.uri}`);
return cb(null, request);
};
Now every locale
in our application is now working fine.现在我们应用程序中的每个locale
都可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.