Skip to content

Comments

perf: Improve memory cache on build#23286

Merged
keithwillcode merged 2 commits intomainfrom
perf/improve-build-memory-cache
Aug 22, 2025
Merged

perf: Improve memory cache on build#23286
keithwillcode merged 2 commits intomainfrom
perf/improve-build-memory-cache

Conversation

@volnei
Copy link
Contributor

@volnei volnei commented Aug 22, 2025

What does this PR do?

Following NextJS Guide https://nextjs.org/docs/app/guides/memory-usage#try-experimentalwebpackmemoryoptimizations
I introduced some memory optimizations to avoid build OOM errors.

@graphite-app graphite-app bot requested a review from a team August 22, 2025 17:58
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 22, 2025

Walkthrough

This PR updates apps/web/next.config.js by enabling experimental.webpackMemoryOptimizations and experimental.webpackBuildWorker, extending the webpack config function to accept the dev flag, and configuring a non-development immutable in-memory cache when config.cache exists. It also updates turbo.json to include NEXT_PUBLIC_API_V2_URL, NEXT_PUBLIC_WEBAPP_URL, NEXT_PUBLIC_WEBSITE_URL, and BUILD_STANDALONE in the globalEnv array, with a formatting change adding a trailing comma after CSP_POLICY.

Possibly related PRs

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch perf/improve-build-memory-cache

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@keithwillcode keithwillcode added core area: core, team members only foundation labels Aug 22, 2025
@graphite-app
Copy link

graphite-app bot commented Aug 22, 2025

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (08/22/25)

1 reviewer was added to this PR based on Keith Williams's automation.

"Add ready-for-e2e label" took an action on this PR • (08/22/25)

1 label was added to this PR based on Keith Williams's automation.

Copy link
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.

Actionable comments posted: 0

🧹 Nitpick comments (2)
apps/web/next.config.js (1)

199-201: Add env-toggle “kill switches” for experimental webpack flags

  • Confirmed that apps/web/package.json declares Next.js v15.4.5, which supports both experimental.webpackMemoryOptimizations and experimental.webpackBuildWorker.
  • Gating these behind environment variables lets you quickly disable them (e.g. in CI or canary rollouts) without code changes.

File: apps/web/next.config.js

       experimental: {
         // externalize server-side node_modules with size > 1mb, to improve dev mode performance/RAM usage
         optimizePackageImports: ["@calcom/ui"],
-        webpackMemoryOptimizations: true,
-        webpackBuildWorker: true,
+        webpackMemoryOptimizations:
+          process.env.NEXT_EXPERIMENTAL_WEBPACK_MEMORY_OPTIMIZATIONS !== "false",
+        webpackBuildWorker:
+          process.env.NEXT_EXPERIMENTAL_WEBPACK_BUILD_WORKER !== "false",
       },
turbo.json (1)

253-257: Scope CSP_POLICY, NEXT_PUBLIC_API_V2_URL and BUILD_STANDALONE to web tasks

Our grep confirms that only apps/web references these three vars (middleware, Next.js config, CSP/lib logic, proxy’ing, tests) and nowhere else in the monorepo:

  • CSP_POLICY is used exclusively by apps/web (middleware.ts, next.config.js, lib/csp.ts, middleware.test.ts) and in the website build task’s env block.
  • NEXT_PUBLIC_API_V2_URL only appears in apps/web (next.config.js proxy, SSR helpers, feature flags) and in two task-specific env arrays.
  • BUILD_STANDALONE only toggles Next.js’s output: "standalone" inside apps/web/next.config.js.

By contrast, NEXT_PUBLIC_WEBAPP_URL and NEXT_PUBLIC_WEBSITE_URL are referenced across packages—constants, seed scripts, APIs, embeds, mail templates, UI components, tests, etc.—so they belong in globalEnv.

Action items (all in turbo.json):

  • Remove "CSP_POLICY" from the top‐level globalEnv and leave it in the website build task’s env (around lines 358–362).
  • Remove "NEXT_PUBLIC_API_V2_URL" and "BUILD_STANDALONE" from globalEnv, retaining them only in their respective task‐scoped env blocks (around lines 311–317 and 253–256).
  • Keep "NEXT_PUBLIC_WEBAPP_URL" and "NEXT_PUBLIC_WEBSITE_URL" in globalEnv, since they’re consumed by many packages.

This refactor isn’t required for correctness but will limit cache invalidations to the web‐only tasks.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 2d90d11 and 6726c36.

📒 Files selected for processing (2)
  • apps/web/next.config.js (2 hunks)
  • turbo.json (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js,jsx}

⚙️ CodeRabbit configuration file

Flag default exports and encourage named exports. Named exports provide better tree-shaking, easier refactoring, and clearer imports. Exempt main components like pages, layouts, and components that serve as the primary export of a module.

Files:

  • apps/web/next.config.js
⏰ Context from checks skipped due to timeout of 180000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Install dependencies / Yarn install & cache
🔇 Additional comments (2)
apps/web/next.config.js (2)

236-237: Including dev in the webpack callback signature — LGTM

This allows precise prod-only tweaks; no concerns here.


237-244: Use a mutable memory cache instead of freezing the config

Overriding config.cache to { type: "memory" } in production is a solid optimization to reduce filesystem churn, but freezing that object can lead to hard-to-debug TypeError: Cannot assign to read only property ... errors if Next.js or any plugin tries to augment cache settings later. JavaScript’s Object.freeze makes all properties non-writable, which provides no real benefit during build but can break downstream mutations.

• In apps/web/next.config.js (around lines 237–244), replace:

-      if (!dev) {
-        if (config.cache) {
-          config.cache = Object.freeze({
-            type: "memory",
-          });
-        }
-      }

with:

+      if (!dev && config.cache) {
+        config.cache = { type: "memory" };
+      }

Optional rollback guard (no code changes needed later):

-      if (!dev && config.cache) {
-        config.cache = { type: "memory" };
-      }
+      if (!dev && config.cache && process.env.NEXT_USE_MEMORY_CACHE !== "false") {
+        config.cache = { type: "memory" };
+      }

Please run a full production build (locally or in CI) both with and without this change (and with NEXT_USE_MEMORY_CACHE="false") to measure RSS/heap differences and build-time impacts, ensuring you don’t introduce OOM risk in your environment.

@keithwillcode
Copy link
Contributor

Looks like the dev build here took 18 minutes to fully be ready, which is right around existing times for dev builds.

@keithwillcode keithwillcode enabled auto-merge (squash) August 22, 2025 19:36
@keithwillcode keithwillcode merged commit d2f8076 into main Aug 22, 2025
82 of 85 checks passed
@keithwillcode keithwillcode deleted the perf/improve-build-memory-cache branch August 22, 2025 19:57
@github-actions
Copy link
Contributor

E2E results are ready!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core area: core, team members only foundation ready-for-e2e

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants