我有一个追随者wrangler.toml
。当我想使用dev
或preview
(例如npx wrangler dev
或npx wrangler preview
)wrangler 要求将 a 添加preview_id
到 KV 命名空间时。这是现有 KV 命名空间的标识符吗?
我在https://github.com/cloudflare/wrangler/issues/1458看到 Cloudflare Workers GH 中有一张票,告诉我应该澄清这一点,但票已关闭并添加了一条错误消息
为了预览具有 KV 命名空间的工作人员,您必须在配置文件中为要预览的每个 KV 命名空间指定一个 preview_id。”
这就是我在这里的原因。:)
至于更大的上下文,如果有人能澄清一下,我会非常高兴:我看到如果我给出一个现有命名空间的值,我可以预览并且我看到__some-worker-dev-1234-workers_sites_assets_preview
在 Cloudflare 中生成了一个类型的 KV 命名空间。这与在 中使用的标识符指向的 KV 命名空间具有不同的标识符,preview_id
并且由我使用的标识符指向的 KV 命名空间preview_id
是空的。为什么提供现有 KV 命名空间的标识符会删除错误消息、部署资产并允许预览,但实际的 KV 命名空间是空的并创建了一个新命名空间?
kv-asset-handler如何知道查看这个生成的命名空间来检索资产?
我目前正在对我的站点使用默认生成的 Cloudare Worker 进行测试,我想知道我是否误解了某些东西,或者是否有一些机制在预览/发布站点名称空间到 scipt 期间捆绑了一些机制。
如果有一些带有自动映射的随机机制,那么是否可以让每个开发人员都可以拥有自己的私有预览 KV 命名空间?
type = "javascript"
name = "some-worker-dev-1234"
account_id = "<id>"
workers_dev = true
kv_namespaces = [
{ binding = "site_data", id = "<test-site-id>" }
]
[site]
# The location for the site.
bucket = "./dist"
# The entry directory for the package.json that contains
# main field for the file name of the compiled worker file in "main" field.
entry-point = ""
[env.production]
name = "some-worker-1234"
zone_id = "<zone-id>"
routes = [
"http://<site>/*",
"https://www.<site>/*"
]
# kv_namespaces = [
# { binding = "site_data", id = "<production-site-id>" }
# ]
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'
/**
* The DEBUG flag will do two things that help during development:
* 1. we will skip caching on the edge, which makes it easier to
* debug.
* 2. we will return an error message on exception in your Response rather
* than the default 404.html page.
*/
const DEBUG = false
addEventListener('fetch', event => {
try {
event.respondWith(handleEvent(event))
} catch (e) {
if (DEBUG) {
return event.respondWith(
new Response(e.message || e.toString(), {
status: 500,
}),
)
}
event.respondWith(new Response('Internal Error', { status: 500 }))
}
})
async function handleEvent(event) {
const url = new URL(event.request.url)
let options = {}
/**
* You can add custom logic to how we fetch your assets
* by configuring the function `mapRequestToAsset`
*/
// options.mapRequestToAsset = handlePrefix(/^\/docs/)
try {
if (DEBUG) {
// customize caching
options.cacheControl = {
bypassCache: true,
}
}
const page = await getAssetFromKV(event, options)
// allow headers to be altered
const response = new Response(page.body, page)
response.headers.set('X-XSS-Protection', '1; mode=block')
response.headers.set('X-Content-Type-Options', 'nosniff')
response.headers.set('X-Frame-Options', 'DENY')
response.headers.set('Referrer-Policy', 'unsafe-url')
response.headers.set('Feature-Policy', 'none')
return response
} catch (e) {
// if an error is thrown try to serve the asset at 404.html
if (!DEBUG) {
try {
let notFoundResponse = await getAssetFromKV(event, {
mapRequestToAsset: req => new Request(`${new URL(req.url).origin}/404.html`, req),
})
return new Response(notFoundResponse.body, { ...notFoundResponse, status: 404 })
} catch (e) {}
}
return new Response(e.message || e.toString(), { status: 500 })
}
}
/**
* Here's one example of how to modify a request to
* remove a specific prefix, in this case `/docs` from
* the url. This can be useful if you are deploying to a
* route on a zone, or if you only want your static content
* to exist at a specific path.
*/
function handlePrefix(prefix) {
return request => {
// compute the default (e.g. / -> index.html)
let defaultAssetKey = mapRequestToAsset(request)
let url = new URL(defaultAssetKey.url)
// strip the prefix from the path for lookup
url.pathname = url.pathname.replace(prefix, '/')
// inherit all other props from the default request
return new Request(url.toString(), defaultAssetKey)
}
}