-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix client console error on React 17 / crash on React 18 - Text conte…
…nt does not match server-rendered HTML (#568) fix client console error - Text content does not match server-rendered HTML
- Loading branch information
Dimitri POSTOLOV
authored
Jul 23, 2022
1 parent
c15f570
commit 488f737
Showing
6 changed files
with
79 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'nextra': patch | ||
--- | ||
|
||
fix client console error - Text content does not match server-rendered HTML |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { NextraPlugin, pageMapCache } from './plugin' | ||
import { MARKDOWN_EXTENSION_REGEX } from './constants' | ||
import { LoaderOptions, Nextra } from './types' | ||
|
||
const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] as const | ||
const MARKDOWN_EXTENSIONS = ['md', 'mdx'] as const | ||
|
||
const nextra: Nextra = (...args) => | ||
function withNextra(nextConfig = {}) { | ||
const nextraConfig = | ||
typeof args[0] === 'string' | ||
? { | ||
theme: args[0], | ||
themeConfig: args[1] as string | ||
} | ||
: args[0] | ||
|
||
const nextraPlugin = new NextraPlugin(nextraConfig) | ||
const { i18n, pageExtensions = DEFAULT_EXTENSIONS } = nextConfig | ||
|
||
if (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 { locales } = useRouter()` where `locales` will be `undefined` | ||
// To fix it we need to explicitly specify `i18n.locales` and `i18n.defaultLocale` | ||
nextConfig.i18n = { | ||
...i18n, | ||
locales: ['en-US'], | ||
defaultLocale: 'en-US' | ||
} | ||
} | ||
|
||
return { | ||
...nextConfig, | ||
pageExtensions: [...pageExtensions, ...MARKDOWN_EXTENSIONS], | ||
webpack(config, options) { | ||
config.plugins ||= [] | ||
config.plugins.push(nextraPlugin) | ||
|
||
config.module.rules.push({ | ||
test: MARKDOWN_EXTENSION_REGEX, | ||
use: [ | ||
options.defaultLoaders.babel, | ||
{ | ||
loader: 'nextra/loader', | ||
options: <LoaderOptions>{ | ||
...nextraConfig, | ||
locales: nextConfig.i18n?.locales, | ||
defaultLocale: nextConfig.i18n?.defaultLocale, | ||
pageMapCache | ||
} | ||
} | ||
] | ||
}) | ||
|
||
if (typeof nextConfig.webpack === 'function') { | ||
return nextConfig.webpack(config, options) | ||
} | ||
|
||
return config | ||
} | ||
} | ||
} | ||
|
||
module.exports = nextra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters