rakkasjs / rakkasjs

Bleeding-edge React framework powered by Vite
https://rakkasjs.org
MIT License
1.06k stars 29 forks source link

cant use await on top when building for vercel #59

Closed sharmapukar217 closed 2 years ago

sharmapukar217 commented 2 years ago

✘ [ERROR] Top-level await is currently not supported with the "cjs" output format.. This is only shown when i set the adapter to values other than nodejs

cyco130 commented 2 years ago

I see. I'll be away from my computer for a few days but I'll fix it as soon as possible when I'm back. Thanks for reporting.

sharmapukar217 commented 2 years ago

One more thing, while using modules like argon2 for vercel and netlify adapter( i haven't tried other), i get error couldn't load .node file. Its similar to the one which i get while using nodejs core nodules with edge run-time on nextjs.

cyco130 commented 2 years ago

For the second problem, I'm working on a solution that will enable us to polyfill some core Node modules. But of course it adds quite a bit of overhead, edge runtimes are simply not compatible with Node. Finding native replacements would be much better.

Native modules (.node) will never be available on the edge but for Vercel and Netlify serverless, it should be possible. I'll investigate how Next.js handles it.

cyco130 commented 2 years ago

Workaround for runtimes that don't support top-level await. Replace the relevant bits in the session example with something like this to eliminate the need for top-level await:

let sessionMiddleware: ReturnType<typeof session>;
const keysPromise = EncryptedCookieStore.generateKeysFromBase64(
  base64Key.split(",") as [string, ...string[]],
);

export default createRequestHandler({
  middleware: {
    beforePages: [
      cookie(),
      async (ctx) => {
        if (!sessionMiddleware) {
          sessionMiddleware = session({
            store: new EncryptedCookieStore(await keysPromise),
            defaultSessionData: {
              cart: [],
            },
            cookieOptions: {
              httpOnly: true,
              secure: import.meta.env.PROD,
              path: "/",
              maxAge: 60 * 60 * 1000, // 1 week
            },
          });
        }

        return sessionMiddleware(ctx);
      },
    ],
  },
});
cyco130 commented 2 years ago

(I'll check whether Vercel really doesn't support top-level await when I have more time)

sharmapukar217 commented 2 years ago

well this one is working for me so i'm closing this issue for now

cyco130 commented 2 years ago

Great, I'll update the example like this so it doesn't require top-level await. I'll see about Vercel later, with a lower priority.