Greg Brimble | Cloudflare Pages
12/06/2021, 9:12 AMrobinio
12/06/2021, 10:09 AMfragje
12/06/2021, 2:24 PMnpx wrangler pages dev ./build --kv=KV
And in wrangler.toml
I have added:
kv_namespaces = [
{ binding = "KV", id = "<id to production kv>", preview_id = "<id to preview kv>" }
]
In functions/test.js
I await the value from KV and return it.
export async function onRequest({env}) {
const value = await env.KV.get('click');
console.log(value);
return new Response(value);
}
Works in production, but not locally. What am I missing?Walshy | Pages
12/06/2021, 2:28 PMjacobmarble
12/06/2021, 2:55 PM--do
known to work properly in wrangler beta.6?jacobmarble
12/06/2021, 2:56 PMrobinio
12/06/2021, 2:58 PMchrisjmccreadie
12/06/2021, 3:17 PMrobinio
12/06/2021, 3:22 PMGreg Brimble | Cloudflare Pages
12/06/2021, 3:37 PMts
// ./functions/my/page.js
// https://my.domain/my/page
export const onRequest = async ({ next }) => {
const response = await next()
return new HTMLRewriter().on('head', {
element(element) {
element.append('<meta property="og:title" content="A cool new title for OpenGraph" />', { html: true })
}
}).transform(response)
}
Not tested, but that should do it!robinio
12/06/2021, 3:42 PMGreg Brimble | Cloudflare Pages
12/06/2021, 3:47 PMif (response.headers.get('Content-Type').match(/text\/html/)
fragje
12/06/2021, 3:54 PMrobinio
12/06/2021, 4:00 PMexport const onRequest = async ({ next }) => {
const response = await next();
if (response.headers.get('Content-Type').match(/text\/html/)) {
return new HTMLRewriter()
.on('head', {
element(element) {
element.append('<meta property="og:title" content="A cool new title for OpenGraph" />', { html: true });
},
})
.transform(response);
} else {
return response;
}
};
oliverjam
12/06/2021, 4:14 PMjschlesser
12/06/2021, 4:17 PMrobinio
12/06/2021, 4:23 PMevent.passThroughOnException()
to make the site does not break on accident?
My current solution is a huge try and catch block, but that's not really ideal IMHO.HardAtWork
12/06/2021, 4:29 PMctx.passThroughOnException()
?chrisjmccreadie
12/06/2021, 4:37 PMchrisjmccreadie
12/06/2021, 4:39 PMHardAtWork
12/06/2021, 4:52 PMctx.passThroughOnException()
is not currently supported in Functions, but that might be a future feature, so stick around.oliverjam
12/06/2021, 4:53 PMawait env.KV.get('click')
locally and not seeing any data. You won't see KV data set in prod while you are running locally with Wrangler, even if the namespace is the same.jschlesser
12/06/2021, 5:15 PMoliverjam
12/06/2021, 5:19 PM[[path]]
syntax. Confirmed above: https://discord.com/channels/595317990191398933/910978223968518144/917171795155636294jschlesser
12/06/2021, 5:20 PMjacobmarble
12/06/2021, 8:58 PMBenQoder
12/07/2021, 6:09 AMgrant
12/07/2021, 3:08 PMBenQoder
12/07/2021, 10:30 PMErwin
12/08/2021, 12:51 AM