blitz-js / blitz

⚡️ The Missing Fullstack Toolkit for Next.js
https://Blitzjs.com
MIT License
13.68k stars 798 forks source link

deploy failed on vercel #4303

Closed gengjiawen closed 7 months ago

gengjiawen commented 8 months ago

Repro: https://github.com/gengjiawen/blitzjs-demo Image: CleanShot 2024-02-21 at 21 50 13@2x

          @gengjiawen can you open an issue for this? I can't help without more information or a repro. Thanks!

Originally posted by @siddhsuresh in https://github.com/blitz-js/blitz/issues/4299#issuecomment-1956577459

gengjiawen commented 8 months ago

@siddhsuresh Can you take a look, thx.

gengjiawen commented 8 months ago

remove eval works for me. My guessing is that eval won't compatible with minifier.

tordans commented 8 months ago

@gengjiawen can you be a bit more specific? Thanks!

We are also having this problem...

gengjiawen commented 8 months ago

patch node modules like this (I am using pnpm)

diff --git a/dist/chunks/auth-plugin.cjs b/dist/chunks/auth-plugin.cjs
index beef77a9f480f095381f176b0da5a3c77c4ee25e..dff1216e3f4c6cacc2ba0e05d12254e56679e471 100644
--- a/dist/chunks/auth-plugin.cjs
+++ b/dist/chunks/auth-plugin.cjs
@@ -162,7 +162,7 @@ function getSession(req2, res2, appDir = false) {
 function getBlitzContext() {
   return __async$1(this, null, function* () {
     try {
-      const { headers, cookies } = eval("require('next/headers')");
+      const { headers, cookies } = require('next/headers');
       const req = new http.IncomingMessage(new net.Socket());
       req.headers = Object.fromEntries(headers());
       const csrfToken = cookies().get(index.COOKIE_CSRF_TOKEN());
@@ -201,7 +201,7 @@ function useAuthenticatedBlitzContext(_0) {
     const ctx = yield getBlitzContext();
     const userId = ctx.session.userId;
     try {
-      const { redirect } = eval("require('next/navigation')");
+      const { redirect } = require('next/navigation');
       if (userId) {
         debug("[useAuthenticatedBlitzContext] User is authenticated");
         if (redirectAuthenticatedTo) {
siddhsuresh commented 8 months ago

thanks for the issue @gengjiawen! I will take a look today.

Does this happen locally for you? Since I am no longer able to replicate this after the latest fix.

gengjiawen commented 8 months ago

Does this happen locally for you

If you refer to the deploy issue, The deploy issue only happens on nextjs. My local yarn start works.

siddhsuresh commented 8 months ago

okay thanks for the info. will keep that in mind and debug

cfragkos commented 8 months ago

probably related to #4309. Please fix it fast

gengjiawen commented 7 months ago

@siddhsuresh vercel also breaks this, not sure it's the same problem.

Repro is use this in app router api

async function handle(req: Request) {
  const ctx = await getBlitzContext()
  if (!ctx.session.userId) {
    return new Response("Unauthorized", { status: 401 })
  }

Error:

⨯ Error: Internal Blitz Error: globalThis.__BLITZ_SESSION_COOKIE_PREFIX is not set
    at prefix (/var/task/.next/server/chunks/3637.js:93:126631)
    at Object.COOKIE_CSRF_TOKEN (/var/task/.next/server/chunks/3637.js:93:126968)
    at /var/task/.next/server/chunks/3637.js:93:107389
    at Generator.next (<anonymous>)
    at /var/task/.next/server/chunks/3637.js:93:105600
    at new Promise (<anonymous>)
    at __async$1 (/var/task/.next/server/chunks/3637.js:93:105378)
    at getBlitzContext (/var/task/.next/server/chunks/3637.js:93:107231)
    at handle (/var/task/.next/server/app/api/claude/route.js:1:1912)
    at /var/task/node_modules/.pnpm/next@13.5.4_@babel+core@7.23.9_react-dom@18.2.0_react@18.2.0_sass@1.71.1/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:39754
siddhsuresh commented 7 months ago

@gengjiawen where is getBlitzContext being imported from? if you are importing from "@blitz/auth" instead import it from "app/blitz-server"

gengjiawen commented 7 months ago

@gengjiawen where is getBlitzContext being imported from? if you are importing from "@blitz/auth" instead import it from "app/blitz-server"

import from blitz-server will result in build error.

Attempted import error: 'setupBlitzServer' is not exported from '@blitzjs/next' (imported as 'setupBlitzServer').

adding a new issue: https://github.com/blitz-js/blitz/issues/4310

gengjiawen commented 7 months ago

since this has been 3 weeks, I have created https://github.com/blitz-js/blitz/pull/4311 to fix this issue. This fix all the issues I run into in productions.

flybayer commented 7 months ago

Hey folks, we have investigated, can reproduce, but so far are unable to understand what is happening on Vercel.

Are any of you able to reach out to Vercel support and enlist their help?

cfragkos commented 7 months ago

Yes I am in contact, and got them to investigate, they were quick to blame the 3rd party dependency, but I pressed them after investigating the source code that the way you import and use debug is not wrong and it's their problem, so it seems they will look into it, I suggest you also reach out because you have more technical knowledge to explain than me

cfragkos commented 7 months ago

Got my answer today

Hi again,

Thank you for waiting. I have reviewed this issue with our team's framework-related expert and can confirm that investigating why the "debug" package is not working on Next.js 14 would require implementation-level debugging which is not in the purview of this support team.

The Blitz.js repo is the best place for this, so, that you're able to get support from framework developers and maintainers.

If there are any other platform-related Qs or issues that you may have, do let me know!