https://discord.cloudflare.com logo
Join Discord
Powered by
# functions
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 7:50 PM
    Middleware still run before static assets.
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 7:51 PM
    So you'll need to do something like
    Copy code
    ts
    let response = await env.ASSETS.fetch(request)
    if (response.status >= 200 && response.status < 400) return response
    
    return new Response("My dynamic Functions response")
  • c

    Cоlе

    01/13/2022, 7:52 PM
    would something like this work?
    Copy code
    js
    export const onRequestGet = async ({ request, env, next, data }) => {
        let pageData = await next();
    
        if (pageData.status === 404) {
            // Do stuff
        } else {
            return new pageData;
        }
    };
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 7:52 PM
    Yup, pretty much the same sort of thing as what I posted 🙂
  • c

    Cоlе

    01/13/2022, 7:53 PM
    I see. I'm not too familiar with env.ASSETS
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 7:53 PM
    next()
    is probably nicer than
    env.ASSETS.fetch
    in case you've got things further down the handler chain
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 7:53 PM
    env.ASSETS.fetch
    is just a shortcut right to the bottom
  • b

    bsam75

    01/13/2022, 7:54 PM
    Thanks for both of your help, really appreciate it
  • c

    Cоlе

    01/13/2022, 7:55 PM
    off-topic question, could you use to env.ASSETS.fetch a separate static assets? like if I trying to build a page or serve a custom asset, could I grab
    public/img/something.png
    ?
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 7:55 PM
    Yup.
  • c

    Cоlе

    01/13/2022, 7:56 PM
    neat! I should probably start doing that
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 7:56 PM
    We're working out some intricacies with this right now (https://github.com/cloudflare/wrangler2/issues/232 and https://github.com/cloudflare/wrangler2/issues/165)
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 7:56 PM
    But we'll get them worked out soon
  • e

    Epailes

    01/13/2022, 8:37 PM
    I'm assuming it's on an unbound worker then rather than bundled worker?
  • c

    Cоlе

    01/13/2022, 9:09 PM
    @User it's bundled
  • c

    Cоlе

    01/13/2022, 9:10 PM
    I'm doing another build with bigger image + more complexity & color blending. I'll post back when I get it going
  • w

    Warlando

    01/13/2022, 9:34 PM
    I'm currently building a SvelteKit application and when developing locally the endpoints run Nodejs and therefore I can use packages like
    crypto
    , but once I build it, because Cloudflare workers have a different Javascript environment it fails and returns the following errror:
    Could not resolve "crypto" (use "platform: 'node'" when building for node)
  • c

    Cоlе

    01/13/2022, 9:35 PM
    for crypto, you're gonna want to use https://github.com/panva/jose
  • c

    Cоlе

    01/13/2022, 9:36 PM
    essentially, you can use most packages that follow ES6 (think
    import {} from "..."
    rather than
    require("...")
    ), and don't rely on
    document
  • w

    Warlando

    01/13/2022, 9:37 PM
    thanks for the answer @User but it seems this package does not have SHA512 hashing
  • w

    Warlando

    01/13/2022, 9:37 PM
    and that's what I need the most
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 9:38 PM
    https://developers.cloudflare.com/workers/runtime-apis/web-crypto#supported-algorithms
  • w

    Warlando

    01/13/2022, 9:38 PM
    this fails on Sveltekit in dev mode
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 9:38 PM
    Copy code
    ts
    const myText = new TextEncoder().encode("Hello world!")
    
    const myDigest = await crypto.subtle.digest(
      {
        name: "SHA-512",
      },
      myText, // The data you want to hash as an ArrayBuffer
    )
    
    console.log(new Uint8Array(myDigest))
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 9:39 PM
    dev mode? You mean with wrangler?
  • w

    Warlando

    01/13/2022, 9:39 PM
    no, I don't need Wrangler because I'm using the cloudflare-adapter
  • w

    Warlando

    01/13/2022, 9:41 PM
    when I use this
    crypto
    lib it returns the following error
  • w

    Warlando

    01/13/2022, 9:42 PM
    crypto is not defined
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 9:42 PM
    Yeah, you'll need to use wrangler.
  • g

    Greg Brimble | Cloudflare Pages

    01/13/2022, 9:43 PM
    I imagine SvelteKit's local runner thing is using Node, which won't have Web Crypto available
1...596061...392Latest