Skip to content

Commit

Permalink
Fix: avoid mutating nextConfig (#598)
Browse files Browse the repository at this point in the history
* Fix: avoid mutating nextConfig

* Fix: pass defaulti18n to nextra/loader

* Refactor: make i18n mutation easier to read

* Refactor: migrate index to TS and create a default i18n

* Fix add index.ts tsup

* Revert migration to TS

* fix: wrong entry

* Update packages/nextra/src/index.js

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Co-authored-by: Shu Ding <g@shud.in>
Co-authored-by: Yixuan Xu <yixuanxu94@outlook.com>
Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>
  • Loading branch information
4 people committed Jul 27, 2022
1 parent f985a49 commit a0398e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .changeset/cool-planets-flow.md
@@ -0,0 +1,7 @@
---
'nextra': patch
'nextra-theme-blog': patch
'nextra-theme-docs': patch
---

fix: avoid mutating nextConfig
39 changes: 20 additions & 19 deletions packages/nextra/src/index.js
Expand Up @@ -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 ||= []
Expand All @@ -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
}
}
Expand All @@ -64,4 +64,5 @@ const nextra = (...args) =>
}
}
}

module.exports = nextra

0 comments on commit a0398e0

Please sign in to comment.