Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

@clerk/react-router using CloudFare Template #4759

Closed
4 tasks done
LydiaF opened this issue Dec 11, 2024 · 2 comments
Closed
4 tasks done

@clerk/react-router using CloudFare Template #4759

LydiaF opened this issue Dec 11, 2024 · 2 comments

Comments

@LydiaF
Copy link

LydiaF commented Dec 11, 2024

Preliminary Checks

Reproduction

https://github.com/lydiaf/importbug

Publishable key

Description

Steps to reproduce:

  1. Open root.tsx, and comment in and out the import rootAuthLoader, you can see app stops loading

Expected behavior:

App loads

Actual behavior:

App does not load with input

--

I am just experiencing this with Cloudflare template, not Vercel template.

Environment

-
@LydiaF LydiaF added the needs-triage A ticket that needs to be triaged by a team member label Dec 11, 2024
@LekoArts LekoArts added confirmed and removed needs-triage A ticket that needs to be triaged by a team member labels Dec 16, 2024
@LekoArts
Copy link
Member

Hello, thanks for the issue!

I've ran your reproduction and can confirm that it it fails with require is not defined.

[vite-node-miniflare error]
ReferenceError: require is not defined
    at /Users/lejoe/code/playground/importbug/node_modules/snakecase-keys/index.js:3:13

@clerk/react-router imports @clerk/backend which itself imports snakecase-keys. However, this is a quite common error with Vite bundling. snakecase-keys is CJS and you can't use require in ESM builds. So you'll need to adjust the optimizeDeps.include list.

I've got it working like this:

  1. Change the template by following this PR change: Modify to use cloudflareDevProxy and load contents of wrangler.toml remix-run/react-router-templates#6

  2. Add snakecase-keys and cookie to the list

        optimizeDeps: {
          include: [
            "react",
            "react/jsx-runtime",
            "react/jsx-dev-runtime",
            "react-dom",
            "react-dom/server",
            "react-router",
    +       "snakecase-keys",
    +       "cookie"
          ],
        },

@LydiaF
Copy link
Author

LydiaF commented Dec 27, 2024

Hi @LekoArts, thanks for the reply.

It is no longer throwing errors, but now I cannot get the <SignIn /> component to render. Is there something else I need to do?

Here is my root file

import {
  isRouteErrorResponse,
  Links,
  Meta,
  Outlet,
  Scripts,
  ScrollRestoration,
} from "react-router"
import { rootAuthLoader } from '@clerk/react-router/ssr.server'
import type { Route } from "./+types/root"
import stylesheet from "./app.css?url"
import { ClerkProvider } from '@clerk/react-router'

export const loader = async (args: Route.LoaderArgs) => {
  return rootAuthLoader(args, () => {
    return {}
  }, {
    secretKey: args.context.cloudflare.env.CLERK_SECRET_KEY,
    publishableKey: args.context.cloudflare.env.CLERK_PUBLISHABLE_KEY,
  })
}

export const links: Route.LinksFunction = () => [
  { rel: "preconnect", href: "https://fonts.googleapis.com" },
  {
    rel: "preconnect",
    href: "https://fonts.gstatic.com",
    crossOrigin: "anonymous",
  },
  {
    rel: "stylesheet",
    href: "https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap",
  },
  { rel: "stylesheet", href: stylesheet },
]

export function Layout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <Meta />
        <Links />
      </head>
      <body>
        {children}
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  )
}

export default function App({ loaderData }: Route.ComponentProps) {
  return <ClerkProvider loaderData={loaderData} >
    <Outlet />
  </ClerkProvider>
}

export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
  let message = "Oops!"
  let details = "An unexpected error occurred."
  let stack: string | undefined

  if (isRouteErrorResponse(error)) {
    message = error.status === 404 ? "404" : "Error"
    details =
      error.status === 404
        ? "The requested page could not be found."
        : error.statusText || details
  } else if (import.meta.env.DEV && error && error instanceof Error) {
    details = error.message
    stack = error.stack
  }

  return (
    <main className="pt-16 p-4 container mx-auto">
      <h1>{message}</h1>
      <p>{details}</p>
      {stack && (
        <pre className="w-full p-4 overflow-x-auto">
          <code>{stack}</code>
        </pre>
      )}
    </main>
  )
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants