Skip to content

Commit

Permalink
wip: move to production config
Browse files Browse the repository at this point in the history
  • Loading branch information
joonatank committed Jan 10, 2025
1 parent 9640101 commit dd0a258
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 32 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ENV NEXT_PUBLIC_SOURCE_BRANCH_NAME=$NEXT_PUBLIC_SOURCE_BRANCH_NAME
# Pass CI variable to the build
ARG CI
ENV CI=$CI
ENV SENTRY_LOG_LEVEL=debug

# Build should not fail on missing env variables
# TODO this should be removed because we need NEXT_PUBLIC_TILAVARAUS_API_URL to be set during build
Expand Down
1 change: 0 additions & 1 deletion apps/admin-ui/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable import/no-extraneous-dependencies */
// @ts-check
import "@testing-library/jest-dom";
import { toHaveNoViolations } from "jest-axe";

Expand Down
25 changes: 21 additions & 4 deletions apps/admin-ui/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as url from "node:url";
import analyser from "@next/bundle-analyzer";
import { withSentryConfig } from "@sentry/nextjs";
import { env } from "./src/env.mjs";
import { getVersion } from "./src/modules/baseUtils.mjs";

// @ts-expect-error -- This works because it's only run on node (not browser)
await import("./src/env.mjs");
Expand Down Expand Up @@ -49,6 +50,11 @@ const config = {
source: "/logout/:any*",
destination: "/logout/:any*",
},
// Do not rewrite sentry tunnel
{
source: "/monitoring/:any*",
destination: "/monitoring/:any*",
},
// Rewrite everything else to use `pages/index`
{
source: "/:any*",
Expand Down Expand Up @@ -97,19 +103,30 @@ export default withSentryConfig(nextConfig, {
// For all available options, see:
// https://github.com/getsentry/sentry-webpack-plugin#options
org: "city-of-helsinki",
project: "tilavaraus-admin-ui",
project: "tilavarauspalvelu-admin-ui",
// only upload source maps to production sentry
sentryUrl: "https://sentry.hel.fi/",
authToken: env.SENTRY_AUTH_TOKEN,
// Only print logs for uploading source maps in CI
silent: !process.env.CI,
// For all available options, see:
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

// Hides source maps from generated client bundles
hideSourceMaps: true,

// Automatically tree-shake Sentry logger statements to reduce bundle size
disableLogger: true,

// Upload a larger set of source maps for prettier stack traces (increases build time)
widenClientFileUpload: true,
// Automatically annotate React components to show their full name in breadcrumbs and session replay
reactComponentAnnotation: {
enabled: false,
},
release: {
name: getVersion(),
},
// Route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
// This can increase your server load as well as your hosting bill.
// Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
// side errors will fail.
tunnelRoute: "/monitoring",
});
2 changes: 1 addition & 1 deletion apps/admin-ui/sentry.client.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
import * as Sentry from "@sentry/nextjs";
import { env } from "@/env.mjs";
import { getVersion } from "@/helpers/serverUtils";
import { getVersion } from "@/modules/baseUtils.mjs";

const { SENTRY_ENVIRONMENT } = env;
const VERSION = getVersion();
Expand Down
2 changes: 1 addition & 1 deletion apps/admin-ui/sentry.edge.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
import * as Sentry from "@sentry/nextjs";
import { env } from "@/env.mjs";
import { getVersion } from "@/helpers/serverUtils";
import { getVersion } from "@/modules/baseUtils.mjs";

const SENTRY_DSN = env.SENTRY_DSN;
const SENTRY_ENVIRONMENT = env.SENTRY_ENVIRONMENT;
Expand Down
2 changes: 1 addition & 1 deletion apps/admin-ui/sentry.server.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
import * as Sentry from "@sentry/nextjs";
import { env } from "@/env.mjs";
import { getVersion } from "@/helpers/serverUtils";
import { getVersion } from "@/modules/baseUtils.mjs";

const { SENTRY_DSN, SENTRY_ENVIRONMENT } = env;
const VERSION = getVersion();
Expand Down
3 changes: 2 additions & 1 deletion apps/admin-ui/src/env.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import { z } from "zod";

// Coerces a string to true if it's "true" or "1", false if "false" or "0"
Expand Down Expand Up @@ -43,7 +44,7 @@ function createEnv() {

if (isServer && !serverConfig?.success) {
// eslint-disable-next-line no-console
console.error("Server env validation failed", serverConfig.error);
console.error("Server env validation failed", serverConfig?.error);
}

const clientSchema = skipValidation ? ClientSchema.partial() : ClientSchema;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { env } from "@/env.mjs";
// @ts-check
import { env } from "../env.mjs";

/** @returns {string} version string based on GIT */
export function getVersion() {
return (
env.NEXT_PUBLIC_SOURCE_BRANCH_NAME?.replace("main", "") ||
Expand Down
2 changes: 1 addition & 1 deletion apps/admin-ui/src/pages/auth/logout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect } from "react";
// NOTE not using App.tsx so need to import i18n here also
import "@/i18n";
import { getVersion } from "@/helpers/serverUtils";
import { getVersion } from "@/modules/baseUtils.mjs";
import { useRouter } from "next/router";

type Props = Awaited<ReturnType<typeof getServerSideProps>>["props"];
Expand Down
2 changes: 1 addition & 1 deletion apps/admin-ui/src/pages/healthcheck.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Head from "next/head";
import Layout from "./layout";
import { getVersion } from "@/helpers/serverUtils";
import { getVersion } from "@/modules/baseUtils.mjs";

/* Separate healthcheck page that does no GraphQL queries to avoid csrf token issues */
export default function Index({ version }: Props) {
Expand Down
2 changes: 1 addition & 1 deletion apps/admin-ui/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { App } from "@/App";
import Layout from "./layout";
import { getVersion } from "@/helpers/serverUtils";
import { getVersion } from "@/modules/baseUtils.mjs";

export type PageProps = Awaited<ReturnType<typeof getServerSideProps>>["props"];

Expand Down
15 changes: 15 additions & 0 deletions apps/ui/modules/baseUtils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @ts-check
/**
* js because this needs to be imported in next.config.mjs
* i.e. during build time so we can't transpile ts to js
*/
import { env } from "../env.mjs";

/** @returns {string} version string based on GIT */
export function getVersion() {
return (
env.NEXT_PUBLIC_SOURCE_BRANCH_NAME?.replace("main", "") ||
env.NEXT_PUBLIC_SOURCE_VERSION?.slice(0, 8) ||
"local"
);
}
10 changes: 2 additions & 8 deletions apps/ui/modules/serverUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ import {
} from "@gql/gql-types";
import { genericTermsVariant } from "./const";
import { base64encode } from "common/src/helpers";

export function getVersion(): string {
return (
env.NEXT_PUBLIC_SOURCE_BRANCH_NAME?.replace("main", "") ||
env.NEXT_PUBLIC_SOURCE_VERSION?.slice(0, 8) ||
"local"
);
}
import { getVersion } from "./baseUtils.mjs";

Check warning on line 17 in apps/ui/modules/serverUtils.ts

View workflow job for this annotation

GitHub Actions / Lint ui

Expected 1 empty line after import statement not followed by another import
export { getVersion }

Check failure on line 18 in apps/ui/modules/serverUtils.ts

View workflow job for this annotation

GitHub Actions / Lint ui

Insert `;`

export function getCommonServerSideProps() {
// NOTE don't return undefined here, it breaks JSON.stringify used by getServerSideProps
Expand Down
20 changes: 9 additions & 11 deletions apps/ui/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as url from "node:url";
import i18nconfig from "./next-i18next.config.js";
import { withSentryConfig } from "@sentry/nextjs";
import { env } from "./env.mjs";
import { getVersion } from "./modules/baseUtils.mjs";

// TODO why was this necessary?
// This breaks tests, they work on admin-ui but not here...
Expand Down Expand Up @@ -68,33 +69,30 @@ export default withSentryConfig(nextConfig, {
// For all available options, see:
// https://github.com/getsentry/sentry-webpack-plugin#options
org: "city-of-helsinki",
project: "tilavaraus-ui",
project: "tilavarauspalvelu-ui",
// project: "tilavaraus-ui",
// only upload source maps to production sentry
sentryUrl: "https://sentry.hel.fi/",
// sentryUrl: "https://sentry.test.hel.ninja/",
authToken: env.SENTRY_AUTH_TOKEN,
// Only print logs for uploading source maps in CI
// probably needs to expose CI env to the docker build
silent: !process.env.CI,
// For all available options, see:
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

// Hides source maps from generated client bundles
hideSourceMaps: true,

// Automatically tree-shake Sentry logger statements to reduce bundle size
disableLogger: true,
// For all available options, see:
// https://github.com/getsentry/sentry-webpack-plugin#options

// For all available options, see:
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

// Upload a larger set of source maps for prettier stack traces (increases build time)
widenClientFileUpload: true,

// Automatically annotate React components to show their full name in breadcrumbs and session replay
reactComponentAnnotation: {
enabled: false,
},

release: {
name: getVersion(),
},
// Route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
// This can increase your server load as well as your hosting bill.
// Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
Expand Down

0 comments on commit dd0a258

Please sign in to comment.