Skip to content

Commit dc24921

Browse files
authored
Merge pull request #136 from microapps/feat/g4-r18
feat(gatsby): gatsby 4 and react 18
2 parents 2bdffa8 + dfb07e1 commit dc24921

File tree

5 files changed

+6637
-8620
lines changed

5 files changed

+6637
-8620
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Easily translate your Gatsby website into multiple languages.
1515

1616
When you build multilingual sites, Google recommends using different URLs for each language version of a page rather than using cookies or browser settings to adjust the content language on the page. [(read more)](https://support.google.com/webmasters/answer/182192?hl=en&ref_topic=2370587)
1717

18+
## :boom: Breaking change since v2.0.0
19+
20+
As of V2.0.0, this plugin only supports Gatsby 4.16.0+ and React 18+.
21+
1822
## :boom: Breaking change since v1.0.0
1923

2024
As of v1.0.0, language JSON resources should be loaded by `gatsby-source-filesystem` plugin and then fetched by GraphQL query. It enables incremental build and hot-reload as language JSON files change.
@@ -65,7 +69,9 @@ plugins: [
6569
languages: [`en`, `es`, `de`],
6670
defaultLanguage: `en`,
6771
// if you are using Helmet, you must include siteUrl, and make sure you add http:https
68-
siteUrl: `https://example.com/`,
72+
siteUrl: `https://example.com`,
73+
// if you are using trailingSlash gatsby config include it here, as well (the default is 'always')
74+
trailingSlash: 'always',
6975
// you can pass any i18next options
7076
i18nextOptions: {
7177
interpolation: {

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gatsby-plugin-react-i18next",
3-
"version": "1.2.3",
3+
"version": "2.0.0",
44
"description": "Easily translate your Gatsby website into multiple languages",
55
"main": "index.js",
66
"types": "index.d.ts",
@@ -48,19 +48,19 @@
4848
"devDependencies": {
4949
"@babel/cli": "^7.15.7",
5050
"@types/bluebird": "^3.5.36",
51-
"@types/react": "^17.0.33",
51+
"@types/react": "^18.0.12",
5252
"@types/react-helmet": "^6.1.4",
53-
"babel-preset-gatsby-package": "^1.14.0",
54-
"gatsby": "^3.0.0",
53+
"babel-preset-gatsby-package": "^2.16.0",
54+
"gatsby": "^4.16.0",
5555
"husky": "^4.3.8",
5656
"i18next": "^21.3.3",
5757
"prettier": "^2.4.1",
5858
"pretty-quick": "^3.1.1",
59-
"react": "^17.0.2",
60-
"react-dom": "^17.0.2",
59+
"react": "^18.2.0",
60+
"react-dom": "^18.2.0",
6161
"react-helmet": "^6.1.0",
6262
"react-i18next": "^11.12.0",
63-
"release-it": "^14.11.6",
63+
"release-it": "^14.14.3",
6464
"typescript": "^4.4.4"
6565
},
6666
"dependencies": {
@@ -70,11 +70,11 @@
7070
"path-to-regexp": "^6.2.0"
7171
},
7272
"peerDependencies": {
73-
"gatsby": "^3.x || ^4.x",
74-
"i18next": "^19.x || ^20.x || ^21.x",
75-
"react": "^16.x || ^17.x",
76-
"react-i18next": "^11.12.0",
77-
"react-helmet": "^6.1.0"
73+
"gatsby": "^4.16.0",
74+
"i18next": "^21.x",
75+
"react": "^18.x",
76+
"react-helmet": "^6.1.0",
77+
"react-i18next": "^11.17.0"
7878
},
7979
"optionalDependencies": {
8080
"react-helmet": "^6.1.0"

src/plugin/wrapPageElement.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ const withI18next = (i18n: I18n, context: I18NextContext) => (children: any) =>
2424
);
2525
};
2626

27-
const removePathPrefix = (pathname: string) => {
27+
const removePathPrefix = (pathname: string, stripTrailingSlash: boolean) => {
2828
const pathPrefix = withPrefix('/');
29+
let result = pathname;
30+
2931
if (pathname.startsWith(pathPrefix)) {
30-
return pathname.replace(pathPrefix, '/');
32+
result = pathname.replace(pathPrefix, '/');
33+
}
34+
35+
if (stripTrailingSlash && result.endsWith('/')) {
36+
return result.slice(0, -1);
3137
}
3238

33-
return pathname;
39+
return result;
3440
};
3541

3642
export const wrapPageElement = (
@@ -41,7 +47,8 @@ export const wrapPageElement = (
4147
generateDefaultLanguagePage = false,
4248
siteUrl,
4349
localeJsonNodeName = 'locales',
44-
fallbackLanguage
50+
fallbackLanguage,
51+
trailingSlash
4552
}: PluginOptions
4653
) => {
4754
if (!props) return;
@@ -69,8 +76,11 @@ export const wrapPageElement = (
6976

7077
if (detected !== defaultLanguage) {
7178
const queryParams = search || '';
79+
const stripTrailingSlash = trailingSlash === 'never';
7280
const newUrl = withPrefix(
73-
`/${detected}${removePathPrefix(location.pathname)}${queryParams}${location.hash}`
81+
`/${detected}${removePathPrefix(location.pathname, stripTrailingSlash)}${queryParams}${
82+
location.hash
83+
}`
7484
);
7585
// @ts-ignore
7686
window.___replace(newUrl);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type PluginOptions = {
2323
localeJsonSourceName?: string;
2424
localeJsonNodeName?: string;
2525
fallbackLanguage?: string;
26+
trailingSlash?: 'always' | 'never' | 'ignore';
2627
verbose?: boolean;
2728
};
2829

0 commit comments

Comments
 (0)