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

    kian

    03/22/2023, 9:46 PM
    But what is
    app
    - that's not a normal Worker, it's a framework.
  • k

    Keizer

    03/22/2023, 9:46 PM
    const app = new Hono();
  • k

    Keizer

    03/22/2023, 9:48 PM
    I had my app working as a raw worker without Hono using raw fetch(req, env) and that brought in the scope from my .toml
  • k

    kian

    03/22/2023, 9:50 PM
    What's the full code for that Worker?
  • k

    kian

    03/22/2023, 9:50 PM
    Other than the
    getTpls
    stuff and whatnot
  • g

    GeorgeTailor

    03/22/2023, 9:50 PM
    too bad that even if you manually create a stream, the error is the same, for some reason the following will not work
    Copy code
    const stream = new ReadableStream({
        start(controller) {
            controller.enqueue('<div></div>');
        }
    });
    new HTMLRewriter()
        .on('div', { element: (element) => {
            element.setAttribute('id', 'test')
        },
    }).transform(stream);
  • k

    kian

    03/22/2023, 9:50 PM
    I suspect it's calling
    arg.body.pipeTo()
    rather than
    arg.pipeTo()
  • k

    kian

    03/22/2023, 9:50 PM
    and
    body
    is only on a request/response
  • k

    Keizer

    03/22/2023, 9:51 PM
    Copy code
    import { Hono } from "hono";
    
    const Mustache = require('mustache');
    
    const app = new Hono();
    
    async function getTpls (env) {
        var object1 = await env.MY_BUCKET.get("templates/header.mustache");
        var object2 = await env.MY_BUCKET.get("templates/footer.mustache");
        var object3 = await env.MY_BUCKET.get("templates/index.mustache");
        var object4 = await env.MY_BUCKET.get("templates/sidebar.mustache");
    
        var newObj = {
            header: await object1.text(),
            footer: await object2.text(),
            index: await object3.text(),
            sidebar: await object4.text(),
            splash: ['party.png', 'asmb1337.png', 'entertainment.png']
        };
    
        return newObj;
    }
    
    app.get("/", async (c, next) => {
        var vdata = await getTpls(c.env);
        console.log('ENV: '+c.env);
        console.log('VDATA: '+vdata);
    
        return c.text(Mustache.render(vdata.index, vdata, vdata));
    });
    
    export default app;
  • k

    Keizer

    03/22/2023, 9:52 PM
    Routes work until I try to use env
  • k

    kian

    03/22/2023, 9:52 PM
    and what's the error you get?
  • g

    GeorgeTailor

    03/22/2023, 9:52 PM
    so, you kind of are forced to use some kind of a KV to store HTML rather than pulling it locally from the bundle
  • k

    Keizer

    03/22/2023, 9:54 PM
    Looks like it's dieing (on the _worker.js build output) on the
    Copy code
    await object.text()
    from the R2 results saying
    Copy code
    Trace: TypeError: Cannot read properties of null (reading 'text')
  • k

    Keizer

    03/22/2023, 9:54 PM
    That function was working when I was using raw and bringing in the env through fetch()
  • k

    Keizer

    03/22/2023, 9:57 PM
    I was digging using R2 for storage, when I change my templates in R2 the worker already has the updates which is super sweet
  • k

    Keizer

    03/22/2023, 9:57 PM
    Now I am transfering to "Pages" so I can also use the static hosting option
  • k

    Keizer

    03/22/2023, 10:13 PM
    Was able to console.log(c.env) and it has data but nothing for MY_BUCKET so that is the issue
    Copy code
    ENV: {"MY_BUCKET":{},"__STATIC_CONTENT":{},"__STATIC_CONTENT_MANIFEST":"{\"assets/css/foundation.css
  • k

    Keizer

    03/22/2023, 10:14 PM
    Too bad their example is typescript. Need to bind into Hono constructor still but R2Bucket wasn''t defined >_<
  • k

    kian

    03/22/2023, 10:14 PM
    That's not the issue - bindings have never stringified
  • k

    Keizer

    03/22/2023, 10:14 PM
    Ok
  • k

    kian

    03/22/2023, 10:14 PM
    As long as you see
    MY_BUCKET
    , it's there
  • k

    kian

    03/22/2023, 10:14 PM
    text()
    only exists if the object exists though - are you sure all of those paths have an object?
  • k

    Keizer

    03/22/2023, 10:15 PM
    Yes these were working before in a normal standalone worker
  • k

    Keizer

    03/22/2023, 10:15 PM
    (before trying Hano when I was using fetch() and Response()
  • k

    Keizer

    03/22/2023, 10:16 PM
    console logging on my objects to make sure data is coming back
  • k

    Keizer

    03/22/2023, 10:17 PM
    Ok the R2 .get() is coming back null >_<
  • k

    Keizer

    03/22/2023, 10:18 PM
    Same exact code I had pulling the templates before wtf
  • k

    Keizer

    03/22/2023, 10:19 PM
    When I go do test dev build wrangler output I have it bound in the terminal:
    Copy code
    Your worker has access to the following bindings:
    - R2 Buckets:
      - MY_BUCKET: hnwatemplates
    ⎔ Starting a local server...
  • k

    Keizer

    03/22/2023, 10:20 PM
    Hmmm, is it better if I just make a worker and include it in a service binding on Pages?
  • k

    Keizer

    03/22/2023, 10:20 PM
    Dunno if that would help since I want to use Hono for better coverage. Not sure if to blame Hono or "Pages" lol
1...360361362...392Latest