Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add performance polyfill for webpack dev mode
  • Loading branch information
andreiborza committed Sep 8, 2025
commit 46aaac9ea0dfdcfef4d0e97736990e5fb7b6dcf5
15 changes: 15 additions & 0 deletions packages/nextjs/rollup.npm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,20 @@ export default [
},
}),
),
...makeNPMConfigVariants(
makeBaseNPMConfig({
entrypoints: ['src/config/polyfills/perf_hooks.js'],

packageSpecificConfig: {
output: {
// Preserve the original file structure (i.e., so that everything is still relative to `src`)
entryFileNames: 'config/polyfills/[name].js',

// make it so Rollup calms down about the fact that we're combining default and named exports
exports: 'named',
},
},
}),
),
...makeOtelLoaders('./build', 'sentry-node'),
];
25 changes: 25 additions & 0 deletions packages/nextjs/src/config/polyfills/perf_hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Polyfill for Node.js perf_hooks module in edge runtime
// This mirrors the polyfill from packages/vercel-edge/rollup.npm.config.mjs

// Ensure performance global is available
if (typeof globalThis !== 'undefined' && globalThis.performance === undefined) {
globalThis.performance = {
timeOrigin: 0,
Copy link
Member

Choose a reason for hiding this comment

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

Maybe do:

 const timeOrigin = Date.now();
  return {
    timeOrigin,
    now: () => Date.now() - timeOrigin,
  };

I think anyone doing timing assertions in dev might see different values

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, updated.

now: function () {
return Date.now();
},
};
}

// Export the performance object for perf_hooks compatibility
export const performance = globalThis.performance || {
timeOrigin: 0,
now: function () {
return Date.now();
},
};

// Default export for CommonJS compatibility
export default {
performance,
};
26 changes: 26 additions & 0 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export function constructWebpackConfigFunction(

addOtelWarningIgnoreRule(newConfig);

// Add edge runtime polyfills when building for edge in dev mode
if (runtime === 'edge' && isDev) {
Copy link
Member

Choose a reason for hiding this comment

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

This will run for all next versions with dev mode correct? is this intended?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I'll have a look if I can scope this to 13 only.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated to only take 13 into account.

addEdgeRuntimePolyfills(newConfig, buildContext);
}

let pagesDirPath: string | undefined;
const maybePagesDirPath = path.join(projectDir, 'pages');
const maybeSrcPagesDirPath = path.join(projectDir, 'src', 'pages');
Expand Down Expand Up @@ -865,6 +870,27 @@ function addOtelWarningIgnoreRule(newConfig: WebpackConfigObjectWithModuleRules)
}
}

function addEdgeRuntimePolyfills(newConfig: WebpackConfigObjectWithModuleRules, buildContext: BuildContext): void {
const { webpack } = buildContext;

// Use ProvidePlugin to inject performance global only when accessed
newConfig.plugins = newConfig.plugins || [];
newConfig.plugins.push(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new (webpack as any).ProvidePlugin({
performance: [path.resolve(__dirname, 'polyfills', 'perf_hooks.js'), 'performance'],
}),
);

// Add module resolution aliases for problematic Node.js modules in edge runtime
newConfig.resolve = newConfig.resolve || {};
newConfig.resolve.alias = {
...newConfig.resolve.alias,
// Redirect perf_hooks imports to a polyfilled version
perf_hooks: path.resolve(__dirname, 'polyfills', 'perf_hooks.js'),
};
}

function _getModules(projectDir: string): Record<string, string> {
try {
const packageJson = path.join(projectDir, 'package.json');
Expand Down
Loading