murzin
murzin
CDCloudflare Developers
Created by murzin on 9/26/2024 in #workers-help
Difference between Static Assets and Pages?
Hi. I just noticed a Static Assets option in Cloudflare Docs, and I fail to see practical difference between the two. There is a page https://developers.cloudflare.com/workers/static-assets/compatibility-matrix/ that shows the difference in avaliable features, but it still doesn't explain how are two products different specifically. Like for example, if I have a Nuxt3 SSR app with bindings for R2, can't I just use both? Pages also support SSR via Pages Functions. So what's the difference here?
2 replies
CDCloudflare Developers
Created by murzin on 6/29/2024 in #workers-help
workers dont see my R2
hey, so workers dont see my R2 for some reason. here's my simple setup:
export default {
async fetch(request, env, ctx) {
try {
// Check if the R2 bucket binding exists
if (!env.MY_BUCKET) {
throw new Error("R2 bucket binding 'MY_BUCKET' is not defined");
}

const prefix = 'your-prefix/' // Replace with your desired prefix
const options = {
prefix: prefix,
limit: 1000, // Adjust as needed
}

const objects = await env.MY_BUCKET.list(options);
const totalFiles = objects.objects.length;

const first10Files = objects.objects.slice(0, 10).map(object => ({
name: object.key,
lastModified: new Date(object.uploaded).toISOString(),
}));

const latestAdditionTime = first10Files.length > 0
? first10Files.reduce((latest, file) =>
file.lastModified > latest ? file.lastModified : latest,
first10Files[0].lastModified)
: null;

return new Response(JSON.stringify({
totalFiles,
latestAdditionTime,
first10Files,
}, null, 2), {
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
console.error("Error in worker:", error);
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
}
export default {
async fetch(request, env, ctx) {
try {
// Check if the R2 bucket binding exists
if (!env.MY_BUCKET) {
throw new Error("R2 bucket binding 'MY_BUCKET' is not defined");
}

const prefix = 'your-prefix/' // Replace with your desired prefix
const options = {
prefix: prefix,
limit: 1000, // Adjust as needed
}

const objects = await env.MY_BUCKET.list(options);
const totalFiles = objects.objects.length;

const first10Files = objects.objects.slice(0, 10).map(object => ({
name: object.key,
lastModified: new Date(object.uploaded).toISOString(),
}));

const latestAdditionTime = first10Files.length > 0
? first10Files.reduce((latest, file) =>
file.lastModified > latest ? file.lastModified : latest,
first10Files[0].lastModified)
: null;

return new Response(JSON.stringify({
totalFiles,
latestAdditionTime,
first10Files,
}, null, 2), {
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
console.error("Error in worker:", error);
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
}
and here's my wrangler.toml:
name = "r2-test"
main = "worker.js"
compatibility_date = "2023-08-23"

[[r2_buckets]]
binding = 'MY_BUCKET' # <~ valid JavaScript variable name
bucket_name = 'jast'
name = "r2-test"
main = "worker.js"
compatibility_date = "2023-08-23"

[[r2_buckets]]
binding = 'MY_BUCKET' # <~ valid JavaScript variable name
bucket_name = 'jast'
jast 100% exists, and keep it mind I'm using workers through the admin account, so there should be no issues, but it just doesnt see the binding for some reaso
2 replies
CDCloudflare Developers
Created by murzin on 6/27/2024 in #workers-help
what's the point of workers?
Hi, I'm new to cloudflare workers. Trying to figure out what even is the point if you can't seemingly import your libraries?? Like I'm trying to add a custom watermark to my image (cuz cloudflares apo didn't work for some reason) using canvas/jimp, and I can't even do that? I think it's a skill issue but then how do I import node libraries?
109 replies
CDCloudflare Developers
Created by murzin on 6/27/2024 in #general-help
what's the point of workers?
Hi, I'm new to cloudflare workers. Trying to figure out what even is the point if you can't seemingly import your libraries?? Like I'm trying to add a custom watermark to my image (cuz cloudflares apo didn't work for some reason) using canvas/jimp, and I can't even do that? I think it's a skill issue but then how do I import node libraries?
2 replies