Skip to content

Commit

Permalink
feat: Minimum workaround for SSG with next-i18next
Browse files Browse the repository at this point in the history
  • Loading branch information
michchan committed Mar 18, 2021
1 parent 790fb4c commit 0caac9e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 80 deletions.
5 changes: 0 additions & 5 deletions next.config.js

This file was deleted.

88 changes: 88 additions & 0 deletions pages/[locale]/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import Head from 'next/head'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { useTranslation } from 'next-i18next'

import styles from '../../styles/Home.module.css'
import { i18n } from '../../next-i18next.config'

export default function Home() {
const { t } = useTranslation('common')
const title = t('title')
return (
<div className={styles.container}>
<Head>
<title>{title}</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<main className={styles.main}>
<h1 className={styles.title}>
{/* Welcome to <a href="https://nextjs.org">Next.js!</a> */}
{title}
</h1>

<p className={styles.description}>
Get started by editing{' '}
<code className={styles.code}>pages/index.js</code>
</p>

<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h3>Documentation &rarr;</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>

<a href="https://nextjs.org/learn" className={styles.card}>
<h3>Learn &rarr;</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>

<a
href="https://github.com/vercel/next.js/tree/master/examples"
className={styles.card}
>
<h3>Examples &rarr;</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>

<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h3>Deploy &rarr;</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>

<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<img src="/vercel.svg" alt="Vercel Logo" className={styles.logo} />
</a>
</footer>
</div>
)
}

export async function getStaticPaths() {
return {
paths: i18n.locales.map(locale => ({ params: { locale } })),
fallback: false,
}
}

export async function getStaticProps({ params }) {
const locale = (params?.locale || i18n.defaultLocale)
return {
props: {
...await serverSideTranslations(locale, ['common']),
},
}
}
84 changes: 9 additions & 75 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,14 @@
import Head from 'next/head'
import styles from '../styles/Home.module.css'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { useTranslation } from 'next-i18next'
import { useRouter } from 'next/router'
import { i18n } from '../next-i18next.config'

export default function Home() {
const { t } = useTranslation('common')
const title = t('title')
return (
<div className={styles.container}>
<Head>
<title>{title}</title>
<link rel="icon" href="/favicon.ico" />
</Head>
const HomeFallback = () => {
const router = useRouter()
// Make sure we're in the browser
if (typeof window !== 'undefined')
// @TODO: Use locale on localStorage / browser preference
router.replace(`${i18n.defaultLocale}/${router.pathname}`)

<main className={styles.main}>
<h1 className={styles.title}>
{/* Welcome to <a href="https://nextjs.org">Next.js!</a> */}
{title}
</h1>

<p className={styles.description}>
Get started by editing{' '}
<code className={styles.code}>pages/index.js</code>
</p>

<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h3>Documentation &rarr;</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>

<a href="https://nextjs.org/learn" className={styles.card}>
<h3>Learn &rarr;</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>

<a
href="https://github.com/vercel/next.js/tree/master/examples"
className={styles.card}
>
<h3>Examples &rarr;</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>

<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h3>Deploy &rarr;</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>

<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<img src="/vercel.svg" alt="Vercel Logo" className={styles.logo} />
</a>
</footer>
</div>
)
}

export async function getStaticProps({ locale }) {
return {
props: {
...await serverSideTranslations(locale, ['common']),
},
}
return null
}

export default HomeFallback

0 comments on commit 0caac9e

Please sign in to comment.