我已经开始为我的网站使用工作人员。我网站上的工作人员的目的:主要是在 HTML 页面的不同位置注入一些代码(html/js/css)。它可以是一些配置数据,也可以是一些法律文本等。
所以我现在要做的是在 KV 中为每个网站创建一个配置,并基于用户国家/语言注入 html/js 等。
下面是一个存储类(单例模式),它包含来自配置的所有信息,但在工作人员中不起作用,我的意思是,在第一次请求之后,数据是持久的,并且在一段时间后它会更新:
例如第一个请求 URL:/about
第二个请求 URL:/es/about/
按输出console.log(event.request)
将显示/es/about
,但Store.request
输出:/about/
任何解决方法,强制刷新数据,我想因为我不在构造函数中这样做,但是通过调用自定义方法应该可以解决问题,但是它没有。?
下面是一些代码示例。
import { Store } from "@helpers/store";
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
});
//HTML Rewriter Class
class Head {
element(el) {
el.append(`
<script id="config">
var config = ${Store.export()};
</script>`, {
html: true
});
}
}
async function handleRequest(request) {
let store = await Store.make(request);
const response = await fetch(request);
let html = new HTMLRewriter();
html.on("head", new Head());
return html.transform(response);
}
//src/helpers/store.js
class Store {
constructor(){
this._request = null
this._config = {}
this._url = null
}
async make(request){
let config = {}
this._request = request;
const domain = this.url.hostname.replace(/www\./g, "");
const country = request.headers.get('cf-ipcountry')
const website = await WEBSITES.get(domain, "json"); //WEBSITES is KV namespace
const { license, lang } = website;
this._config = {
country,
domain,
license,
lang
};
return this;
}
export(){
return JSON.stringify(this._config)
}
get request(){
return this._request;
}
get url(){
if(!this._url){
this._url = new URL(this.request.url)
}
return this._url;
}
}
export default new Store()