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

    rajatfermat

    03/23/2023, 5:58 PM
    Hi, I am investigating a basic react-ssr setup with Cloudflare pages. In my
    functions/index.js
    , I added the following code:
    Copy code
    import React from "react";
    import ReactDOMServer from "react-dom/server";
    import App from "../src/components/App";
    import template from "../dist/index.html";
    
    export async function onRequestGet(context) {
      // get HTML string from the `App` component
      const appBodyHTML = ReactDOMServer.renderToString( <App /> );
      console.log(appBodyHTML);
      const appHtml = generateFullHTML(template, appBodyHtml)
      return new Response(appHtml, {
        headers: { "content-type": "text/html" },
      });
    }
    The error I am getting is on a tangent though:
    Copy code
    ✘ [ERROR] The JSX syntax extension is not currently enabled
    
        functions/[index].js:71:49:
          71 │   const appHTML = ReactDOMServer.renderToString( <App /> );
             ╵                                                  ^
    
      The esbuild loader for this file is currently set to "js" but it must be set to "jsx" to be able
      to parse JSX syntax. You can use "loader: { '.js': 'jsx' }" to do that.
  • r

    rajatfermat

    03/23/2023, 5:58 PM
    Has anyone come across that before?
  • b

    Better James

    03/23/2023, 6:02 PM
    You're trying to use jsx in a js file that esbuild isn't expecting to contain jsx
  • r

    rajatfermat

    03/23/2023, 6:05 PM
    I am doing this inside
    functions/index.js
    . I dont have
    esbuild
    setup in my project. The project uses webpack to bundle the static assets.
  • m

    meyer

    03/23/2023, 6:18 PM
    wrangler runs esbuild under the hood which means that
    functions/index.js
    is getting crunched with esbuild.
  • m

    meyer

    03/23/2023, 6:19 PM
    (speaking from about two weeks of experience so i might be wrong)
  • s

    Skye

    03/23/2023, 6:34 PM
    You're correct
  • r

    rajatfermat

    03/23/2023, 7:00 PM
    Tweaking the cli tool sounds wrong here. Am I to assume then that SSR is not possible with wrangler+pages ?
  • h

    HardAtWork

    03/23/2023, 7:15 PM
    It is, though in general, people use a system like Vite/Astro, that is built to handle it
  • r

    rajatfermat

    03/23/2023, 7:16 PM
    Is there a simple example of react ssr on cloudflare pages?
  • r

    rajatfermat

    03/23/2023, 7:19 PM
    The error that I am seeing:
    Copy code
    The esbuild loader for this file is currently set to "js" but it must be set to "jsx" to be able
      to parse JSX syntax. You can use "loader: { '.js': 'jsx' }" to do that.
    is fundamentally a blocker for SSR dev. Wrangler needs to be updated. I am on version 2.13.0
  • b

    Better James

    03/23/2023, 7:20 PM
    You are trying to write jsx inside a js file. That is not standard practice. You should be using jsx/tsx files for jsx/tsx
  • b

    Better James

    03/23/2023, 7:21 PM
    Nonetheless, it would be better to use an existing framework/tooling if you want to achieve SSR
  • h

    HardAtWork

    03/23/2023, 7:24 PM
    You should be able to pass in a custom loader, but again, most people just use a framework
  • r

    rajatfermat

    03/23/2023, 7:26 PM
    I eventually want to try a framework like next or remix. However, that is a bigger migration for an existing pure client side react app.
  • r

    rajatfermat

    03/23/2023, 7:27 PM
    I was hoping to render the initial Html inside my
    functions/index.js
    and swap out
    React.render
    with
    React.hydrate
    in my client side app.
  • r

    rajatfermat

    03/23/2023, 7:28 PM
    This would allow me to gain SSR on initial render with minimal code changes to my client code.
  • r

    rajatfermat

    03/23/2023, 7:32 PM
    Basically, i dont want to buy into a full featured framework like Next or Remix just yet.
  • h

    HardAtWork

    03/23/2023, 7:33 PM
    Systems like Vite are very minimal, and shouldn’t require much extra set up
  • h

    HardAtWork

    03/23/2023, 7:34 PM
    And many modern frameworks actually use Vite as a building block, so you could see it as sort of an incremental adoption
  • r

    rajatfermat

    03/23/2023, 7:39 PM
    I am confused. What should I use vite for? My react app uses webpack to bundle. Is it specifically for the
    functions
    folder?
  • h

    HardAtWork

    03/23/2023, 7:54 PM
    Vite/Astro come with a very fast bundler, but they also have SSR that works on Workers directly
  • h

    HardAtWork

    03/23/2023, 7:55 PM
    So you don’t have to mess with wrangler trying to compile some JSX
  • h

    HardAtWork

    03/23/2023, 7:55 PM
    https://vitejs.dev
  • r

    rajatfermat

    03/23/2023, 7:55 PM
    Is their some existing literature on this?
  • h

    HardAtWork

    03/23/2023, 7:57 PM
    This one is from Astro: https://docs.astro.build/en/guides/integrations-guide/cloudflare/
  • s

    Skye

    03/23/2023, 7:59 PM
    I really would recommend picking up a framework that's similar to what you're aiming to do instead of reinventing the wheel
  • s

    Skye

    03/23/2023, 7:59 PM
    It may take slightly longer, but you'll get a lot more out of it
  • h

    HardAtWork

    03/23/2023, 8:01 PM
    Oh wait, here’s the one for Vite…
  • h

    HardAtWork

    03/23/2023, 8:01 PM
    https://vite-plugin-ssr.com/cloudflare-pages
1...362363364...392Latest