diff --git a/.changeset/cool-planets-flow.md b/.changeset/cool-planets-flow.md new file mode 100644 index 0000000000..dfbc21cbef --- /dev/null +++ b/.changeset/cool-planets-flow.md @@ -0,0 +1,7 @@ +--- +'nextra': patch +'nextra-theme-blog': patch +'nextra-theme-docs': patch +--- + +fix: avoid mutating nextConfig diff --git a/packages/nextra/src/index.js b/packages/nextra/src/index.js index 524e26409b..6c65911a3c 100644 --- a/packages/nextra/src/index.js +++ b/packages/nextra/src/index.js @@ -4,37 +4,37 @@ import { DEFAULT_LOCALE, MARKDOWN_EXTENSION_REGEX } from './constants' const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] const MARKDOWN_EXTENSIONS = ['md', 'mdx'] -const nextra = (...args) => +const nextra = (...config) => function withNextra(nextConfig = {}) { const nextraConfig = - typeof args[0] === 'string' + typeof config[0] === "string" ? { - theme: args[0], - themeConfig: args[1] + theme: config[0], + themeConfig: config[1] } - : args[0] + : config[0] const nextraPlugin = new NextraPlugin(nextraConfig) - const { i18n, pageExtensions = DEFAULT_EXTENSIONS } = nextConfig + const { pageExtensions = DEFAULT_EXTENSIONS } = nextConfig - if (i18n?.locales) { + if (nextConfig.i18n?.locales) { console.log( '[nextra] You have Next.js i18n enabled, read here https://nextjs.org/docs/advanced-features/i18n-routing for the docs.' ) - } else if (!i18n?.defaultLocale) { - // If `i18n.locales` and `i18n.defaultLocale` were not specified, - // client will receive error - Text content does not match server-rendered HTML. - // Due to `const { locale } = useRouter()` where `locale` will be `undefined` - // To fix it we need to explicitly specify `i18n.locales` and `i18n.defaultLocale` - nextConfig.i18n = { - ...i18n, - locales: [DEFAULT_LOCALE], - defaultLocale: DEFAULT_LOCALE - } + } + + // If `i18n.locales` and `i18n.defaultLocale` were not specified, + // client will receive error - Text content does not match server-rendered HTML. + // Due to `const { locale } = useRouter()` where `locale` will be `undefined` + // To fix it we need to explicitly specify `i18n.locales` and `i18n.defaultLocale` + const i18n = { + locales: nextConfig.i18n?.locales || [DEFAULT_LOCALE], + defaultLocale: nextConfig.i18n?.defaultLocale || DEFAULT_LOCALE } return { ...nextConfig, + i18n, pageExtensions: [...pageExtensions, ...MARKDOWN_EXTENSIONS], webpack(config, options) { config.plugins ||= [] @@ -48,8 +48,8 @@ const nextra = (...args) => loader: 'nextra/loader', options: { ...nextraConfig, - locales: nextConfig.i18n?.locales, - defaultLocale: nextConfig.i18n?.defaultLocale, + locales: i18n.locales, + defaultLocale: i18n.defaultLocale, pageMapCache } } @@ -64,4 +64,5 @@ const nextra = (...args) => } } } + module.exports = nextra