Skip to content

Comments

Apply optimization for unused actions (#69178)#4

Open
MitchLewis930 wants to merge 1 commit intopr_034_beforefrom
pr_034_after
Open

Apply optimization for unused actions (#69178)#4
MitchLewis930 wants to merge 1 commit intopr_034_beforefrom
pr_034_after

Conversation

@MitchLewis930
Copy link

PR_034

@greptile-apps
Copy link

greptile-apps bot commented Jan 29, 2026

Greptile Overview

Greptile Summary

This PR implements tree-shaking optimization for server actions in Next.js webpack builds, eliminating unused exported actions from the bundle to reduce size.

Key Changes:

  • Added tracking of which actions are actually imported/used per entry point and runtime
  • Filter out unused actions before generating action entries
  • Inline actions (prefixed with $$ACTION_) are always preserved
  • CJS imports include all actions (wildcarded) since tree-shaking isn't possible
  • ESM imports track specific imported identifiers for precise filtering

Test Coverage:

  • Basic scenario: unused exported actions excluded from manifest
  • Re-exports: tree-shaking works through named and namespace re-exports
  • Shared modules: same action module used by multiple pages optimized correctly
  • Mixed modules: CJS imports preserve all actions, ESM imports tree-shake
  • Edge and Node runtimes both covered

Turbopack Status:
All new tests documented as failing in turbopack-build-tests-manifest.json (expected, feature not yet implemented for Turbopack)

Confidence Score: 4/5

  • Safe to merge with comprehensive test coverage, though Turbopack implementation is pending
  • The implementation is well-structured with proper tracking and filtering logic. Comprehensive test coverage validates basic, re-export, shared module, and mixed module scenarios. The main concern is the complexity of the module graph traversal logic which could have edge cases, but the test suite appears thorough.
  • packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts - verify the traversal logic handles all import patterns correctly

Important Files Changed

Filename Overview
packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts Implements action tree-shaking optimization to track and filter unused server actions, reducing bundle size by only including actions that are actually imported and used
test/production/app-dir/actions-tree-shaking/_testing/utils.ts New test utility functions to read action manifest and calculate action counts per route/layer for validation
test/production/app-dir/actions-tree-shaking/basic/basic.test.ts Tests that unused actions are excluded from manifest while used actions remain
test/turbopack-build-tests-manifest.json Documents that new tree-shaking tests are failing in Turbopack (expected, feature not implemented there yet)

Sequence Diagram

sequenceDiagram
    participant WP as Webpack Compilation
    participant FCEP as FlightClientEntryPlugin
    participant MG as Module Graph
    participant AM as Action Manifest

    Note over WP,FCEP: Server Entry Processing
    WP->>FCEP: processServerEntry(entryName, dependencies)
    FCEP->>MG: Traverse module graph from entry
    MG-->>FCEP: Collect client components & CSS imports
    
    Note over FCEP,MG: Track Used Actions (New)
    FCEP->>MG: filterUsedActions(module, importedIds)
    loop For each dependency
        MG->>FCEP: Get dependency.ids (imported identifiers)
        alt Is action server layer entry
            FCEP->>FCEP: setUsedActionsInEntry(entryName, modResource, ids)
        else Continue traversing
            FCEP->>MG: Recurse to next module
        end
    end

    Note over WP,FCEP: Client Entry Processing
    WP->>FCEP: injectClientEntryAndSSRModules()
    FCEP->>MG: collectClientActionsFromDependencies(entryName)
    MG-->>FCEP: Return all action imports
    
    Note over FCEP,MG: Filter Unused Actions
    loop For each action module
        FCEP->>FCEP: getUsedActionsInEntry(entryName, filePath)
        alt Has usedActionNames and not wildcard
            FCEP->>FCEP: Filter to only used action names
        else No usage tracked
            FCEP->>FCEP: Remove from actions map (tree-shaken)
        end
    end

    Note over FCEP,AM: Create Action Entries
    FCEP->>WP: injectActionEntry(filteredActions, entryName)
    FCEP->>AM: Update action manifest with remaining actions
    WP-->>FCEP: Action entry created
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

4 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

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