Duplicate SWC Transform For Use Client Components When React Compiler Is Disabled
Next.js with Turbopack passes the same filename to SWC for multiple compilation layers (e.g., RSC and client), causing duplicate entries in SWC AST output and preventing plugins from distinguishing contexts.
Next.js's compilation workflow transforms the same module (such as a use client component) in multiple layers (RSC, client, etc.), but the filename passed to SWC's TransformContext does not include layer information, leading to identical filenames and ambiguity for SWC plugins.
1. pnpm i
2. npm run build
3. Inspect .swc-plugin-stage-output directory to observe duplicate identical filenames.
Environment: Next.js 16.1.6, React 19.2.3, Turbopack enabled, React Compiler disabled.
Fixing Code Block
export function addLayerToFilename(filename: string, layer: string | undefined): string {
if (!layer) return filename;
const separator = filename.includes('?') ? '&' : '?';
return `${filename}${separator}layer=${layer}`;
}
// In Next.js SWC transform call site (e.g., next/src/build/swc/index.ts),
// apply the function before passing filename to SWC:
// const transformedFilename = addLayerToFilename(filename, options.layer);
// Then use transformedFilename as the filename in TransformContext.
The fix appends a layer query parameter (e.g., ?layer=app-rsc or &layer=app-client) to the filename before handing it to SWC. This makes filenames unique per compilation layer, allowing SWC plugins to distinguish between duplicate entries. The function handles both cases of existing query strings and no query strings.
Edge Case Audit
SWC plugins that rely on exact filename matches will need to be updated to parse the new layer query parameter. This change may not be compatible with older Next.js versions or external tooling that assumes filenames without query parameters. Rollback: remove the added layer parameter or revert the change in the transform call site. Test across all layer types (RSC, client, edge) to ensure no unintended side effects.