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

    James

    02/22/2023, 7:23 PM
    Another option, if all of your static assets like images/css, etc. are in a single folder like
    assets
    , you could exclude that folder from running any functions using `_routes`: https://developers.cloudflare.com/pages/platform/functions/routing/#functions-invocation-routes
  • n

    Noodles

    02/22/2023, 8:43 PM
    Hey all! I am just making a simple landing page for a school simulation project, but I can't seem to get functions to work/build correctly. In the building of the pages it shows:
    Copy code
    Found Functions directory at /functions. Uploading.
    12:36:37.272    
    12:36:37.386    ✘ [ERROR] No routes found when building Functions directory: /opt/buildhome/repo/functions
    Warning: Wrangler did not find routes when building functions. Skipping.
    12:36:37.400    Found _routes.json in output directory. Uploading.
    12:36:37.423    Validating asset output directory
    I have attached the KV namespace to the project for the function, but it seems like it just won't build the function at all. Functions folder is created and _routes.json is present.
  • p

    Pause

    02/22/2023, 8:44 PM
    unfortunately not; images/pdfs are among the html files
  • p

    Pause

    02/22/2023, 8:44 PM
    I'll try the content-type thing
  • p

    Pause

    02/22/2023, 9:40 PM
    @James content-type seems to work fine. no issues or performance impact
  • p

    Pause

    02/22/2023, 9:42 PM
    using
    indexOf
    to see if
    text/html
    is in there
  • p

    Pause

    02/22/2023, 9:42 PM
    although if the response is just
    text/html
    I could probably do an equality comparison
  • s

    Skye

    02/22/2023, 9:42 PM
    it'll likely include a charset too - doing an inlcudes check is what I'd do
  • k

    kian

    02/22/2023, 9:42 PM
    it sometimes has a charset
  • k

    kian

    02/22/2023, 9:43 PM
    that
  • p

    Pause

    02/22/2023, 9:44 PM
    okay switched to includes. works too
  • j

    João Castro

    02/23/2023, 6:52 AM
    Is there some way to write a middleware that only applies to pages routes? Im using HTMLRewriter, and it would be great if i could avoid triggering the middleware from static assets and api routes.
  • h

    HardAtWork

    02/23/2023, 8:43 AM
    Doesn't middleware only run on Function routes that already exist anyway?
  • l

    Lor

    02/23/2023, 9:10 AM
    Not sure if this is for #910978223968518144 or #992060581832032316 I'm exerpiencing a weird error but i'm not quite sure what is wrong. I might just not be good enough to understand what the error is saying. It only seems to break when I use prepared statements (the error happens on the bind() part) Error:
    Copy code
    js
    Error: Type 'function' not supported for value 'function(block) {
          return block(this);
        }'
    Code
    Copy code
    ts
    let prepare = this.DB.prepare("INSERT INTO users (email, password, salt, id) VALUES (?, ?, ?, ?)");
    let command = prepare.bind(email, password, salt, id);
  • s

    Skye

    02/23/2023, 9:32 AM
    That error seems to hint that one of the variables you're passing in your .bind() call is a function
  • l

    Lor

    02/23/2023, 9:33 AM
    all of them are strings, i promise
  • s

    Skye

    02/23/2023, 9:36 AM
    All that D1's code is doing to get that error is a typeof check
  • s

    Skye

    02/23/2023, 9:37 AM
    If you want to be sure, you can .toString() all of your variables
  • l

    Lor

    02/23/2023, 9:49 AM
    Just tried it now, still the same problem.
  • s

    Skye

    02/23/2023, 9:51 AM
    That's the only place in the entire codebase that throws that error 🤔
  • s

    Skye

    02/23/2023, 9:51 AM
    Could you share the entire code so I can have a look?
  • l

    Lor

    02/23/2023, 9:59 AM
    These should be the relevant ones
    Copy code
    ts
    export interface Env {
        DATABASE: D1Database;
        PEPPER: string;
    }
    Copy code
    ts
    export class UserDB {
        DB: D1Database;
    
        constructor(db: D1Database) {
            this.DB = db;
        }
    
        async createUser(email: string, password: string, salt: string, id: string): Promise<User> {
            console.log("preparing command")
            let prepare = this.DB.prepare("INSERT INTO users (email, password, salt, id) VALUES (?, ?, ?, ?)");
            let command = prepare.bind(email.toString(), password.toString(), salt.toString(), id.toString()); // Throws here
            await command.run();
            return await this.getUser(id)
        }
    }
    Copy code
    ts
    export const onRequestPost: PagesFunction<Env> = async (context) => {
        let {email, password, challenge} = await context.request.json<{
            email: string, password: string, challenge: string
        }>();
        
        // Validation here
    
        let db = new UserDB(context.env.DATABASE)
        
        let salt: string = Buffer.from(crypto.getRandomValues(new Uint8Array(16))).toString('base64');
        let id: string = uuidv4();
        let hashedPassword: string = SHA512(salt + password + context.env.PEPPER).toString(CryptoJS.enc.Hex);
        let result = await db.createUser(email, hashedPassword, salt, id); // Throws here
    }
  • s

    Skye

    02/23/2023, 10:00 AM
    Those all are definitely strings.. how odd 🤔
  • s

    Skye

    02/23/2023, 10:01 AM
    Is it still the same
    Copy code
    function(block) {
          return block(this);
        }
    in the message, even with the .toString()?
  • l

    Lor

    02/23/2023, 10:01 AM
    Yes
  • s

    Skye

    02/23/2023, 10:01 AM
    Is this deployed code or local testing?
  • l

    Lor

    02/23/2023, 10:02 AM
    deployed in pages, yes
  • l

    Lor

    02/23/2023, 10:02 AM
    This is a function
  • s

    Skye

    02/23/2023, 10:02 AM
    Alright, can you make an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose
  • l

    Lor

    02/23/2023, 10:08 AM
    Will do
1...349350351...392Latest