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

    Walshy | Pages

    03/10/2023, 1:52 AM
    You're likely hitting the asset server
  • k

    krsr

    03/10/2023, 2:00 AM
    hmm, that's interesting. maybe I need to specify a
    _routes.json
    ? there's only one function in the project currently so i was hoping it'd autogenerate correctly
  • w

    Walshy | Pages

    03/10/2023, 2:09 AM
    it will
  • w

    Walshy | Pages

    03/10/2023, 2:09 AM
    But are you sure the path is correct?
  • k

    krsr

    03/10/2023, 2:10 AM
    function is at
    /functions/waitlist.ts
    , which should make the route
    /waitlist
    , right?
  • w

    Walshy | Pages

    03/10/2023, 2:11 AM
    It should yep, what's the code?
  • k

    krsr

    03/10/2023, 2:11 AM
    https://github.com/solanus-systems/solanus.systems/blob/routes/functions/waitlist.ts
  • k

    krsr

    03/10/2023, 2:11 AM
    and the POST comes from: https://github.com/solanus-systems/solanus.systems/blob/routes/src/scripts/waitlist.ts
  • w

    Walshy | Pages

    03/10/2023, 2:17 AM
    hmm, this looks good and I can see the Function did compile
  • k

    krsr

    03/10/2023, 2:20 AM
    i feel validated 🙂
  • b

    bbong

    03/10/2023, 3:53 AM
    Hi I am trying to implement oauth flow with functions and while trying to store a jwt token in a cookie I get a
    TypeError: Can't modify immutable headers.
    but when I run it locally it works.
    t
    z
    • 3
    • 6
  • r

    Rémi

    03/10/2023, 9:06 AM
    Hi everyone, is it possible to make an HTTP request in a function in Pages? I tried plenty of things but could never get it to work and always get the message "The package "node:net" wasn't found on the file system but is built into node. Add "node_compat = true" to your wrangler.toml file to enable Node compatibility." I tried adding "node_compat = true" to the file but still get the same error during the next deployment.
  • h

    HardAtWork

    03/10/2023, 9:15 AM
    You can use the
    fetch
    API: https://developers.cloudflare.com/workers/runtime-apis/fetch
  • r

    Rémi

    03/10/2023, 9:19 AM
    Thank you so much, I was trying to install node-fetch but it was pointless, everything was already there and I spent hours on this 🤦‍♂️
  • s

    stukennedy

    03/10/2023, 10:52 AM
    new to the channel ... I've got a full-stack HTMX JAMstack webapp running on Cloudflare, been handling auth etc. I needed to write a middleware to check the auth but I've noticed sometimes the router doesn't navigate to a sub-route and keeps navigating to the parent. It was happening with one page, and now it's happening with all of them, with no code changes. (this is on local dev) I'm logging out the request URL in the middleware and it's trying to get the parent page before I do anything, so it's definitely not my code. any ideas?
    Copy code
    typescript
    import { parseToken } from '@lib/token';
    
    export const authentication: PagesFunction = async (context) => {
      console.log('middleware', context.request.url);
      if (parseToken(context.request)) {
        return await context.next();
      } else {
        console.error('redirect');
        const url = new URL(context.request.url);
        return Response.redirect(url.origin, 301);
      }
    };
    
    export const onRequest = [authentication];
    This is a
    _middleware.ts
    file inside my
    functions/dash/
    folder when I try to navigate to URL
    http://127.0.0.1:3000/dash/products
    it logs out
    middleware http://127.0.0.1:3000/dash
  • z

    ZøD?

    03/10/2023, 12:08 PM
    Hey, @stukennedy apologies, unrelated to your question but you maybe able to help me (apologies, I do not know the answer to your question!) I am trying to deploy a static website with a middleware. The static website has an output directory. Where do I put my functions directory? Is it in the output directory or the root project directory? Edit: nevermind, it is the root directory of the project. My issue was that I have a monorepo, so it's the root of the monorepo!
  • z

    ZøD?

    03/10/2023, 12:19 PM
    Can we use environment variables in our
    _middleware.js
    function? Edit: for future folks; answer is "yes". The
    onRequest
    function's
    EventContext
    has an
    env
    property that can be used (https://developers.cloudflare.com/pages/platform/functions/api-reference/#eventcontext)
  • d

    dave

    03/10/2023, 7:13 PM
    Stupid question, how do I make an endpoint like
    /myscript.js
    instead of just
    /myscript
    ?
  • u

    Unsmart | Tech debt

    03/10/2023, 7:13 PM
    probably with a file
    myscript.js.js
    🤷
  • j

    James

    03/10/2023, 7:14 PM
    yeah, this ^
  • d

    dave

    03/10/2023, 7:14 PM
    thanks! 🙂
  • d

    dave

    03/10/2023, 7:14 PM
    myscript.js.ts
    lol
  • j

    James

    03/10/2023, 7:14 PM
    cursed file extension, haha
  • k

    krsr

    03/10/2023, 7:29 PM
    I think I might know what's going on here — I'm pulling in @cloudflare/pages-plugin-mailchannels to send an email at that route, but that plugin in turn pulls in @cloudflare/pages-plugin-static-forms, which adds middleware that operates on every request looking for form submissions. so I think I need to implement my own email-sending handler instead of using the plugin's version, in order to do it without middleware
  • k

    krsr

    03/10/2023, 8:07 PM
    yeah, all I needed was
    sendEmail
    , it turns out. in case it helps anyone, the handler now looks like this: https://github.com/solanus-systems/solanus.systems/blob/main/functions/waitlist.ts
  • s

    stukennedy

    03/11/2023, 12:20 AM
    Sorry just seen this … yes the root directory. The CF docs are a bit vague on some things. Wonder if we could override the location of the functions folder and call it routes and put it in src for example.
  • d

    Deleted User

    03/11/2023, 2:39 AM
    Have you encountered any errors when using fetch in the page function?
  • d

    Deleted User

    03/11/2023, 2:40 AM
    I am using the nextjs framework
  • d

    Deleted User

    03/11/2023, 2:44 AM
    Every time I call this API, I get an error here, can someone help me? Thanks
    w
    • 2
    • 1
  • w

    William.

    03/11/2023, 7:51 PM
    Does anyone have links/tips that could explain to me why my middleware function for my angular project works in Wrangler but not on dev/prod branch ? Been having the problem for weeks now and its hard to get anyone to help me 🤓
1...356357358...392Latest