Skip to content
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

Add reloadOnPrerender option to reload resources in serverSideTranslations so that developers don't have to restart their server when making changes to their translation JSON files #1359

Merged
merged 6 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ export const getStaticProps = async ({ locale }) => ({
});
```

#### Reloading Resources in Development

Because resources are loaded once when the server is started, any changes made to your translation JSON files in development will not be loaded until the server is restarted.

In production this does not tend to be an issue, but in development you may want to see updates to your translation JSON files without having to restart your development server each time. To do this, set the `reloadOnPrerender` config option to `true`.

This option will reload your translations whenever `serverSideTranslations` is called (in `getStaticProps` or `getServerSideProps`). If you are using `serverSideTranslations` in `getServerSideProps`, it is recommended to disable `reloadOnPrerender` in production environments as to avoid reloading resources on each server call.
isaachinman marked this conversation as resolved.
Show resolved Hide resolved

#### Options

| Key | Default value |
Expand All @@ -221,6 +229,7 @@ export const getStaticProps = async ({ locale }) => ({
| `localeExtension` | `'json'` |
| `localePath` | `'./public/locales'` |
| `localeStructure` | `'{{lng}}/{{ns}}'` |
| `reloadOnPrerender` | `false` |
| `serializeConfig` | `true` |
| `strictMode` | `true` |
| `use` (for plugins) | `[]` |
Expand Down
1 change: 1 addition & 0 deletions src/config/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const defaultConfig = {
react: {
useSuspense: true,
},
reloadOnPrerender: false,
serializeConfig: true,
strictMode: true,
use: [],
Expand Down
7 changes: 7 additions & 0 deletions src/serverSideTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import path from 'path'
import { createConfig } from './config/createConfig'
import createClient from './createClient'

import { globalI18n } from './appWithTranslation'

import { UserConfig, SSRConfig } from './types'
import { FallbackLng } from 'i18next'

Expand Down Expand Up @@ -60,8 +62,13 @@ export const serverSideTranslations = async (
localeExtension,
localePath,
fallbackLng,
reloadOnPrerender,
} = config

if (reloadOnPrerender) {
await globalI18n?.reloadResources()
}
isaachinman marked this conversation as resolved.
Show resolved Hide resolved

const { i18n, initPromise } = createClient({
...config,
lng: initialLocale,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type UserConfig = {
localeExtension?: string
localePath?: string
localeStructure?: string
reloadOnPrerender?: boolean
serializeConfig?: boolean
strictMode?: boolean
use?: any[]
Expand Down