-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(nextjs): Add edge polyfills for nextjs-13 in dev mode #17488
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
Changes from 4 commits
ae5fa5d
330f172
46aaac9
5bb46ea
5202ba6
5250cba
4b8db28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
export function middleware() { | ||
// Basic middleware to ensure that the build works with edge runtime | ||
return NextResponse.next(); | ||
} |
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, | ||
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(); | ||
}, | ||
}; | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Default export for CommonJS compatibility | ||
export default { | ||
performance, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,11 @@ export function constructWebpackConfigFunction( | |
|
||
addOtelWarningIgnoreRule(newConfig); | ||
|
||
// Add edge runtime polyfills when building for edge in dev mode | ||
if (runtime === 'edge' && isDev) { | ||
|
||
addEdgeRuntimePolyfills(newConfig, buildContext); | ||
} | ||
|
||
let pagesDirPath: string | undefined; | ||
const maybePagesDirPath = path.join(projectDir, 'pages'); | ||
const maybeSrcPagesDirPath = path.join(projectDir, 'src', 'pages'); | ||
|
@@ -865,6 +870,24 @@ function addOtelWarningIgnoreRule(newConfig: WebpackConfigObjectWithModuleRules) | |
} | ||
} | ||
|
||
function addEdgeRuntimePolyfills(newConfig: WebpackConfigObjectWithModuleRules, buildContext: BuildContext): void { | ||
// Use ProvidePlugin to inject performance global only when accessed | ||
newConfig.plugins = newConfig.plugins || []; | ||
newConfig.plugins.push( | ||
new buildContext.webpack.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'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,7 +99,13 @@ export function getBuildContext( | |
distDir: '.next', | ||
...materializedNextConfig, | ||
} as NextConfigObject, | ||
webpack: { version: webpackVersion, DefinePlugin: class {} as any }, | ||
webpack: { | ||
version: webpackVersion, | ||
DefinePlugin: class {} as any, | ||
ProvidePlugin: class { | ||
constructor(public definitions: Record<string, any>) {} | ||
} as any, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Can we use a more precise type? or type this with unknown instead of any please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are just test fixtures, I don't think it's necessary to be more precise here. |
||
}, | ||
defaultLoaders: true, | ||
totalPages: 2, | ||
isServer: buildTarget === 'server' || buildTarget === 'edge', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe do:
I think anyone doing timing assertions in dev might see different values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, updated.