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

    Yudai Nakata

    01/06/2023, 2:49 PM
    Hi I'm trying to build an app with Pages (hopefully with R2 and D1 as well), but am struggling with setting up Functions properly. The app is an Vite-powered SPA, but all the requests matching to Functions routing return the SPA instead of an API response defined in
    functions
    directory. Below are the steps to reproduce my problem:
    Copy code
    npm init vite@latest repro -- --template react
    cd repro
    npm install
    npm install -D wrangler
    mkdir -p functions/api
    echo "export const onRequest = c => new Response('Hello world!');" > functions/api/hello.js
    npx wrangler pages dev -- npx vite
    Then, running
    curl http://localhost:5173/api/hello
    returns the scaffolded app instead of
    Hello world!
    . I have consulted the documentation at https://developers.cloudflare.com/pages/platform/functions/ especially the Routing section, but found no clue. I'd appreciate it if anyone could point out what I'm doing wrong.
  • k

    kian

    01/06/2023, 3:51 PM
    5173 is Vite - you need to open Wrangler's port which is likely 8788, but otherwise scroll down in the terminal output.
  • k

    kian

    01/06/2023, 3:53 PM
  • i

    Isaac McFadyen | YYZ01

    01/06/2023, 3:55 PM
    Vite
  • y

    Yudai Nakata

    01/06/2023, 4:36 PM
    Thanks, 8788 definitely worked! Other two questions occurred to me though; - Where can I find the port number for wrangler? The initial terminal output didn't show it. - How can Pages call Functions API? The following patch in
    src/App.jsx
    logs the SPA in DevTools because it uses the Vite's port. However, what I want to call here is obviously Functions'
    /api/hello
    endpoint.
    Copy code
    diff
    -         <button onClick={() => setCount((count) => count + 1)}>
    +         <button onClick={() => {
    +           fetch('/api/hello')
    +             .then(r=>r.text())
    +             .then(t=>console.log(t))
    +           return setCount((count) => count + 1)
    +         }}>
  • q

    Quacksire

    01/07/2023, 12:40 AM
    I'm getting a
    Script startup exceeded CPU time limit
    with all of my page functions... but I don't think that a function that retunrs another fecth should cause it.
    Copy code
    js
    export async function onRequestGet(context) {
        try {
            if (!context.request.headers.get('X-SL-User')) {
                return new Response('Not logged in', {status: 401})
            }
            let token = context.request.headers.get('X-SL-User').split(':')[0]
            let uid = context.request.headers.get('X-SL-User').split(':')[1]
            token = decodeURI(token)
            let response = await fetch(`https://subdomain.schoolloop.com/mapi/assignments?studentID=${uid}`,
                {
                    headers: {
                        "Authorization": `Basic ${token}`
                    }
                }
            )
            response = await response.json()
            return new Response(
                JSON.stringify(response),
                {
                    status: 200,
                    headers: {
                        'content-type': 'application/json',
                        "cache-control": `s-maxage=1200, stale-while-revalidate=600`
                    },
                }
            )
        } catch (e) {
            return new Response(`${e} | token: ${token} | uid: ${uid}`, {status: 500})
        }
    }
  • j

    James

    01/07/2023, 12:41 AM
    Are you importing anything? Is that your entire Function? Any middleware that runs before?
  • q

    Quacksire

    01/07/2023, 12:42 AM
    this is the entire function
  • q

    Quacksire

    01/07/2023, 12:42 AM
    I've got 3 others that are exactly the same, just a different url
  • j

    James

    01/07/2023, 12:42 AM
    Are you using any kind of framework to build this? That function itself looks totally reasonable on the surface
  • q

    Quacksire

    01/07/2023, 12:43 AM
    I'm using Nextjs but this is a seperate functions/ dir
  • q

    Quacksire

    01/07/2023, 12:43 AM
    Edge Runtime
  • j

    James

    01/07/2023, 12:43 AM
    And
    next-on-pages
    I imagine?
  • q

    Quacksire

    01/07/2023, 12:43 AM
    yes
  • q

    Quacksire

    01/07/2023, 12:43 AM
    hol up
  • q

    Quacksire

    01/07/2023, 12:44 AM
    nvm
  • q

    Quacksire

    01/07/2023, 12:44 AM
    same thing
  • q

    Quacksire

    01/07/2023, 12:44 AM
    removed a console.log I had, but it doesn't seem to do the trick
  • j

    James

    01/07/2023, 12:45 AM
    I don't really have a good suggestion for ya unfortunately. This is a common issue with
    next-on-pages
    as the bundle size grows. https://github.com/cloudflare/next-on-pages/issues/47. https://github.com/cloudflare/next-on-pages/issues/2. etc.
  • j

    James

    01/07/2023, 12:45 AM
    I can only really recommend creating an issue on the repo with a reproduction.
    next-on-pages
    itself is in a state right now that I couldn't personally recommend it due to lack of maintenance and support, and would encourage you to consider an alternate solution like Netlify or Vercel for your project if you need a quick resolution. 😦
  • q

    Quacksire

    01/07/2023, 12:46 AM
    Is there a way to handle multiple get requests in a single page function file?
  • q

    Quacksire

    01/07/2023, 12:46 AM
    I could also just move it to a worker
  • j

    James

    01/07/2023, 12:46 AM
    Multiple get requests? What do you mean? To different paths?
  • q

    Quacksire

    01/07/2023, 12:48 AM
    Right now I have page functions files with single get requests to their path, would there be a way to consolidate them into a single file instead, or that not recommended?
  • w

    Walshy | Pages

    01/07/2023, 12:49 AM
    you can do
    [path].js
    which would fire for
    /_sl/*
  • j

    James

    01/07/2023, 12:49 AM
    You could do something like
    [slug].js
    but that of course would match all slugs under
    _sl/
  • j

    James

    01/07/2023, 12:49 AM
    otherwise no, you couldn't really listen for just those 4
  • q

    Quacksire

    01/07/2023, 12:50 AM
    now, would that fix the original issue? lol
  • j

    James

    01/07/2023, 12:50 AM
    And I honestly doubt that's your issue here either. The size of your next project due to next-on-pages is likely just your issue here.
  • j

    James

    01/07/2023, 12:50 AM
    Of which there's very little you can do unfortunately
1...326327328...392Latest