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

    micromashor

    05/24/2023, 6:03 PM
    oh, right
  • s

    Skye

    05/24/2023, 6:04 PM
    But nodejs compat will never have a date where it's enabled, so the types aren't exposed
  • m

    micromashor

    05/24/2023, 6:04 PM
    I see
  • s

    Skye

    05/24/2023, 6:04 PM
    You can probably just add regular node types tbh
  • m

    micromashor

    05/24/2023, 6:05 PM
    I tried that, and I get all sorts of nasty errors about redeclaring TextEncoder, TextDecoder, URLSearchParams, etc
  • m

    micromashor

    05/24/2023, 6:20 PM
    until I can figure out a solution, I can rewrite the offending module to use ArrayBuffer instead
  • m

    micromashor

    05/24/2023, 6:29 PM
    it's definitely going to be a pain because I relied on the very comfy API Buffer has for working with base64
  • m

    micromashor

    05/24/2023, 6:46 PM
    well actually... looks like there's a tsconfig.json option for this -
    skipLibCheck
  • n

    niezle.ziolko

    05/24/2023, 7:21 PM
    Hello, Can I get any help on this channel?
  • z

    zegevlier

    05/24/2023, 7:27 PM
    Depends on what you want help with! But please don't post the same question in multiple channels.
  • z

    zegevlier

    05/24/2023, 7:27 PM
    Are you talking about #1111012249922961498 ? You put that question in the right place.
  • b

    bennycondon

    05/25/2023, 1:53 AM
    Is it possible to place your /functions folder in a subdirectory? I’m trying to place my /functions folder at ./src/functions. If I run
    wrangler pages dev ./src/functions --local
    on wrangler@2.20.0: * I receive the output “No functions. Shimming…” and * Each function responds with 404s. I have no issues if I place my /functions folder at the root.
  • n

    nora

    05/25/2023, 5:10 AM
    Not at the moment, and they won't be looking into it for a few months.
  • b

    bennycondon

    05/25/2023, 7:03 AM
    No worries! Thanks for the reply.
  • h

    HardAtWork

    05/25/2023, 7:13 AM
    You can have a command to move it from
    /src/functions
    to
    /functions
    before deploy?
  • s

    stukennedy

    05/25/2023, 8:34 AM
    Here is an approach I'm using for full-stack webdev on Cloudflare Pages. It's zero-build, uses HTMX for dynamic content ... but there's no JS framework. HTMX is just a small client library that makes AJAX calls available directly from HTML tag attributes and enforces HATEOAS ... basically every REST endpoint returns HTML (and not JSON) Take a look at this bare-bones project, with the routing and middleware of CF Pages you hardly need to write any code to generate fully functioning websites https://github.com/stukennedy/cloudflare-htmx
  • s

    stukennedy

    05/25/2023, 8:41 AM
    So ... I have a question. I'm wanting to get websockets working in CloudFlare pages, and I just don't seem to be able to get it to behave. I created a
    websocket.ts
    endpoint and it almost seems to be setting up
    Copy code
    typescript
    async function handleSession(request: Request, websocket: WebSocket) {
      websocket.accept();
      websocket.addEventListener('message', async ({ data }) => {
        websocket.send('<div>Some HTML</div>');
      });
    
      websocket.addEventListener('close', async (evt) => {
        // Handle when a client closes the WebSocket connection
        console.log(evt);
      });
    }
    
    export const onRequest: PagesFunction = async ({ request }) => {
      console.log('websocket');
      const upgradeHeader = request.headers.get('Upgrade');
      if (!upgradeHeader || upgradeHeader !== 'websocket') {
        return new Response('Expected Upgrade: websocket', { status: 400 });
      }
      const [client, server] = Object.values(new WebSocketPair());
      await handleSession(request, server);
      return new Response(null, {
        status: 101,
        webSocket: client,
      });
    };
    I get
    Copy code
    websocket
    A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.
    [mf:inf] GET /websocket 500 Internal Server Error (4ms)
  • s

    stukennedy

    05/25/2023, 9:38 AM
    EDIT: I figured out the problem ... the _middleware method that I have implemented to handle the layout of my routes was being triggered on the websocket stuff. SOLUTION: I renamed my
    websocket.ts
    to
    _websocket.ts
    and added a check for any route that starts with '_' in my middleware, in those cases it just does a
    next()
    I also needed to remove
    hx-boost
    from my app, this is HTMX that does some fancy dynamic page fetching stuff. I've got it working locally now, let's see if I can get it working on Cloudflare itself.
  • m

    micromashor

    05/25/2023, 4:30 PM
    Is there a way to use an Advanced mode function that allows me to put the
    _worker.js
    file somewhere other than the output directory? I'm really not a fan of having all of the backend code exposed on the public site.
  • s

    Skye

    05/25/2023, 4:39 PM
    It doesn't actually get uploaded as a public asset for anyone to see - that's just where you put it
  • m

    micromashor

    05/25/2023, 4:40 PM
    what about other js modules it references?
  • m

    micromashor

    05/25/2023, 4:40 PM
    I assume those would get uploaded, so I'd have to use a bundler to put it all in a single file
  • s

    Skye

    05/25/2023, 4:40 PM
    Those aren't required to be in the output directory - they can be anywhere in your project
  • m

    micromashor

    05/25/2023, 4:41 PM
    ah ok
  • n

    nora

    05/25/2023, 4:41 PM
    if you're importing modules from outside your functions folder, create a build/public folder and point it as your build output folder in your pages settings
  • n

    nora

    05/25/2023, 4:42 PM
    e.g.
    Copy code
    public/
       favicon.ico
    functions/
       index.ts
    controllers/
        getSomething.ts
  • n

    nora

    05/25/2023, 4:42 PM
    whereas index.ts imports ../controllers/getSomething.ts
  • n

    nora

    05/25/2023, 4:42 PM
    (or js, same thing)
  • m

    micromashor

    05/25/2023, 4:42 PM
    so for all CF cares I could just make the
    _worker.js
    file a shim e.g.
    export * from '../api/index.js';
    right?
  • s

    Skye

    05/25/2023, 4:43 PM
    Yes
1...388389390391392Latest