Skip to content

Commit

Permalink
fix hydration for default gitTimestamp component (#1119)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Dec 24, 2022
1 parent 5c617d4 commit c1867fd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-flies-check.md
@@ -0,0 +1,5 @@
---
'nextra-theme-docs': patch
---

fix hydration for default `gitTimestamp` component
7 changes: 5 additions & 2 deletions examples/blog/theme.config.jsx
Expand Up @@ -9,7 +9,10 @@ export default {
>
CC BY-NC 4.0
</abbr>{' '}
<time>{new Date().getFullYear()}</time> © Shu Ding.
<time dateTime={new Date().toISOString()}>
{new Date().getFullYear()}
</time>{' '}
© Shu Ding.
<a href="/feed.xml">RSS</a>
<style jsx>{`
a {
Expand All @@ -23,5 +26,5 @@ export default {
}
`}</style>
</small>
),
)
}
1 change: 0 additions & 1 deletion examples/swr-site/theme.config.tsx
Expand Up @@ -121,7 +121,6 @@ const config: DocsThemeConfig = {
);
},
},
gitTimestamp: ({ timestamp }) => <>Last updated on {timestamp.toString()}</>,
head() {
const config = useConfig();
const description =
Expand Down
12 changes: 7 additions & 5 deletions packages/nextra-theme-docs/src/constants.tsx
Expand Up @@ -55,11 +55,13 @@ export const DEFAULT_THEME: DocsThemeConfig = {
return (
<>
Last updated on{' '}
{timestamp.toLocaleDateString(locale, {
day: 'numeric',
month: 'long',
year: 'numeric'
})}
<time dateTime={timestamp.toISOString()}>
{timestamp.toLocaleDateString(locale, {
day: 'numeric',
month: 'long',
year: 'numeric'
})}
</time>
</>
)
},
Expand Down
27 changes: 19 additions & 8 deletions packages/nextra-theme-docs/src/index.tsx
Expand Up @@ -6,9 +6,17 @@ import { useRouter } from 'next/router'
import 'focus-visible'
import cn from 'clsx'
import { MDXProvider } from '@mdx-js/react'
import { useMounted } from 'nextra/hooks'

import './polyfill'
import { Head, NavLinks, Sidebar, SkipNavContent, Breadcrumb, Banner } from './components'
import {
Head,
NavLinks,
Sidebar,
SkipNavContent,
Breadcrumb,
Banner
} from './components'
import { getComponents } from './mdx-components'
import { ActiveAnchorProvider, ConfigProvider, useConfig } from './contexts'
import { DEFAULT_LOCALE } from './constants'
Expand Down Expand Up @@ -46,6 +54,7 @@ const Body = ({
children
}: BodyProps): ReactElement => {
const config = useConfig()
const mounted = useMounted()

if (themeContext.layout === 'raw') {
return <div className="nx-w-full nx-overflow-x-hidden">{children}</div>
Expand All @@ -56,13 +65,15 @@ const Body = ({
? new Date(timestamp)
: null

const gitTimestampEl = date ? (
<div className="nx-mt-12 nx-mb-8 nx-block nx-text-xs nx-text-gray-500 ltr:nx-text-right rtl:nx-text-left dark:nx-text-gray-400">
{renderComponent(config.gitTimestamp, { timestamp: date })}
</div>
) : (
<div className="nx-mt-16" />
)
const gitTimestampEl =
// Because a user's time zone may be different from the server page
mounted && date ? (
<div className="nx-mt-12 nx-mb-8 nx-block nx-text-xs nx-text-gray-500 ltr:nx-text-right rtl:nx-text-left dark:nx-text-gray-400">
{renderComponent(config.gitTimestamp, { timestamp: date })}
</div>
) : (
<div className="nx-mt-16" />
)

const content = (
<>
Expand Down

1 comment on commit c1867fd

@vercel
Copy link

@vercel vercel bot commented on c1867fd Dec 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.