DCE and Edge Runtime

VerifiedSafe

Safe conditional require patterns and edge runtime constraints, covering webpack DCE and Node-only imports.

Sby Skills Guide Bot
DevelopmentAdvanced
8/29/2026
Claude Code
#webpack#dead-code-elimination#edge-runtime#nextjs#typescript

Recommended for

Automated summary

This skill documents webpack dead-code-elimination-safe require() patterns and edge runtime constraints inside the Next.js repository.

Strengths

  • Pinpoints which branching shapes (if/else) webpack can actually eliminate and which fail (early-return guards, bare if without else)
  • Explains the interaction between DCE and TypeScript definite assignment when branching on process.env
  • Warns that NEXT_RUNTIME is not a feature flag and shows how to force flags false for edge builds in define-env.ts
  • Gives exact verification commands (pnpm test-start-webpack on standalone.test.ts) and flags the NEXT_SKIP_ISOLATE=1 pitfall

Limitations

  • Repository-internal: assumes familiarity with define-env.ts, app-page.ts and entry-base.ts
  • Covers only webpack and Turbopack, with no guidance for other bundlers
  • Verification steps rely on Next.js-specific e2e tests that do not transfer to other projects
When to use it

Use it when editing conditional require() calls, Node-only imports, or edge runtime branching inside the Next.js codebase.

When not to use it

Skip it for ordinary Next.js application code, where you are not touching build templates or define-env.ts.

Security analysis

Safe
Quality score88/100

This is an internal technical documentation skill about webpack dead-code elimination, edge runtime constraints, and Next.js internals. It contains no executable instructions, credential handling, network calls, destructive actions, or obfuscated payloads, so the listing carries no meaningful execution risk.

No concerns found

How this analysis is made

Examples

DCE-safe node:stream require
Add a DCE-safe conditional require for node:stream in server/stream-utils, branching with if/else on process.env.__NEXT_USE_NODE_STREAMS so webpack can drop the dead path.
Force flag false for edge
In define-env.ts, force __NEXT_USE_NODE_STREAMS to false when isEdgeServer is true so edge builds don't try to resolve node:stream.
Fix TS definite assignment
This switcher module errors with 'variable used before being assigned' because it uses two independent if blocks on process.env.X — convert it to a single if/else that TypeScript and webpack DCE both accept.

name: dce-edge description: > DCE-safe require() patterns and edge runtime constraints. Use when writing conditional require() calls, guarding Node-only imports (node:stream etc.), or editing define-env-plugin.ts / app-render / stream-utils for edge builds. Covers if/else branching for webpack DCE, TypeScript definite assignment, the NEXT_RUNTIME vs real feature flag distinction, and forcing flags false for edge in define-env.ts. metadata: internal: true

DCE + Edge

Use this skill when changing conditional require() paths, Node-only imports, or edge/runtime branching.

DCE-Safe require() Pattern

Webpack only DCEs a require() when it sits inside the dead branch of an if/else whose condition DefinePlugin can evaluate at compile time.

// CORRECT - webpack can eliminate the dead branch
if (process.env.__NEXT_USE_NODE_STREAMS) {
  require('node:stream')
} else {
  // web path
}

What does NOT work:

  • Early-return/throw guards: webpack doesn't do control-flow analysis for throws/returns, so the require() is still traced.
  • Bare if without else: works for inline node:* specifiers but NOT for require('./some-module') that pulls a new file into the module graph.

Always test edge changes with pnpm test-start-webpack on test/e2e/app-dir/app/standalone.test.ts (has edge routes), not with NEXT_SKIP_ISOLATE=1 which skips the full webpack compilation.

TypeScript + DCE Interaction

Use if/else (not two independent if blocks) when assigning a variable conditionally on process.env.X. TypeScript cannot prove exhaustiveness across if (flag) { x = a }; if (!flag) { x = b } and will error with "variable used before being assigned". The if/else pattern satisfies both TypeScript (definite assignment) and webpack DCE.

Compile-Time Switcher Pattern

Platform-specific code (node vs web) can use a single .ts switcher module that conditionally require()s either .node.ts or .web.ts into a typed variable, then re-exports the shared runtime API as named exports. Keep the branch as if/else so DefinePlugin can dead-code-eliminate the unused require(). Keep shared types canonical in .node.ts, with .web.ts importing them via import type and the switcher re-exporting types as needed. Examples: stream-ops.ts and debug-channel-server.ts.

NEXT_RUNTIME Is Not a Feature Flag

In user-project webpack server compilers, process.env.NEXT_RUNTIME is inlined to 'nodejs'. Guarding Node-only require('node:*') paths with NEXT_RUNTIME === 'nodejs' does not prune anything. For feature-gated codepaths, guard on the real feature define (e.g. process.env.__NEXT_USE_NODE_STREAMS).

Edge Runtime Constraints

Edge routes do NOT use pre-compiled runtime bundles. They are compiled by the user's webpack/Turbopack, so define-env.ts controls DCE. Feature flags that gate node:* imports must be forced to false for edge builds in define-env.ts (isEdgeServer ? false : flagValue), otherwise webpack will try to resolve node:stream etc. and fail.

app-page.ts Template Gotchas

  • app-page.ts is a build template compiled by the user's bundler. Any require() in this file is traced by webpack/turbopack at next build time. You cannot require internal modules with relative paths because they won't be resolvable from the user's project. Instead, export new helpers from entry-base.ts and access them via entryBase.* in the template.
  • Template helpers should stay out of RenderResult. If app-page.ts needs a Node-stream-only utility, prefer a small dedicated helper module in server/stream-utils/ (with DCE-safe if/else + require()).

Verification

  • Validate edge bundling regressions with pnpm test-start-webpack test/e2e/app-dir/app/standalone.test.ts
  • For module-resolution/build-graph fixes, verify without NEXT_SKIP_ISOLATE=1

Related Skills

  • $flags - flag wiring (config/schema/define-env/runtime env)
  • $react-vendoring - entry-base boundaries and vendored React
  • $runtime-debug - reproduction and verification workflow

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/dce-and-edge-runtime-mte1lklc/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/dce-and-edge-runtime-mte1lklc' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/dce-and-edge-runtime-mte1lklc/download' --output '.skill-downloads/dce-and-edge-runtime-mte1lklc/SKILL.md.part' && mv '.skill-downloads/dce-and-edge-runtime-mte1lklc/SKILL.md.part' '.skill-downloads/dce-and-edge-runtime-mte1lklc/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/dce-and-edge-runtime-mte1lklc/SKILL.md' && test -s '.skill-downloads/dce-and-edge-runtime-mte1lklc/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/dce-edge' && cp -R '.skill-downloads/dce-and-edge-runtime-mte1lklc' '.claude/skills/dce-edge'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/dce-and-edge-runtime-mte1lklc'

openskills read 'dce-and-edge-runtime-mte1lklc'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/dce-and-edge-runtime-mte1lklc/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills