-
-
Notifications
You must be signed in to change notification settings - Fork 764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor simple example to typescript #1975
Changes from 9 commits
0f062b8
b4d3ba8
618b612
a607fc5
d18e815
6da54d0
eeb5333
f155b3b
35bbfc4
5803b59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
// used for SSR (getServerSideProps) | ||
// const path = require('path') | ||
// const localePath = path.resolve('./public/locales') | ||
|
||
// @link https://github.com/i18next/next-i18next | ||
module.exports = { | ||
// https://www.i18next.com/overview/configuration-options#logging | ||
debug: process.env.NODE_ENV === 'development', | ||
i18n: { | ||
defaultLocale: 'en', | ||
locales: ['en', 'de'], | ||
}, | ||
// localePath, | ||
reloadOnPrerender: process.env.NODE_ENV === 'development', | ||
/** | ||
* @link https://github.com/i18next/next-i18next#6-advanced-configuration | ||
* | ||
localePath: typeof window === 'undefined' ? | ||
require('path').resolve('./my-custom/path'): | ||
'/public/my-custom/path', | ||
*/ | ||
// serializeConfig: false, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const pc = require('picocolors') | ||
|
||
const nextUtilsConfig = () => { | ||
const trueEnv = ['true', '1', 'yes'] | ||
const esmExternals = trueEnv.includes( | ||
process.env?.NEXTJS_ESM_EXTERNALS ?? 'false' | ||
) | ||
const tsconfigPath = process.env.NEXTJS_TSCONFIG_PATH | ||
? process.env.NEXTJS_TSCONFIG_PATH | ||
: './tsconfig.json' | ||
|
||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`${pc.green( | ||
'warn -' | ||
)} experimental.esmExternals is ${esmExternals ? 'enabled': 'disabled'}` | ||
) | ||
return { | ||
esmExternals, | ||
tsconfigPath, | ||
} | ||
} | ||
|
||
module.exports = { | ||
loadCustomBuildParams: nextUtilsConfig, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
const { i18n } = require('./next-i18next.config') | ||
// @ts-check | ||
const { i18n } = require('./next-i18next.config.js') | ||
|
||
module.exports = { | ||
// You can remove the following 2 lines when integrating our example. | ||
const { loadCustomBuildParams } = require('./next-utils.config') | ||
adrai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { esmExternals = false, tsconfigPath } = loadCustomBuildParams() | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
experimental: { | ||
esmExternals, // https://nextjs.org/blog/next-11-1#es-modules-support | ||
}, | ||
i18n, | ||
reactStrictMode: true, | ||
typescript: { | ||
tsconfigPath, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import type { AppProps } from 'next/app' | ||
import { appWithTranslation } from 'next-i18next' | ||
// import nextI18NextConfig from '../next-i18next.config.js' | ||
|
||
const MyApp = ({ Component, pageProps }) => <Component {...pageProps} /> | ||
const MyApp = ({ Component, pageProps }: AppProps) => ( | ||
<Component {...pageProps} /> | ||
) | ||
|
||
// https://github.com/i18next/next-i18next#unserialisable-configs | ||
export default appWithTranslation(MyApp/*, nextI18NextConfig */) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import Link from 'next/link' | ||
import { useRouter } from 'next/router' | ||
import type { GetStaticProps, InferGetStaticPropsType } from 'next' | ||
|
||
import { useTranslation, Trans } from 'next-i18next' | ||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations' | ||
|
||
import { Header } from '../components/Header' | ||
import { Footer } from '../components/Footer' | ||
|
||
const Homepage = () => { | ||
type Props = { | ||
// Add custom props here | ||
} | ||
|
||
const Homepage = (_props: InferGetStaticPropsType<typeof getStaticProps>) => { | ||
|
||
const router = useRouter() | ||
const { t } = useTranslation('common') | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const onToggleLanguageClick = (newLocale) => { | ||
const onToggleLanguageClick = (newLocale: string) => { | ||
const { pathname, asPath, query } = router | ||
router.push({ pathname, query }, asPath, { locale: newLocale }) | ||
} | ||
|
@@ -77,10 +82,10 @@ const Homepage = () => { | |
) | ||
} | ||
|
||
// export const getServerSideProps = async ({ locale }) => ({ | ||
export const getStaticProps = async ({ locale }) => ({ | ||
// or getServerSideProps: GetServerSideProps<Props> = async ({ locale }) | ||
export const getStaticProps: GetStaticProps<Props> = async ({ locale }) => ({ | ||
props: { | ||
...await serverSideTranslations(locale, ['common', 'footer']), | ||
...await serverSideTranslations(locale ?? 'en', ['common', 'footer']), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really necessary? Shouldn't it fallback automatically? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is not the case, we may move this check down here and fallback to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. locale is always there (except maybe for nextjs prior to when locale was added) I don't really want to the throw Error in there. I feel it's enough with the ??. Second for the sake of the simple example I don't really want to require/import the next-i18next.config.js to get the fallback. At least not yet, cause it wasn't working in olders version without the localePath trick. I mean all those next-i18next, nextjs legacy but supported. At least If I remember well. That said a more advanced example should be great. One that typechecks the keys and handle properly localePath in all scenarios (getStatic, getServer, app and csr pages + monorepo), but for that the example will look a bit like bryanprimus/layout-reproduce-i18next#1. Which will make the simple example less simple. So for now, I'm just focusing on whatever help me for publishing in a safe way. Doing so I saw a lot more to do in the codebase and those little might not be necessary anymore once I've cleaned'up. (not sure I can but let's hope) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean in the library code, not in the example code... check the links in my previous comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tldr; I would remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes. To be sure we're talking about the same Here nextjs type the locale as string | undefined, and I want it as string. Even if we would accept an undefined as first parameter to serverSideTranslations and do the job there, I feel would worsen the signature (I mean having optional as first param is not living well, ie I don't feel neither than nextjs is at fault here. It's good that the developer handles the fallback. I mean in how next-i18next works right now. And mysefl I tend to create my own function (that will deal with more) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
}, | ||
}) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"baseUrl": ".", | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"incremental": true, | ||
"paths": { | ||
"@/backend": ["./src/backend/index"], | ||
"@/lib": ["./src/lib/index"], | ||
"@belgattitude/http-exception": [ | ||
"../../packages/http-exception/src/index" | ||
], | ||
"@belgattitude/http-exception/serializer": [ | ||
"../../packages/http-exception/src/serializer/index" | ||
] | ||
} | ||
}, | ||
"include": [ | ||
"next-env.d.ts", | ||
"**/*.ts", | ||
"**/*.tsx", | ||
"**/*.js", | ||
"**/*.jsx", | ||
"**/*.cjs", | ||
"**/*.mjs" | ||
], | ||
"exclude": ["**/node_modules", "**/.*/"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is the part that I want to merge into the peer-deps change and new publishing. It will help me cover edge cases