Skip to content

(SP: 1) [FIX] Fix Netlify SSR runtime env vars for develop deploy#425

Merged
ViktorSvertoka merged 5 commits intodevelopfrom
sl/feat/db-optimization
Mar 28, 2026
Merged

(SP: 1) [FIX] Fix Netlify SSR runtime env vars for develop deploy#425
ViktorSvertoka merged 5 commits intodevelopfrom
sl/feat/db-optimization

Conversation

@LesiaUKR
Copy link
Copy Markdown
Collaborator

@LesiaUKR LesiaUKR commented Mar 28, 2026

Goal

Fix Netlify develop deploy crash where SSR runtime does not receive env vars from dashboard despite correct configuration (All scopes, all contexts).

Scope

  • Remove node_bundler = "esbuild" from netlify.toml — conflicted with @netlify/plugin-nextjs own bundling, potentially breaking env var injection
  • Add env block in next.config.ts to inline APP_ENV and DATABASE_URL at build time — ensures values are available in SSR runtime regardless of platform env delivery

Expected impact

All 28 SSR pages that depend on @/db should stop crashing on Netlify develop deploy. Blog categories in header remain disabled (separate follow-up).

Out of scope

  • Restoring blog categories in root layout — separate task after SSR stability is confirmed
  • Hardcoding blog categories — planned follow-up in _dev-notes/blog-header-hardcode-plan.md

Summary by CodeRabbit

  • Bug Fixes

    • Fixed category title formatting to properly trim whitespace and prevent displaying empty titles.
  • Tests

    • Updated test environment configuration to properly inject required environment variables.
  • Chores

    • Removed explicit bundler override from deployment configuration.
    • Made environment setup more flexible by allowing optional environment variables.

@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Mar 28, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
devlovers-net Ignored Ignored Preview Mar 28, 2026 2:18pm

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 28, 2026

📝 Walkthrough

Walkthrough

This pull request relaxes APP_ENV validation, making it optional rather than required; updates environment variable exposure in Next.js configuration and test runtimes; improves category title fallback logic to trim whitespace; and removes explicit Netlify Functions bundler configuration.

Changes

Cohort / File(s) Summary
Environment Variable Configuration
frontend/db/index.ts, frontend/next.config.ts, frontend/vitest.config.ts, frontend/vitest.shop.config.ts
Removed hard-fail check for missing APP_ENV; added runtime logging for APP_ENV, NETLIFY, CONTEXT, and NODE_ENV in non-test runs; exposed APP_ENV and DATABASE_URL to Next.js runtime; injected APP_ENV: 'local' into Vitest test environments.
Category Query Logic
frontend/db/queries/categories/admin-categories.ts
Changed title fallback from row.title ?? row.slug to `row.title?.trim()
Build Configuration
netlify.toml
Removed node_bundler = "esbuild" setting from Netlify Functions configuration.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • (SP: 1) [FIX] Hot fix index APP ENV #421 — Modifies APP_ENV handling and IS_LOCAL_ENV initialization in the same file with overlapping concerns around environment variable normalization.
  • Sl/feat/db optimization #423 — Touches both frontend/db/index.ts for APP_ENV runtime checks and frontend/db/queries/categories/admin-categories.ts for title fallback logic.
  • Develop #235 — Modifies netlify.toml with conflicting changes to the Netlify Functions node bundler configuration.

Suggested labels

bug

Suggested reviewers

  • AM1007
  • ViktorSvertoka
  • liudmylasovetovs

Poem

🐰 Hops through the config with springy delight,
APP\_ENV now optional—a more flexible sight!
Trimmed titles and logging, bundlers removed with care,
The codebase hops forward with environmental flair! 🌿

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly addresses the main objective: fixing Netlify SSR runtime environment variables for develop deployment.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sl/feat/db-optimization

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (4)
frontend/vitest.shop.config.ts (1)

16-18: Same formatting inconsistency as vitest.config.ts.

The env block indentation is misaligned with sibling properties.

🔧 Suggested fix
   test: {
-      env: {
-    APP_ENV: 'local',
-    },
+    env: {
+      APP_ENV: 'local',
+    },
     environment: 'node',
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/vitest.shop.config.ts` around lines 16 - 18, The env block in
frontend/vitest.shop.config.ts is misindented compared to sibling properties;
fix the indentation of the env object (the APP_ENV property) so it lines up with
other top-level keys in the config (match the formatting used in
vitest.config.ts), ensuring consistent spacing and alignment for the env: {
APP_ENV: 'local' } block.
frontend/db/index.ts (1)

82-85: Clarify error message when APP_ENV is undefined.

When APP_ENV is unset, IS_LOCAL_ENV is false, so this branch executes. The error message interpolates APP_ENV which would show "APP_ENV=undefined requires DATABASE_URL" — this could be confusing for debugging.

💡 Suggested improvement
   if (!url) {
     throw new Error(
-      `[db] APP_ENV=${APP_ENV} requires DATABASE_URL to be set`
+      `[db] APP_ENV=${APP_ENV ?? '<undefined>'} requires DATABASE_URL to be set`
     );
   }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/db/index.ts` around lines 82 - 85, The error thrown when
DATABASE_URL is missing interpolates APP_ENV which may be undefined and
confusing; update the throw in frontend/db/index.ts to produce a clear message
by checking APP_ENV (and/or IS_LOCAL_ENV) and printing a readable value like
"unset" or "unknown" (e.g., use APP_ENV ?? 'unset') and include context that
DATABASE_URL is missing and whether IS_LOCAL_ENV is true/false; change the throw
new Error(...) around the url check to use this clarified message referencing
APP_ENV and IS_LOCAL_ENV.
frontend/vitest.config.ts (1)

16-18: Minor formatting inconsistency.

The env block indentation appears misaligned compared to sibling properties like environment and include. This may cause linter warnings.

🔧 Suggested fix
   test: {
-      env: {
-    APP_ENV: 'local',
-    },
+    env: {
+      APP_ENV: 'local',
+    },
     environment: 'node',
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/vitest.config.ts` around lines 16 - 18, The env block in
vitest.config.ts is misindented relative to sibling properties (e.g.,
environment and include); fix by aligning the env property and its inner APP_ENV
line with the same indentation level used for environment and include so the
object properties are consistently formatted (look for the env: { APP_ENV:
'local', } block and adjust spacing to match the surrounding configuration
entries).
frontend/next.config.ts (1)

8-11: Refactor to use serverRuntimeConfig for server-only secrets.

The env block exposes DATABASE_URL to both server and client code paths. While no client code currently references process.env.DATABASE_URL, this pattern is fragile — someone could accidentally add a client-side reference and expose the connection string. Server-only secrets should use serverRuntimeConfig instead:

const nextConfig: NextConfig = {
  serverRuntimeConfig: {
    DATABASE_URL: process.env.DATABASE_URL,
  },
  publicRuntimeConfig: {
    APP_ENV: process.env.APP_ENV,
  },
  // ...
};

Then access as getServerSideProps or getConfig().serverRuntimeConfig.DATABASE_URL on the server, keeping it unreachable from client bundles.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/next.config.ts` around lines 8 - 11, The current next.config.ts
exposes DATABASE_URL via the env block; change the NextConfig export to move
DATABASE_URL into serverRuntimeConfig and put APP_ENV into publicRuntimeConfig
(i.e., replace the env object with serverRuntimeConfig: { DATABASE_URL:
process.env.DATABASE_URL } and publicRuntimeConfig: { APP_ENV:
process.env.APP_ENV }), then update server-side access patterns to read the
secret from getConfig().serverRuntimeConfig.DATABASE_URL or within
getServerSideProps rather than from process.env in client-safe code; ensure no
client bundle imports process.env.DATABASE_URL.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@frontend/db/index.ts`:
- Around line 82-85: The error thrown when DATABASE_URL is missing interpolates
APP_ENV which may be undefined and confusing; update the throw in
frontend/db/index.ts to produce a clear message by checking APP_ENV (and/or
IS_LOCAL_ENV) and printing a readable value like "unset" or "unknown" (e.g., use
APP_ENV ?? 'unset') and include context that DATABASE_URL is missing and whether
IS_LOCAL_ENV is true/false; change the throw new Error(...) around the url check
to use this clarified message referencing APP_ENV and IS_LOCAL_ENV.

In `@frontend/next.config.ts`:
- Around line 8-11: The current next.config.ts exposes DATABASE_URL via the env
block; change the NextConfig export to move DATABASE_URL into
serverRuntimeConfig and put APP_ENV into publicRuntimeConfig (i.e., replace the
env object with serverRuntimeConfig: { DATABASE_URL: process.env.DATABASE_URL }
and publicRuntimeConfig: { APP_ENV: process.env.APP_ENV }), then update
server-side access patterns to read the secret from
getConfig().serverRuntimeConfig.DATABASE_URL or within getServerSideProps rather
than from process.env in client-safe code; ensure no client bundle imports
process.env.DATABASE_URL.

In `@frontend/vitest.config.ts`:
- Around line 16-18: The env block in vitest.config.ts is misindented relative
to sibling properties (e.g., environment and include); fix by aligning the env
property and its inner APP_ENV line with the same indentation level used for
environment and include so the object properties are consistently formatted
(look for the env: { APP_ENV: 'local', } block and adjust spacing to match the
surrounding configuration entries).

In `@frontend/vitest.shop.config.ts`:
- Around line 16-18: The env block in frontend/vitest.shop.config.ts is
misindented compared to sibling properties; fix the indentation of the env
object (the APP_ENV property) so it lines up with other top-level keys in the
config (match the formatting used in vitest.config.ts), ensuring consistent
spacing and alignment for the env: { APP_ENV: 'local' } block.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d120a427-f439-4db9-af7b-c31bbc414347

📥 Commits

Reviewing files that changed from the base of the PR and between 04191f8 and 8aef55f.

📒 Files selected for processing (6)
  • frontend/db/index.ts
  • frontend/db/queries/categories/admin-categories.ts
  • frontend/next.config.ts
  • frontend/vitest.config.ts
  • frontend/vitest.shop.config.ts
  • netlify.toml
💤 Files with no reviewable changes (1)
  • netlify.toml

@ViktorSvertoka ViktorSvertoka merged commit 6e021de into develop Mar 28, 2026
8 checks passed
@ViktorSvertoka ViktorSvertoka deleted the sl/feat/db-optimization branch March 28, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants