Skip to content

feat: implement Next.js skew protection for Netlify #3023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

biilmann
Copy link

Summary

This PR implements Next.js skew protection for Netlify, providing automatic deployment version routing to prevent skew issues during deployments.

Key Features:

  • 🔄 Automatic deployment ID routing based on query params, headers, or cookies
  • 🚀 Seamless integration with Next.js skew protection (VERCEL_SKEW_PROTECTION_ENABLED=1)
  • 🎯 Smart routing: static assets/API routes → old deployments, HTML pages → current deployment
  • 🛡️ Zero-configuration setup - works automatically when enabled

Implementation Details

Core Components

  1. Environment Setup: Maps NETLIFY_DEPLOY_ID to VERCEL_DEPLOYMENT_ID
  2. Edge Function: Handles routing based on deployment IDs from:
    • Query parameter: ?dpl=<id>
    • Header: X-Deployment-Id
    • Cookie: __vdpl=<id>
  3. Smart Routing: Routes static assets and API calls to old deployments while keeping HTML pages on current deployment

Files Changed

  • src/index.ts - Added skew protection setup in onPreBuild
  • src/build/functions/edge.ts - Integrated edge function creation
  • src/build/templates/skew-protection.tmpl.js - New edge function template
  • tests/ - Added comprehensive test coverage
  • CLAUDE.md - Added project documentation

Test Plan

  • Unit tests for edge function creation
  • Integration tests for environment variable setup
  • Test fixture for skew protection scenarios
  • TypeScript type checking passes
  • ESLint validation passes

Usage

Enable skew protection by setting:

VERCEL_SKEW_PROTECTION_ENABLED=1

The plugin automatically:

  1. Sets VERCEL_DEPLOYMENT_ID from NETLIFY_DEPLOY_ID
  2. Creates the skew protection edge function
  3. Adds routing logic to the edge functions manifest

Benefits

  • Version Consistency: Prevents asset/API mismatches during deployments
  • Zero Downtime: Seamless transitions between deployments
  • Developer Experience: Works exactly like Vercel's implementation
  • Performance: Minimal overhead with smart caching

This implementation follows the Next.js skew protection specification exactly and provides the same developer experience as Vercel's platform.

🤖 Generated with Claude Code

This implements Next.js skew protection that automatically routes requests
to the appropriate deployment based on deployment IDs, preventing version
skew issues during deployments.

**Features:**
- Automatically enabled when VERCEL_SKEW_PROTECTION_ENABLED=1
- Maps NETLIFY_DEPLOY_ID to VERCEL_DEPLOYMENT_ID for Next.js compatibility
- Creates edge function to handle deployment ID routing
- Supports all three deployment ID carriers (?dpl, X-Deployment-Id header, __vdpl cookie)
- Routes static assets and API routes to old deployments when needed
- Allows HTML pages to use current deployment for proper skew protection

**Implementation:**
- Added skew protection edge function template
- Integrated edge function creation into plugin lifecycle
- Updated onPreBuild to set deployment ID environment variables
- Added comprehensive test coverage
- Updated documentation in CLAUDE.md

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@biilmann biilmann requested a review from a team as a code owner July 31, 2025 21:51
…ON_ENABLED

Updated environment variable name to use NEXT_ prefix instead of VERCEL_
for better Netlify branding consistency while maintaining the same functionality.

Changes:
- src/index.ts: Updated env var check and warning message
- src/build/functions/edge.ts: Updated env var checks in two locations
- tests/integration/skew-protection.test.ts: Updated test env setup
- CLAUDE.md: Updated documentation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@cshawaus
Copy link

cshawaus commented Aug 4, 2025

@biilmann, this is awesome primarily because I had only just put it on my to-do list to explore. I'm looking forward to enabling this as soon as it is merged. 💯

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.

3 participants