Skip to content

Commit 0736a0d

Browse files
Make stackServerApp lazy: don't require STACK_SECRET_SERVER_KEY at build time
The StackServerApp constructor requires STACK_SECRET_SERVER_KEY, but this env var isn't available during next build in CI workflows that don't provide .env.production.local. Since Next.js evaluates modules at build time to collect page metadata, importing server.tsx would crash. Fix: change stackServerApp from a module-level constant to a lazily- initialized getter function getStackServerApp(). The StackServerApp is only constructed on first runtime use, not during build. Also reverts the .env placeholder hack from the previous commit. Co-Authored-By: Konstantin Wohlwend <n2d4xc@gmail.com>
1 parent 6fc1bf1 commit 0736a0d

5 files changed

Lines changed: 22 additions & 16 deletions

File tree

apps/dashboard/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ NEXT_PUBLIC_STACK_API_URL=# enter your stack endpoint here, For local developmen
33
NEXT_PUBLIC_STACK_IS_LOCAL_EMULATOR=# set to true to enable local emulator UI behavior (auto-login + read-only environment config updates)
44
NEXT_PUBLIC_STACK_PROJECT_ID=internal
55
NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY=# enter your Stack publishable client key here. For local development, just enter a random string, then run `pnpm db:reset`
6-
STACK_SECRET_SERVER_KEY=this-secret-server-key-is-for-local-development-only
6+
STACK_SECRET_SERVER_KEY=# enter your Stack secret client key here. For local development, do the same as above
77
NEXT_PUBLIC_STACK_DOCS_BASE_URL=# enter the base URL of the docs here
88
NEXT_PUBLIC_STACK_EXTRA_REQUEST_HEADERS=# a list of extra request headers to add to all Stack Auth API requests, as a JSON record
99
NEXT_PUBLIC_STACK_STRIPE_PUBLISHABLE_KEY=# enter your Stripe publishable key here

apps/dashboard/src/app/(main)/(protected)/(outside-dashboard)/projects/actions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
"use server";
22
import { isRemoteDevelopmentEnvironmentEnabled } from "@/lib/remote-development-environment/env";
33

4-
async function getStackServerApp() {
4+
async function getServerApp() {
55
if (isRemoteDevelopmentEnvironmentEnabled()) {
66
throw new Error("Team invitation management is not available in the remote development environment dashboard.");
77
}
8-
return (await import("@/stack/server")).stackServerApp;
8+
return (await import("@/stack/server")).getStackServerApp();
99
}
1010

1111
export async function revokeInvitation(teamId: string, invitationId: string) {
1212
"use server";
13-
const stackServerApp = await getStackServerApp();
13+
const stackServerApp = await getServerApp();
1414
const user = await stackServerApp.getUser();
1515
const team = await user?.getTeam(teamId);
1616
if (!team) {
@@ -24,7 +24,7 @@ export async function revokeInvitation(teamId: string, invitationId: string) {
2424
}
2525

2626
export async function listInvitations(teamId: string) {
27-
const stackServerApp = await getStackServerApp();
27+
const stackServerApp = await getServerApp();
2828
const user = await stackServerApp.getUser();
2929
const team = await user?.getTeam(teamId);
3030
if (!team) {
@@ -39,7 +39,7 @@ export async function listInvitations(teamId: string) {
3939
}
4040

4141
export async function inviteUser(teamId: string, email: string, origin: string) {
42-
const stackServerApp = await getStackServerApp();
42+
const stackServerApp = await getServerApp();
4343
const callbackUrl = new URL(stackServerApp.urls.teamInvitation, origin).toString();
4444
const user = await stackServerApp.getUser();
4545
const team = await user?.getTeam(teamId);

apps/dashboard/src/app/(main)/integrations/featurebase/sso/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { stackServerApp } from "@/stack/server";
1+
import { getStackServerApp } from "@/stack/server";
22
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
33
import { getOrCreateFeaturebaseUser } from "@stackframe/stack-shared/dist/utils/featurebase";
44
import { urlString } from "@stackframe/stack-shared/dist/utils/urls";
@@ -21,7 +21,7 @@ export default async function FeaturebaseSSO({
2121
return <div>Missing return_to parameter. Please go back and try again.</div>;
2222
}
2323

24-
const user = await stackServerApp.getUser();
24+
const user = await getStackServerApp().getUser();
2525
if (!user) {
2626
redirect(urlString`/handler/sign-in?after_auth_return_to=${urlString`/integrations/featurebase/sso?return_to=${returnTo}`}`);
2727
}

apps/dashboard/src/app/(main)/integrations/oauth-confirm-page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { stackServerApp } from "@/stack/server";
1+
import { getStackServerApp } from "@/stack/server";
22
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
33
import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors";
44
import { redirect } from "next/navigation";
@@ -18,7 +18,7 @@ export default async function IntegrationConfirmPage(props: {
1818
const onContinue = async (options: { projectId: string, projectName?: string }) => {
1919
"use server";
2020

21-
const user = await stackServerApp.getUser();
21+
const user = await getStackServerApp().getUser();
2222
if (!user) {
2323
return { error: "unauthorized" };
2424
}

apps/dashboard/src/stack/server.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import { StackServerApp } from "@stackframe/stack";
55
import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors";
66
import { stackClientApp } from "./client";
77

8-
if (isRemoteDevelopmentEnvironmentEnabled()) {
9-
throw new StackAssertionError("stackServerApp is not available in the local remote development environment dashboard.");
10-
}
8+
let _stackServerApp: StackServerApp | undefined;
119

12-
export const stackServerApp = new StackServerApp({
13-
inheritsFrom: stackClientApp,
14-
});
10+
export function getStackServerApp(): StackServerApp {
11+
if (!_stackServerApp) {
12+
if (isRemoteDevelopmentEnvironmentEnabled()) {
13+
throw new StackAssertionError("stackServerApp is not available in the local remote development environment dashboard.");
14+
}
15+
_stackServerApp = new StackServerApp({
16+
inheritsFrom: stackClientApp,
17+
});
18+
}
19+
return _stackServerApp;
20+
}

0 commit comments

Comments
 (0)