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

    Subh

    12/14/2021, 11:27 AM
    In function folder
    await fetch(NEXT_PUBLIC_NHOST_GRAPHQL_API
    this will not work if defined in Production environment variables? Asking because
    NEXT_PUBLIC_NHOST_GRAPHQL_API is not defined
    got this error in production functions.
  • g

    geelen

    12/14/2021, 12:28 PM
    is it defined as an ENV var in both environments?
  • g

    Greg Brimble | Cloudflare Pages

    12/14/2021, 12:57 PM
    Probably should be
    env.NEXT_PUBLIC_NHOST_GRAPHQL_API
  • j

    JustinNoel

    12/14/2021, 1:15 PM
    I'm getting my butt kicked by TypeScript. I thought this was working fine a few days ago but now TS is throwing errors for this:
    export const onRequestGet: PagesFunction<Env, "params", Data> = async ({ env, params }) => { ... }
    I am now getting
    Cannot find name 'PagesFunction'.ts(2304)
    However, I've setup my
    tsconfing.json
    as follows per the
    @cloudflare/workers-types
    instructions:
    Copy code
    {
      "compilerOptions": {
        "alwaysStrict": true,
        "esModuleInterop": true,
        "jsx": "react-jsx",
        "jsxImportSource": "preact",
        "moduleResolution": "node",
        "noImplicitAny": true,
        "noUncheckedIndexedAccess": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "resolveJsonModule": true,
        "strict": true,
        "target": "ES2020",
        "module": "CommonJS",
        "lib": [
          "ES2020"
        ],
        "types": [
          "@cloudflare/workers-types"
        ]
      },
      "include": [
        "dev/**/*",
        "src/**/*",
        "functions/**/*"
      ],
      "exclude": [
        "node_modules"
      ]
    }
    It's now just Visual Studio Code either. If I run
    yarn tsc
    , I get the following:
    Copy code
    functions/api/images/[id].ts:9:28 - error TS2304: Cannot find name 'PagesFunction'.
    
    9 export const onRequestGet: PagesFunction<
                                 ~~~~~~~~~~~~~
    
    functions/api/images/[id].ts:13:14 - error TS7031: Binding element 'env' implicitly has an 'any' type.
    
    13 > = async ({ env, params }) => {
                    ~~~
    
    functions/api/images/[id].ts:13:19 - error TS7031: Binding element 'params' implicitly has an 'any' type.
    
    13 > = async ({ env, params }) => {
                         ~~~~~~
    Anyone have any ideas?
  • g

    geelen

    12/14/2021, 1:44 PM
    what version of workers-types is in your lockfile?
  • j

    JustinNoel

    12/14/2021, 2:14 PM
    Thanks for the reply. That was the culprit! I had two different ones.
    Copy code
    "@cloudflare/workers-types@^2.2.2":
      version "2.2.2"
      resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-2.2.2.tgz#1bced16bba801d7af987da835467545bb5cc7ac6"
      integrity sha512-kaMn2rueJ0PL1TYVGknTCh0X0x0d9G+FNXAFep7/4uqecEZoQb/63o6rOmMuiqI09zLuHV6xhKRXinokV/MY9A==
    
    "@cloudflare/workers-types@^3.2.0":
      version "3.2.0"
      resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-3.2.0.tgz#df300466f5f8a03b205bdd533990017b0538496e"
      integrity sha512-y0+f7QeB5/fMMdU0wSwvBB18yE9kAD2s7Wben8a4uI4f/EJyE+eJrai5QO52Pq8EmWP0vRpKqZh0qU857WhY2A==
    Now, I realize why this happened. I installed Miniflare as a dependency so I could load some fake data into my KV namespace for developing locally. Miniflare must have added the old version of
    @cloudflare/workers-types
    . Removing Miniflare (and the old types) did the trick. So, I'll have to use Miniflare a bit differently. Thanks so much!
  • r

    robinio

    12/14/2021, 3:40 PM
    Last week we requested a raise of the 100.000 requests/day limit, as this would potentially block us from releasing a prod version on CF pages. We are in no particular hurry as we still have to replace a few of our old PHP functions before are able to switch to the JAM stack, but I am guessing early January we should be ready for prod. Since this is a blocker, I'd like to resolve this early. If we are not eligible that's perfectly fine as well, but then we would probably need a different solution until pages-functions is out of BETA. So I am wondering, how long does it usually take to get an answer?
  • n

    Nevi | Pages

    12/14/2021, 5:23 PM
    Hey there - private messaging you
  • j

    jschlesser

    12/14/2021, 9:04 PM
    Is there any supported way to use some package management with pages-functions to support installing and managing 3rd party libraries?
  • p

    pxkq

    12/14/2021, 9:31 PM
    Using middleware (https://developers.cloudflare.com/pages/platform/functions#middleware-routing)
    Copy code
    ├── functions
    │   ├── _middleware.ts        # Applies to all routes
    │   ├── ...
    │   └── todos
    │       ├── _middleware.ts    # Adds extra middleware to /todos/**
    │       ├── ...
    └── ...
    I assumed the order of execution was top level first, deeper level next. I have an errorHandler on the top level, but it's executing first the inner middleware. Any idea why could that be? Using wrangler pages dev (alpha)
  • j

    jschlesser

    12/14/2021, 9:49 PM
    im using wrangler pages dev (alpha) and my middleware is executing top down, as you expect. One thing to check is that you are using the
    return await next()
    in both spots. If your top level isn't awaiting next, it might cause the behavior you are seeing.
  • p

    pxkq

    12/14/2021, 9:56 PM
    Weird. I do have
    return await next()
    in both. But I have them as in the example using arrays:
    Copy code
    //top level
    export const onRequest = [
      errorHandler,
    ]
    Copy code
    //next level
    export const onRequest = [
      secondMiddleware,
    ]
    Maybe that affects the awating? I'll try move it around
  • p

    pxkq

    12/14/2021, 9:59 PM
    Nah, that's not it.
  • j

    jschlesser

    12/14/2021, 10:00 PM
    Mine setup is pretty simple, ill post my middlewares here in full
  • j

    jschlesser

    12/14/2021, 10:01 PM
    Top Level
    Copy code
    import {setup as kvsetup } from './lib/global/KV'
    
    const errorHandler = async ({ next }) => {
        try {
            console.log('before errorHandler')
            return await next()
        } catch (err) {
            return new Response(`${err.message}\n${err.stack}`, { status: 500 })
        }
    }
    
    const setup = async({env, next}) => {
        kvsetup(env)
        return await next()
    }
    
    export const onRequest = [
        errorHandler,
        setup
    ]
    Level Down
    Copy code
    const consoleHello = async ({ next }) => {
        try {
            console.log('hello from api middleware')
            return await next()
        } catch (err) {
            return new Response(`${err.message}\n${err.stack}`, { status: 500 })
        }
    }
    
    export const onRequest = [
        consoleHello
    ]
  • j

    jschlesser

    12/14/2021, 10:02 PM
    My console prints out the top level console log of the top level first and the lower level second
  • p

    pxkq

    12/14/2021, 10:06 PM
    I just copy pasted your example and I get:
    Copy code
    hello from api middleware
    before errorHandler
    actual endpoint
  • p

    pxkq

    12/14/2021, 10:08 PM
    Just to be clear:
    Copy code
    ├── functions
    │   ├── _middleware.ts        # THIS ONE IS TOP LEVEL (before errorHanlder)
    │   ├── ...
    │   └── todos
    │       ├── _middleware.ts    # THIS IS DEEP LEVEL (hello from api middleware)
    │       ├── ...
    └── ...
  • j

    jschlesser

    12/14/2021, 10:08 PM
    Huh, here is mine after i added a console.log to the actual endpoint
    Copy code
    before errorHandler
    hello from api middleware
    actual endpoint
  • j

    jschlesser

    12/14/2021, 10:09 PM
    Yes, that is how I have it set up
  • p

    pxkq

    12/14/2021, 10:09 PM
    😖
  • p

    pxkq

    12/14/2021, 10:09 PM
    Ni idea then. I'll update the wrangler to the latest alpha (I did it yesterday)
  • j

    jschlesser

    12/14/2021, 10:09 PM
    when did you install wrangler@Alpha ?
  • j

    jschlesser

    12/14/2021, 10:10 PM
    Yeah, i installed yesterday too
  • p

    pxkq

    12/14/2021, 10:11 PM
    Just updated it (0.0.0-ae4fe86), same result 😦
  • j

    jschlesser

    12/14/2021, 10:11 PM
    I dont think it should matter but my files are .js not .ts
  • p

    pxkq

    12/14/2021, 10:12 PM
    I wouldn't expect that to make a difference either
  • p

    pxkq

    12/14/2021, 10:17 PM
    I think I found it. Seems like a bug in wrangler, could you try it? I have in a second level, so not like I was saying:
    Copy code
    ├── functions
    │   ├── _middleware.ts        # THIS ONE IS TOP LEVEL (before errorHanlder)
    │   ├── ...
    │   └── todos
    │       ├── _middleware.ts    # THIS IS DEEP LEVEL (hello from api middleware)
    │       ├── ...
    └── ...
    But as:
    Copy code
    ├── functions
    |    |-- api/
    │       ├── _middleware.ts        # THIS ONE IS TOP LEVEL (before errorHanlder)
    │       ├── ...
    │       └── todos
    │           ├── _middleware.ts    # THIS IS DEEP LEVEL (hello from api middleware)
    │           ├── ...
    └── ...
    That way middlewares get inverted
  • j

    jschlesser

    12/14/2021, 10:24 PM
    Now i get:
    Copy code
    hello from api middleware
    before errorHandler
    actual endpoint, hey
  • j

    jschlesser

    12/14/2021, 10:24 PM
    Just like you
1...424344...392Latest