Skip to content

Conversation

@swarnava
Copy link
Member

@swarnava swarnava commented Oct 23, 2025

When a developer used "use server" directive in a Pages Router file, the build did not fail despite it being unsupported. This could lead to runtime errors when importing APIs from dist that are gated behind the cacheComponents flag, since Server Actions are only supported in the App Router.

- Add validation in getPagesPageStaticInfo to throw error when 'use server' is used
- Server Actions are only supported in App Router
- 'use client' remains allowed in Pages Router as it only marks client boundaries
- Add unit tests to verify validation behavior
- Add integration tests to verify build errors and error messages
…rectives

The PARSE_PATTERN regex is used as an optimization to skip parsing files
that don't contain certain keywords. Without including 'use\s' in the pattern,
files with only 'use server' or 'use client' directives (and no other keywords
like getStaticProps, export const, etc.) would return early and skip AST parsing.

This meant the 'use server' validation would never run for these files,
silently allowing the invalid directive through.

Changes:
- Add 'use\s' to PARSE_PATTERN to match 'use server' and 'use client'
- The \s (whitespace) requirement prevents false matches on variable names
  like useState, useEffect, or useServer
- Add test to verify validation works for files with only directives
- Add test to verify no false positives on React hooks and 'use' variables
@icyJoseph
Copy link
Collaborator

This wouldn't work for nested components, right? or?

I think import 'server-only' in your server actions, if you still support Pages Router would be the way to go 🤔

@swarnava
Copy link
Member Author

@icyJoseph true, I was thinking how this incident should've failed during build time.

…endation

- Document that 'use server' validation only checks page files, not nested components
- Recommend using 'import server-only' for comprehensive protection across all components
- Add comment explaining PARSE_PATTERN regex and why it includes 'use\s'
@swarnava swarnava closed this Oct 24, 2025
@swarnava swarnava deleted the fix-validate-use-server-in-pages-router branch October 24, 2025 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants