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

    Isaac McFadyen | YYZ01

    04/09/2022, 4:32 PM
    Which you can definitely do, since Pages lets you have separate preview/production variables.
  • h

    Hazard

    04/09/2022, 4:32 PM
    What are these build-time variables?
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 4:32 PM
    https://developers.cloudflare.com/pages/platform/build-configuration/#environment-variables
  • h

    Hazard

    04/09/2022, 5:04 PM
    So would something like this work?
    Copy code
    ts
        // IF DEV ENV
        if (!process.env["CF_PAGES"] && !userid && email == "admin@a") {
            userid = crypto.randomUUID();
            let user: User = {
                password,
                createdAt: new Date().toISOString()
            }
            await userEmailKV.put(userid, email);
            await usersKV.put(userid, JSON.stringify(user));
        }
  • h

    Hazard

    04/09/2022, 5:05 PM
    to have sort of build-time conditional code
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 5:07 PM
    So that wouldn't work, build-time variables aren't accessible at runtime. Instead: - Open your Pages dashboard, go to Variables, and add a variable called DEVELOPMENT under Production with a value of
    false
    . - Then add one under Preview with a value of
    true
    - Then, in your code, you can check the variable called DEVELOPMENT at runtime.
  • h

    Hazard

    04/09/2022, 5:09 PM
    ok was just wondering, thank you for the thorough help
  • h

    Hazard

    04/09/2022, 5:09 PM
    where are these env vars used then?
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 5:12 PM
    In Functions you can access them on the context input to your handler (not process.env).
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 5:12 PM
    https://developers.cloudflare.com/pages/platform/functions/
  • h

    Hazard

    04/09/2022, 5:26 PM
    Are KVs for production and preview separate? And how do I access the preview btw? Right now im running the Pages functions locally.
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 5:27 PM
    The bindings are, if you bind the same KV Namespace to both Production and Preview then it will share data, otherwise you can bind different Namespaces to keep data seperate.
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 5:27 PM
    Preview variables are any time you create a commit on a non-master/main branch. How are you simulating locally?
  • h

    Hazard

    04/09/2022, 5:28 PM
    with
    npx wrangler pages dev ./svelte-kit -k users
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 5:29 PM
    Hmm... I don't think Wrangler2 pulls bindings from the dashboard so you'll need to define with the --binding flag, like:
    --binding VARIABLE=value
  • h

    Hazard

    04/09/2022, 5:29 PM
    And the KV that wrangler2 uses is only local, right?
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 5:29 PM
    Yeah.
  • h

    Hazard

    04/09/2022, 7:49 PM
    Im adding this to all my responses but im still getting CORS error and 405 on OPTIONS
    Copy code
    ts
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS,PUT,PATCH,DELETE',
    'Access-Control-Max-Age': '86400',
  • m

    mike.pete

    04/09/2022, 8:05 PM
    Does anyone know why I might be getting 522 Origin Connection Time-out with Cloudflare Images Direct Creator Upload? context: I'm getting the upload url using Functions, and returning that to the frontend, then I'm uploading images from the frontend It works for the most part, but for some reason I keep getting 522 errors intermittently **this is being tested locally with wrangler beta and cf pages/functions/images
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 9:00 PM
    What's the specific error?
  • h

    Hazard

    04/09/2022, 9:01 PM
    from the browser
    Copy code
    Access to fetch at 'http://localhost:8788/api/v1/login' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 9:02 PM
    You must either not be hitting the correct Function or are setting your headers incorrectly then. Can you do a
    curl -v https://YOUR_URL
    with your URL and see what it gives you?
  • h

    Hazard

    04/09/2022, 9:07 PM
    It gives me lots of HTML
  • h

    Hazard

    04/09/2022, 9:07 PM
    some error thing, expecting json
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 9:07 PM
    Try scrolling to the top, it'll show you headers beside >
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 9:07 PM
    Oh, then that's why.
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 9:07 PM
    Can you tell what the error page is from that HTML?
  • h

    Hazard

    04/09/2022, 9:08 PM
    lemme try with postman, its a bit unreadable
  • h

    Hazard

    04/09/2022, 9:09 PM

    https://i.imgur.com/pbDp8Eu.png▾

  • h

    Hazard

    04/09/2022, 9:09 PM
    it makes sense cause im not sending any json
1...115116117...392Latest