Greg Brimble | Cloudflare Pages
01/17/2022, 1:45 AMGreg Brimble | Cloudflare Pages
01/17/2022, 1:45 AMcacheKey
but `match`ing on the Request.Cоlе
01/17/2022, 7:38 AMwaitUntil
on? I know normally in workers you call it on event, but can I access the event from a function?Cоlе
01/17/2022, 7:40 AMput(cachekey
now, but it doesn't seem to cache. the cache folder doesn't appear in .mf/
anymoreCоlе
01/17/2022, 7:46 AMts
export const onRequestGet: PagesFunction<{}> = async ({ request, env }): Promise<Response> => {
const doc = "getKits";
const subject = "0042N";
cache = cache || await caches.open('kits');
const cacheKey = new Request(request.url, {
headers: request.headers,
method: 'GET'
});
const cached = await cache.match(cacheKey);
console.log(cached);
if (cached) {
const cloned = cached.clone();
cloned.headers.set('cf-cache-status', 'HIT');
return cloned;
}
let real = GetMongoDocument(env, collectionName, { _id: query(subject!) }).then(async (res) => {
const response = toJSON(res);
cache.put(cacheKey, response.clone());
response.headers.set('cf-cache-status', 'MISS');
return response;
}).catch((err) => {
return toError(Errors.ERR_INTERNAL_ERROR, doc, err);
});
return real;
};
Zelderon
01/17/2022, 8:39 AMexport async function onRequestGet() {
let request = new Request(`https://mapi.storyblok.com/`);
let res = await fetch(request)
return new Response(JSON.stringify({res: res, content: await res.text()}))
}
it works fine at the following pages.dev URL (no custom domain attached, status 200 returned with content): https://cf-pages-fetch-error.pages.dev/basic-fetch-https
but not at my pages.dev URL with a custom domain attached (status 525 returned).
Any idea why? I can provide the other pages.dev URL privately.HardAtWork
01/17/2022, 9:01 AMoscartbeaumont
01/17/2022, 1:17 PMtest.html
from a function called [path].ts
. In trying to make this work I came across the GitHub issue cloudflare/wrangler2#232 which states return next("/test.html");
is not currently working. I am wondering if a workaround exists until this feature is fixed (potentially using env.ASSETS.fetch
)? I made a gist (https://gist.github.com/oscartbeaumont/56e7aaf2a6d510c8a5d48de47e2f2e51) which shows what I am trying to do and my current code. Any help is appreciated.Greg Brimble | Cloudflare Pages
01/17/2022, 1:19 PMts
env.ASSETS.fetch(new Request("http://fakehost/test.html", request))
should work in the meantimeoscartbeaumont
01/17/2022, 1:21 PMFailed to parse URL from /test.html
Greg Brimble | Cloudflare Pages
01/17/2022, 1:25 PMGreg Brimble | Cloudflare Pages
01/17/2022, 1:25 PMoscartbeaumont
01/17/2022, 1:27 PM/test
and just loops until my browser returns ERR_TOO_MANY_REDIRECTS.Greg Brimble | Cloudflare Pages
01/17/2022, 1:30 PMredirect
property to follow
? I haven't tried that myself, but that should collapse the redirect inside the Function and just return a normal non-redirecting response.Greg Brimble | Cloudflare Pages
01/17/2022, 1:31 PMfunctions/test.js
?oscartbeaumont
01/17/2022, 1:41 PMfunctions/[path].ts
so it must be looping with itself. I changed the function to be called functions/test.js
temporarily and the function returns a 301 redirect to /test
instead of serving the file directly. If I set request.redirect = "follow";
before using the asset fetch you provided above it makes no difference.Greg Brimble | Cloudflare Pages
01/17/2022, 1:46 PMGreg Brimble | Cloudflare Pages
01/17/2022, 1:46 PMGreg Brimble | Cloudflare Pages
01/17/2022, 1:47 PM/test
, so you could just:
ts
env.ASSETS.fetch(new Request("http://fakehost/test", request))
If you need it dynamically, you could get the redirection from the Location
header of the response.oscartbeaumont
01/17/2022, 1:58 PMif (url.pathname === "/test") { return await next(); }
and it now everything works when the function is called [path].ts
. Thanks heaps for your help, I am looking forwards to these issues being fixed!Greg Brimble | Cloudflare Pages
01/17/2022, 2:04 PMZelderon
01/17/2022, 2:37 PMe0
01/17/2022, 5:20 PMGreg Brimble | Cloudflare Pages
01/17/2022, 6:16 PMCоlе
01/17/2022, 6:35 PMCоlе
01/17/2022, 6:35 PMcache.match()
😦Cоlе
01/17/2022, 8:14 PMresponse.headers.append("Cache-Control", "s-maxage=xxx")
is not optional when cache.put(response)
HardAtWork
01/17/2022, 8:28 PMCloudflare-CDN-Cache-Control
header.Cоlе
01/17/2022, 8:31 PMStew
01/17/2022, 10:44 PM