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

    Walshy | Pages

    02/26/2023, 7:12 PM

    https://user-images.githubusercontent.com/40433209/192508289-425b8006-eb7c-498b-a742-7403ca77b7bc.png▾

  • a

    ajgeiss0702

    02/26/2023, 7:29 PM
    was able to get it working with orgin rules. thanks for the info 🙂
  • s

    SuchACharles

    02/26/2023, 11:41 PM
    Is it possible to connect to D1 through a NextJS api function similar to this, or do we have to use the functions dir to get this to work?
    Copy code
    js
    import type { NextRequest } from 'next/server'
    
    export const config = {
        runtime: 'edge',
    }
    
    export default async function handler(
        req: NextRequest,
        env: any
    ): Promise<Response> {
        const ps = env.MY_DB.prepare('SHOW TABLES')
        const data = await ps.first()
    
        return new Response(
            JSON.stringify({
                message: 'Hello World!',
                data: data,
            }),
            {
                status: 200,
                headers: {
                    'Content-Type': 'application/json',
                },
            }
        )
    }
  • v

    vedmant

    02/27/2023, 1:57 AM
    Hi, how I can send emails via functions, it doesn't let me use any node package like nodemailer. I tried:
    Copy code
    js
    const resp = await fetch('https://api.mailchannels.net/tx/v1/send', {
          method: 'POST',
          headers: {
            'content-type': 'application/json',
          },
          body: JSON.stringify({
            personalizations: [
              {
                to: [{ email: 'info@bla.com', name: 'bla' }],
              },
            ],
            from: {
              email: 'no-reply@bla.com',
              name: 'bla contact form',
            },
            subject: 'bla.com contact form',
            content: [
              {
                type: 'text/plain',
                value: `Name: ${output.name}\n Email: ${output.email}\n Message: ${output.message}`,
              },
            ],
          }),
        })e
    And it doesn't work, returns:
    401 Authorization Required
    So what's proper way of sending emails via functions? I have AWS SES configured, but I can't send SMTP, also looks like I can't use
    aws-sdk
  • v

    vedmant

    02/27/2023, 2:37 AM
    Looks like I'll better use AWS Lambda for this
  • p

    Pause

    02/27/2023, 3:40 AM
    Can Pages have its own Durable Objects or can it only access the objects of a Worker?
  • p

    Pause

    02/27/2023, 4:11 AM
    I make a worker with the classes defined in wrangler.toml and use the DOs from pages I think
  • h

    HardAtWork

    02/27/2023, 8:21 AM
    You cannot deploy a Durable Object class from a Pages deployment at the moment.
  • r

    rage5quid

    02/27/2023, 10:09 AM
    Does using Cloudflare Functions advanced mode imply that I have to do the bundling myself? Or is this something that can be handled by wrangler? I wish to use typescript.
  • h

    HardAtWork

    02/27/2023, 10:16 AM
    Advanced mode just means that
    wrangler
    won't automatically include the Pages router for your functions. It should still bundle your
    _worker.ts
    file.
  • r

    rage5quid

    02/27/2023, 10:19 AM
    If I try to run it locally it won't generate a
    _worker.js
    file from my
    _worker.ts
    file. Or is this something that still has to be implemented in the
    wranger pages dev
    module? Or is there a way to make wrangler generate the js file locally?
  • r

    rage5quid

    02/27/2023, 10:20 AM
    No functions. Shimming... is the message
  • h

    HardAtWork

    02/27/2023, 10:20 AM
    If it is working correctly, it shouldn't make any changes to any files in your directory. It should just bundle and upload your Functions directly.
  • r

    rage5quid

    02/27/2023, 10:22 AM
    What about running it locally using
    wrangler pages dev
    ? When the _worker.js file does not exist?
  • h

    HardAtWork

    02/27/2023, 10:24 AM
    It should transpile/run it directly. I believe the builder(
    esbuild
    ) has a function that allows it to build directly to an object in memory, allowing it to run faster, because it doesn't have to write to a
    worker.js
    file, and then immediately read it again.
  • r

    rage5quid

    02/27/2023, 10:26 AM
    Hmm, that's not whats happening. So that would be a bug?
  • r

    rage5quid

    02/27/2023, 10:28 AM
    I can try to publish it to see what will happen, but for local development I probably need to do some custom esbuild stuff for now
  • h

    HardAtWork

    02/27/2023, 10:29 AM
    What do you mean?
  • r

    rage5quid

    02/27/2023, 10:33 AM
    I have this project with a
    src
    folder with a
    _worker.ts
    with a fetch handler. Together with a simple tsconfig.json with the workers-types. If I run
    wranger pages dev src
    the worker does not run, all I get is 404's. When I compile the _worker.ts to _worker.js using tsc then it works as expected. So the transpilation does not happen automatically. Or did you mean that I have to manage the esbuild myself?
  • h

    HardAtWork

    02/27/2023, 10:35 AM
    Oh, guess I was wrong, you do need to compile it first... My bad!
  • h

    HardAtWork

    02/27/2023, 10:35 AM
    I usually use the regular Functions router, so I'm not as used to using Advanced Mode as I probably should be...
  • r

    rage5quid

    02/27/2023, 10:36 AM
    No problem, you can't know everything 🙂
  • r

    rage5quid

    02/27/2023, 10:37 AM
    Is the default esbuild configuration public, so I could use that?
  • h

    HardAtWork

    02/27/2023, 10:38 AM
    I just use
    esbuild _worker.ts --bundle --minify --target=esnext --format=esm --outfile=_worker.js
    , but I can try to dig out the "official" one for you...
  • r

    rage5quid

    02/27/2023, 10:38 AM
    That would be great, thank you!
  • h

    HardAtWork

    02/27/2023, 10:41 AM
    Looks like it is basically that. A little bit hard to see, since the
    wrangler
    source is pretty complex, but the above has always worked for me.
  • l

    Larry

    02/27/2023, 1:10 PM
    Correct. Currently, you have to think of your DOs as another web service that you have to deploy separately although with the right setup, you get hot reload in dev mode so it doesn't feel that way while developing. I'm constantly forgetting to re-publish my DOs when I make a change to my Pages code that relies upon changes to my DOs so I'm looking into ways to deploy them all at the same time, but it's not trivial and means I have to move away from the automatic CI, which makes me sad because it's so convenient.
  • b

    Better James

    02/27/2023, 4:15 PM
    Hey there, anyone have any wisdom on this matter? I thought we could have separate functions files for different routes, but wrangler dev locally appears to be trying to compile all my functions into one worker file and is throwing
    Cannot redefine property: ...
    , because each function route is requiring another file that defines the same property on globalThis. My understanding of functions is that each file is its own thing, i.e. my function routes shouldn't be sharing logic between the two of them. Is there a way to not have wrangler compile all pages functions into the same file? I thought you were supposed to be able to use different files for different routes.
  • k

    kian

    02/27/2023, 4:23 PM
    They're different files when you're writing them, sure, but at the end of the day they're all bundled together like Workers
  • k

    kian

    02/27/2023, 4:23 PM
    What are they writing onto
    globalThis
    ?
1...352353354...392Latest