#Snippets help

12 messages · Page 1 of 1 (latest)

chilly marten
#

Only concern at the moment is exception handling in snippets, what’s the best way?

If any type of error occurs in the snippet how would I know and how should I handle it?

In workers, I’d either catch exceptions and log/report or use tail and send to sentry for example. Can also see error analytics in workers/etc but not for snippets.

vast inlet
#

(sorry, just saw your message in the channel)

cc @dense anchor MeowHeartCloudflare

For now I recommend putting a try/catch around your entire snippet - since it can have multiple subrequests now (yayy), what I've done is reserve one subrequest to logging everything from the snippet

chilly marten
#

Thank you! I’m finding that snippets are a good way to prevent unwanted traffic hitting workers and running up request count :).

vast inlet
#

for that, you can also look at waf rules and api shield, could help block invalid requests early

chilly marten
#

Do snippets not return CF-Cache-Status?

chilly marten
#

And managed transforms must run after snippets?

dense anchor
chilly marten
#

Thanks @dense anchor

With Managed Transforms -> Add security headers it appears that the x-frame-options header for example is still in the response. Should that be the case? If not, might be me.

Other question, I’m trying to set a unique request id header in a snippet then in a worker read that back and continue to pass it along to origin, however in the worker that header is not there. Am I missing something?

dense anchor
dense anchor
#

for Workers <> Snippets, i've tested it and i can access a header that is set by snippet from a worker and it is sent further down the stream to the origin

#

this is my simple snippet:

`export default {
async fetch(request) {
// Get the current timestamp
const timestamp = Date.now();

    // Convert the timestamp to hexadecimal format
    const hexTimestamp = timestamp.toString(16);

    // Clone the request and add the custom header
    const modifiedRequest = new Request(request, {
        headers: new Headers(request.headers)
    });
    modifiedRequest.headers.set("X-Hex-Timestamp", hexTimestamp);

    // Log the custom header for debugging
    console.log(`X-Hex-Timestamp: ${hexTimestamp}`);

    // Pass the modified request to the origin
    const response = await fetch(modifiedRequest);

    return response;
},

};`