getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.75k stars 1.52k forks source link

NextJS SSR with Vercel not working #12619

Open camillevingere opened 6 days ago

camillevingere commented 6 days ago

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

8.9.2

Framework Version

14.2.4

Link to Sentry event

No response

SDK Setup

// This file configures the initialization of Sentry on the server. // The config you add here will be used whenever the server handles a request. // https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from "@sentry/nextjs";

const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;

Sentry.init({ dsn: SENTRY_DSN || 'DSN',

// Adjust this value in production, or use tracesSampler for greater control tracesSampleRate: 1,

// Setting this option to true will print useful information to the console while you're setting up Sentry. debug: false,

integrations: [Sentry.prismaIntegration()],

// Uncomment the line below to enable Spotlight (https://spotlightjs.com) // spotlight: process.env.NODE_ENV === 'development',

});

Steps to Reproduce

  1. Take a NextJS project
  2. Deploy in production on Vercel
  3. Don't forget to put env variables

Expected Result

The errors should be logged in Sentry

Actual Result

The errors are logged in local development in CSR and SSR but in production on VERCEL only the CSR is working. All that is an server side error is not logged in Sentry.

lforst commented 6 days ago

Hi, can you share how you are throwing errors that are not logged? Thanks!

camillevingere commented 6 days ago

Hi, here is an example :

`export const requiredAuth = async () => { const user = await auth();

if (!user) { throw new AuthError("You must be authenticated to access this resource."); }

return user; };`

This is working in local but not on Vercel.

lforst commented 6 days ago

Where are you calling requiredAuth? In a server component? Api route? Route handler? App dir, pages dir?

camillevingere commented 5 days ago

I am using it in a Server Component

lforst commented 5 days ago

Would you mind giving a small but complete example? Please verify that this is not your issue: https://docs.sentry.io/platforms/javascript/guides/nextjs/#:~:text=Errors%20in%20Nested%20React%20Server%20Components (section "Errors in Nested React Server Components")

camillevingere commented 3 days ago

Sure,

I already checked and this is not the case. Reminder : it is working in local.

Here is a SSR page :

import {
  Layout,
  LayoutContent,
  LayoutHeader,
  LayoutTitle
} from "@/components/layout/layout";
import { buttonVariants } from "@/components/ui/button";
import { DataTable } from "@/components/ui/data-table";
import { requiredAuth } from "@/lib/auth/helper";
import Link from "next/link";
import AnalysisChart from "./AnalysisChart";
import { columns } from "./columns";
import { getMarkets } from "./market.query";
import { getProducts } from "../../courses/product.query";
import { Typography } from "@/components/ui/typography";

export default async function MarketAnalysisPage({
  searchParams,
}: {
  searchParams: { [key: string]: string | string[] | undefined };
}) {
  const user = await requiredAuth();
  const page = Number(searchParams.page ?? 0);

  const products = await getProducts({
    userId: user.id,
  });

  if (
    products.products.find(
      (product) => product.name === "Devenir Développeur Freelance V2",
    ) === undefined
  ) {
    return (
      <Layout>
        <LayoutHeader>
          <LayoutTitle>Analyse du marché</LayoutTitle>
        </LayoutHeader>
        <LayoutContent>
          <Typography className="mb-4">
            Tu dois avoir la formation{" "}
            <strong>Devenir Développeur Freelance™️</strong> pour accéder à cet
            outil.
          </Typography>
          <Link
            className={buttonVariants({ size: "sm", variant: "outline" })}
            href="/formations/devenir-developpeur-freelance"
          >
            Acheter la formation
          </Link>
        </LayoutContent>
      </Layout>
    );
  }

  const markets = await getMarkets({
    userId: user.id,
    userPage: page,
  });

  return (
    <Layout>
      <LayoutHeader>
        <LayoutTitle>Analyse du marché</LayoutTitle>
      </LayoutHeader>
      <LayoutContent className="flex flex-col gap-8">
        {markets && <AnalysisChart data={markets} />}
        <div>
          <Link
            className={buttonVariants({ size: "sm", variant: "outline" })}
            href="/analysis/market/new"
          >
            Nouvelle entrée
          </Link>
        </div>
        {markets && (
          <DataTable
            columns={columns}
            data={markets.sort(function (a, b) {
              return b.date.getTime() - a.date.getTime();
            })}
          />
        )}
      </LayoutContent>
    </Layout>
  );
}

With the requiredAuth which throw an error if not connected. Sentry logs in local but not in production using Vercel.

lforst commented 3 days ago

Yeah okay, that error should definitely be reported.

I tried to reproduce with a server component like yours:

export const dynamic = 'force-dynamic';

export default async function Page() {
  throw new Error('boom');
  return <p>hi</p>;
}

and it is reported, even when deploying on vercel. Can you verify that you set your DSN correctly and maybe turn on debug: true in your server Sentry.init() call? Thanks.

camillevingere commented 2 hours ago

Capture d’écran du 2024-07-01 10-33-19 Capture d’écran du 2024-07-01 10-32-38 Capture d’écran du 2024-07-01 10-31-36

I checked again, I have the DSN in env variables, the same in NEXT_PUBLIC_SENTRY_DSN and SENTRY_DSN (since it is working client side, I think the DSN is good).

As you can see the error is logged in Vercel, and the client is logging the error in the console which is a Server Component.

I checked the build logs with debug true, nothing unusual (but I can send you all the logs if you want)

lforst commented 2 hours ago

If you add

  beforeSend(event, hint) {
    console.log(event.exception?.values);
    return event;
  },

Do you see anything in the logs?

camillevingere commented 2 hours ago

I tried and nothing, since it is not send I doubt it goes in the beforeSend :/

camillevingere commented 19 minutes ago

I tried to put the DSN in hardcode to be sure, still nothing logged

camillevingere commented 15 minutes ago

Update: server actions errors seemed to be logged in sentry (but not the other server errors)