问题标签 [cloudflare-workers]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
188 浏览

javascript - Cloudflare 工作人员:防止带有参数的 URL 的 302(来自磁盘缓存)

我有一个工作人员,我想在 URL 以这种方式具有特定参数时绕过缓存:

我不确定我做错了什么,请求的响应总是302 (from disk cache)这会导致我的站点出现错误,因为我需要避免这些请求被缓存。我的工人可能有什么问题?或者我的缓存设置有问题?

以下是完整回复:

请求响应

0 投票
1 回答
194 浏览

javascript - TypeError:“RequestInitializerDict”上“headers”字段的类型不正确:提供的值不是“variant”类型

我是 JavaScript 新手,我正在尝试使用 Cloudflare Worker 上的 JS 为 IFTTT 制作一个 Github API 网关(因为它无法修改标头)。这是代码:

当我尝试运行它时出现此错误:

这是一个运行良好但灵活性较低的旧版本:

唯一的区别似乎是apiRequest,但我不知道如何解决它。我试图先声明变量,var apiRequest = new Object()但没有奏效。

0 投票
1 回答
1150 浏览

serverless - Cloudflare Workers: Module not found: Error: Can't resolve 'child_process' in '~/node_modules/nodemailer/lib/sendmail-transport'

I was getting the following error with the Nodemailer package in a Cloudflare Worker and wanted to document what I found.

0 投票
1 回答
76 浏览

browser - 为什么 Cloudflare worker 上的站点预览与浏览器不同?

我在 Cloudflare 中有一个域A.tk,并且我的服务器无法将端口 80 与 domain 一起使用A.top。访问的唯一方法A.topA.top:61445,所以我尝试fetch('A.top:61445')在 Cloudflare worker 中,并在访问时更改响应A.tk。worker 一切正常,浏览器不行,问题在哪里

0 投票
1 回答
143 浏览

python - Cloudflare KV API Bulk PUT - 错误 10012 - 无法将 KVPair 解组为中间结构

用 Python 向 CF Workers KV写入大量内容PUT,但无法弄清楚这里出了什么问题。据我所知,我的 JSON 是有效的。

这是我得到的错误

错误

{ "result": null, "success": false, "errors": [ { "code": 10012, "message": "could not parse array of key/value objects from request body: 'could not unmarshal KVPair into intermediate struct: 'json: cannot unmarshal object into Go struct field kvPairJSON.value of type string''" } ], "messages": [] }

有效载荷

[{ “key”:“aals”, “value”:{ “sup”:{ “firo”:“aals”, “mean”:“aals”, “alpha”:[]}}}]

代码

response = requests.put(f"{CF_BASEURL}/bulk", headers=headers, data=json.dumps(payload))

任何想法表示赞赏。我确定这是一个 SMH 时刻......

0 投票
2 回答
1474 浏览

cache-control - Cache-Control headers in a Cloudflare Workers site

I'm just testing Workers sites with a Hugo-created static site. It already existed, so I used the docs’ instructions for adapting an existing site. The cache-control headers for the woff2 and css files all show up with no-cache, contrary to what I'd have expected based on https://support.cloudflare.com/hc/en-us/articles/200172516#h_a01982d4-d5b6-4744-bb9b-a71da62c160a. The Workers site in question is https://hosts-test-hugo.brycewray.workers.dev/.

I found the following at https://levelup.gitconnected.com/use-cloudflare-javascript-workers-to-deploy-you-static-generated-site-ssg-1c518e078646 but don't know if it's related:

A Cloudflare Worker is a piece of JavaScript code that runs every time you access a specific route on a website proxied by Cloudflare. The code is executed on every request before they reach Cloudflare’s cache. This means Worker responses are not cached (although requests made by the worker to other web services might be cached with the appropriate caching headers).

Does the site need to have a custom domain — i.e., rather than being a “.workers.dev” URL — before it will have normal caching behavior? Is that even related?

[Note: I am posting this here because I’ve been unsuccessful in getting a response on either the Cloudflare community forum or the Cloudflare subreddit — hoping for better results here.]

0 投票
1 回答
216 浏览

cloudflare-workers - 牧马人工人:HTMLRewriter.on 与 ElementHandler 不工作

我正在尝试使用我的 Worker 使用以下语法重写 HTML 页面的内容:

这直接取自HTMLRewriter 文档,但每当我点击调用它的端点时,它都会给我这个错误:

这是我第一次使用 Workers;我究竟做错了什么?

0 投票
1 回答
378 浏览

javascript - 使用 Cloudflare Worker 进行部署的问题

我正在尝试使用 wrangler dev 或 wrangler publish 部署我的工作人员,但即使我已经按照文档中的指示将我的帐户 ID 添加到 wrangler.toml 文件中,我仍然收到此错误。

错误:需要字段account_id才能部署到workers.dev

任何帮助将不胜感激!

0 投票
2 回答
241 浏览

javascript - Cloudflare Workers“单例”类

我已经开始为我的网站使用工作人员。我网站上的工作人员的目的:主要是在 HTML 页面的不同位置注入一些代码(html/js/css)。它可以是一些配置数据,也可以是一些法律文本等。

所以我现在要做的是在 KV 中为每个网站创建一个配置,并基于用户国家/语言注入 html/js 等。

下面是一个存储类(单例模式),它包含来自配置的所有信息,但在工作人员中不起作用,我的意思是,在第一次请求之后,数据是持久的,并且在一段时间后它会更新:

例如第一个请求 URL:/about 第二个请求 URL:/es/about/

按输出console.log(event.request)将显示/es/about,但Store.request输出:/about/

任何解决方法,强制刷新数据,我想因为我不在构造函数中这样做,但是通过调用自定义方法应该可以解决问题,但是它没有。?

下面是一些代码示例。

0 投票
1 回答
24 浏览

deployment - 通过 fab.dev 部署时找不到操作模块?

我正在通过 fab.dev 将我的 next.js 应用程序部署到 cloudflare 工作人员,并且在生成 fab.zip 文件后我无法部署它。