Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/ui/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
type AutocompleteCollection,
} from "@algolia/autocomplete-core";
import { getAlgoliaResults } from "@algolia/autocomplete-preset-algolia";
import algoliasearch from "algoliasearch/lite";
import { liteClient as algoliasearch } from "algoliasearch/lite";
import {
Dialog,
DialogPanel,
Expand Down Expand Up @@ -113,8 +113,8 @@ function useAutocomplete({
queries: [
{
indexName: ALGOLIA_SOURCE_IDX,
query,
params: {
query,
hitsPerPage: 6,
},
},
Expand Down
26 changes: 26 additions & 0 deletions instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as Sentry from "@sentry/nextjs";

const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;
const SENTRY_ENVIRONMENT = process.env.SENTRY_ENVIRONMENT;
Comment on lines +1 to +4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider improving environment variable handling

The current implementation has a few potential issues:

  1. Using both SENTRY_DSN and NEXT_PUBLIC_SENTRY_DSN might lead to confusion. Consider using only one of them consistently.
  2. SENTRY_ENVIRONMENT is not checked for existence, which might lead to undefined behavior if the environment variable is not set.

Consider refactoring the environment variable handling as follows:

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

const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN;
const SENTRY_ENVIRONMENT = process.env.SENTRY_ENVIRONMENT || 'development';

if (!SENTRY_DSN) {
  console.warn('NEXT_PUBLIC_SENTRY_DSN is not set. Sentry will not be initialized.');
}

This approach:

  • Uses only NEXT_PUBLIC_SENTRY_DSN for consistency
  • Provides a default value for SENTRY_ENVIRONMENT
  • Adds a warning if SENTRY_DSN is not set


export function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
// this is your Sentry.init call from `sentry.server.config.js|ts`
Sentry.init({
dsn: SENTRY_DSN,
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1,
environment: SENTRY_ENVIRONMENT,
});
}
Comment on lines +7 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve Sentry initialization for Node.js runtime

The Sentry initialization for Node.js looks generally correct, but there are a few points to consider:

  1. Setting tracesSampleRate to 1 might be too high for production, as it samples 100% of transactions.
  2. There's no error handling if SENTRY_DSN is undefined.

Consider refactoring the Node.js initialization as follows:

if (process.env.NEXT_RUNTIME === "nodejs") {
  if (SENTRY_DSN) {
    Sentry.init({
      dsn: SENTRY_DSN,
      tracesSampleRate: process.env.NODE_ENV === 'production' ? 0.1 : 1,
      environment: SENTRY_ENVIRONMENT,
    });
  } else {
    console.warn('Sentry DSN not provided. Skipping Sentry initialization for Node.js runtime.');
  }
}

This approach:

  • Adjusts tracesSampleRate based on the environment
  • Adds error handling for missing SENTRY_DSN
  • Uses the SENTRY_ENVIRONMENT variable we defined earlier


// This is your Sentry.init call from `sentry.edge.config.js|ts`
if (process.env.NEXT_RUNTIME === "edge") {
Sentry.init({
dsn: SENTRY_DSN,
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1,
environment: SENTRY_ENVIRONMENT,
});
}
Comment on lines +18 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor Edge runtime initialization to reduce duplication

The Sentry initialization for Edge runtime is identical to the Node.js runtime, which leads to code duplication and the same potential issues we identified earlier.

Consider refactoring to reduce duplication and improve both initializations:

function initSentry(runtime: string) {
  if (SENTRY_DSN) {
    Sentry.init({
      dsn: SENTRY_DSN,
      tracesSampleRate: process.env.NODE_ENV === 'production' ? 0.1 : 1,
      environment: SENTRY_ENVIRONMENT,
    });
  } else {
    console.warn(`Sentry DSN not provided. Skipping Sentry initialization for ${runtime} runtime.`);
  }
}

export function register() {
  switch (process.env.NEXT_RUNTIME) {
    case "nodejs":
      initSentry("Node.js");
      break;
    case "edge":
      initSentry("Edge");
      break;
    default:
      console.warn(`Unexpected NEXT_RUNTIME value: ${process.env.NEXT_RUNTIME}`);
  }
}

This refactored version:

  • Reduces code duplication
  • Improves error handling
  • Adjusts tracesSampleRate based on the environment
  • Provides more informative logging

}
2 changes: 1 addition & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
20 changes: 9 additions & 11 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,18 @@ const config = {
// Temporary to check pipelines due to weird error I can only get on CodePipeline
ignoreBuildErrors: true,
},
experimental: {
instrumentationHook: true,
},
};

// Injected content via Sentry wizard below

const { withSentryConfig } = require("@sentry/nextjs");

module.exports = withSentryConfig(
withMDX(withBundleAnalyzer(config)),
{
silent: true,
org: "codu",
project: "codu",
},
{
hideSourceMaps: true,
},
);
module.exports = withSentryConfig(withMDX(withBundleAnalyzer(config)), {
silent: true,
org: "codu",
project: "codu",
hideSourceMaps: true,
});
Loading