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

    pencilflip

    02/01/2022, 12:26 AM
    I tried just copying the HTML file as a string and returning that
  • p

    pencilflip

    02/01/2022, 12:26 AM
    but don't think that works... browser is having trouble loading the JS
  • c

    Cоlе

    02/01/2022, 1:37 AM
    @User I did the exact thing you're trying to do, last week. lemme put together an example
  • c

    Cоlе

    02/01/2022, 1:45 AM
    here's a half-pseudocode example:
    Copy code
    ts
    export const onRequestGet: PagesFunction<{}> = async ({ request, env, next, data }) => {
        // Get api data and page at same time
        let apiData = fetch(`INSERT API URL/whatever`).then(res => res.json());
        let pageData = await next();
        let [api, page] = await Promise.all([apiData, pageData]);
    
        return new HTMLRewriter().on("head", new HeadHandler(api, env)).transform(page);
    };
    
    class HeadHandler {
        private api: any;
    
        constructor(api: any) {
            this.api = api;
        }
    
        comments(comment: Comment) {
            if (comment.text.trim() !== "INJECT_META_TAGS") { return; }
    
            // Append twitter meta tags
            comment.after(`<meta property="twitter:card" content="summary">`, { html: true });
            comment.after(`<meta property="twitter:title" content="${this.api.title}">`, { html: true });
            comment.after(`<meta property="twitter:description" content="${this.api.description}">`, { html: true });
        }
    }
    Then, I just put
    <!-- INJECT_META_TAGS -->
    in the head of my page, and the HTMLRewriter will insert the tags after it
  • c

    Cоlе

    02/01/2022, 1:46 AM
    you can substitute out the
    await next()
    with however you're getting the HTML for the page, if you're not just intercepting the request/using middleware.
  • c

    Cоlе

    02/01/2022, 1:47 AM
    https://developers.cloudflare.com/workers/runtime-apis/html-rewriter
  • c

    Cоlе

    02/01/2022, 2:11 AM
    *if you do want to intercept, then name the file
    index.ts
    , then calling
    next()
    will return a
    Promise<Response>
    for index.html
  • z

    zsmooth

    02/01/2022, 2:12 AM
    That’s about what i was gonna send @pencilflip ^^
  • p

    pencilflip

    02/01/2022, 2:25 AM
    gotcha thanks for the example
  • p

    pencilflip

    02/01/2022, 2:26 AM
    i think there's some deeper issue with what i'm trying to do though...
  • p

    pencilflip

    02/01/2022, 2:26 AM
    when I do
    yarn build
    and then
    npx wrangler pages dev build
    (for my CRA app), only the home page gets rendered
  • p

    pencilflip

    02/01/2022, 2:26 AM
    if i go to any other page, like
    localhost:3000/test
    , it doesn't render
  • p

    pencilflip

    02/01/2022, 2:27 AM
    even if the test page just looks like this
    Copy code
    function Test() {
      return <div>Test</div>;
    }
  • z

    zsmooth

    02/01/2022, 2:34 AM
    Your function needs to intercept all requests:
    functions/[[path]].js
    is what i believe the file name needs to be to route everything to it
  • p

    pencilflip

    02/01/2022, 2:40 AM
    i see the problem with no functions folddr
  • c

    Cоlе

    02/01/2022, 2:47 AM
    I think you need it. My understanding was that by default, it would show a 404. I could be wrong
  • c

    Cоlе

    02/01/2022, 2:47 AM
    at least, that's how I've been building all my pages
  • c

    Cоlе

    02/01/2022, 2:48 AM
    you typically would have
    [[path]].ts
    which would catch all pages, then try to get it to serve
    index.html
  • c

    Cоlе

    02/01/2022, 2:49 AM
    server sees
    /index.html
    but browser sees
    /whatever-path-you-went-to
  • c

    Cоlе

    02/01/2022, 2:50 AM
    or you do
    [path].ts
    to only catch 1-depth "routes", then 404 everything deeper, such was
    /whatever/path
  • b

    bryan.

    02/01/2022, 5:19 PM
    Is there an SDK for Pages Functions that can be installed to properly resolve types while doing local dev?
  • j

    James

    02/01/2022, 5:24 PM
    I believe
    @cloudflare/workers-types
    exports a
    PagesFunction
    type which can be used. Is there anything else you need?
  • b

    bryan.

    02/01/2022, 5:44 PM
    oh awesome! would it also have
    HTMLRewriter
    ?
  • b

    bryan.

    02/01/2022, 5:45 PM
    I also had another quick question around how the route matching works -- are the only available options
    [path].ts
    and
    [[path]].ts
    at the time being? we'd ideally like to do some type of regex matching to filter this down to a smaller set of routes.
  • b

    bryan.

    02/01/2022, 5:46 PM
    Though, taking a step back the reason we'd like to do this is because we're assuming there's a performance cost with having every request be intercepted by a function -- are there any resources that can help me learn what the potential performance impact of this would be vs. hitting the CDN directly to fetch our static site on Cloudflare Pages?
  • j

    James

    02/01/2022, 5:46 PM
    It does, yep!
  • j

    James

    02/01/2022, 5:47 PM
    Mostly, yep. The full docs can be found at https://developers.cloudflare.com/pages/platform/functions. For anything more complex like a regex match, you'd probably be better off matching the entire folder and then handling the requests manually yourself.
  • j

    James

    02/01/2022, 5:49 PM
    The perf impact of hitting a Function vs hitting the assets on Pages should be effectively zero. Maybe a millisecond or two, but Pages also uses Workers behind the scenes for most (if not all) interactions with your assets which are techncially stored in KV
  • j

    James

    02/01/2022, 5:49 PM
    Today there is a "cost" (not monetary yet) in terms of your functions account limit (100k/day etc.), but in the future, that limit won't apply to requests that just hit static assets, and only to functions that do something
  • b

    bryan.

    02/01/2022, 5:52 PM
    That's amazing! Thanks so much for the helpful replies 🙂
1...707172...392Latest