diff --git a/examples/with-sentry/next.config.js b/examples/with-sentry/next.config.js index 272fef9c62884..7d5c90f380c5a 100644 --- a/examples/with-sentry/next.config.js +++ b/examples/with-sentry/next.config.js @@ -21,6 +21,7 @@ const COMMIT_SHA = VERCEL_BITBUCKET_COMMIT_SHA process.env.SENTRY_DSN = SENTRY_DSN +const basePath = '' module.exports = withSourceMaps({ serverRuntimeConfig: { @@ -63,12 +64,12 @@ module.exports = withSourceMaps({ include: '.next', ignore: ['node_modules'], stripPrefix: ['webpack://_N_E/'], - urlPrefix: '~/_next', + urlPrefix: `~${basePath}/_next`, release: COMMIT_SHA, }) ) } - return config }, + basePath, }) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index acfece465c8b5..18df99e10f9af 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -1121,10 +1121,68 @@ export default async function getBaseWebpackConfig( // Enable webpack 5 caching if (config.experimental.unstable_webpack5cache) { - webpackConfig.cache = { + const nextPublicVariables = Object.keys(process.env).reduce( + (prev: string, key: string) => { + if (key.startsWith('NEXT_PUBLIC_')) { + return `${prev}|${key}=${process.env[key]}` + } + return prev + }, + '' + ) + const nextEnvVariables = Object.keys(config.env).reduce( + (prev: string, key: string) => { + return `${prev}|${key}=${config.env[key]}` + }, + '' + ) + + const configVars = JSON.stringify({ + crossOrigin: config.crossOrigin, + pageExtensions: config.pageExtensions, + trailingSlash: config.trailingSlash, + modern: config.experimental.modern, + buildActivity: config.devIndicators.buildActivity, + autoPrerender: config.devIndicators.autoPrerender, + plugins: config.experimental.plugins, + reactStrictMode: config.reactStrictMode, + reactMode: config.experimental.reactMode, + optimizeFonts: config.experimental.optimizeFonts, + optimizeImages: config.experimental.optimizeImages, + scrollRestoration: config.experimental.scrollRestoration, + basePath: config.basePath, + pageEnv: config.experimental.pageEnv, + excludeDefaultMomentLocales: config.future.excludeDefaultMomentLocales, + assetPrefix: config.assetPrefix, + target, + reactProductionProfiling, + }) + + const cache: any = { type: 'filesystem', + // Includes: + // - Next.js version + // - NEXT_PUBLIC_ variable values (they affect caching) TODO: make this module usage only + // - next.config.js `env` key + // - next.config.js keys that affect compilation + version: `${process.env.__NEXT_VERSION}|${nextPublicVariables}|${nextEnvVariables}|${configVars}`, + buildDependencies: { + config: [], + }, cacheDirectory: path.join(dir, '.next', 'cache', 'webpack'), } + + // Adds `next.config.js` as a buildDependency when custom webpack config is provided + if (config.webpack && config.configFile) { + cache.buildDependencies = { + config: [config.configFile], + } + } + + webpackConfig.cache = cache + + // @ts-ignore TODO: remove ignore when webpack 5 is stable + webpackConfig.optimization.realContentHash = false } } diff --git a/packages/next/next-server/server/config.ts b/packages/next/next-server/server/config.ts index c9eefdca419a6..ac652280fb9f2 100644 --- a/packages/next/next-server/server/config.ts +++ b/packages/next/next-server/server/config.ts @@ -277,7 +277,11 @@ export default function loadConfig( ) } - return assignDefaults({ configOrigin: CONFIG_FILE, ...userConfig }) + return assignDefaults({ + configOrigin: CONFIG_FILE, + configFile: path, + ...userConfig, + }) } else { const configBaseName = basename(CONFIG_FILE, extname(CONFIG_FILE)) const nonJsPath = findUp.sync(