Skip to content

Commit 1e82dd7

Browse files
authored
chore(examples): fix tanstack-start example on API & SSR requests (#2210)
1 parent b833653 commit 1e82dd7

File tree

8 files changed

+150
-113
lines changed

8 files changed

+150
-113
lines changed

examples/tanstack-start/src/api.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,18 @@ import {
22
createStartAPIHandler,
33
defaultAPIFileRouteHandler,
44
} from "@tanstack/react-start/api"
5+
import {
6+
defaultLocale,
7+
dynamicActivate,
8+
isLocaleValid,
9+
} from "./modules/lingui/i18n"
10+
11+
export default createStartAPIHandler(async (ctx) => {
12+
// Define the locale based on the Accept-Language header
13+
const headerLocale = ctx.request.headers.get("Accept-Language") ?? ""
14+
await dynamicActivate(
15+
isLocaleValid(headerLocale) ? headerLocale : defaultLocale
16+
)
517

6-
export default createStartAPIHandler(defaultAPIFileRouteHandler)
18+
return defaultAPIFileRouteHandler(ctx)
19+
})

examples/tanstack-start/src/locales/en/messages.po

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,50 @@ msgstr ""
1313
"Language-Team: \n"
1414
"Plural-Forms: \n"
1515

16+
#. js-lingui-explicit-id
17+
#: src/routes/users.$userId.tsx:18
18+
msgid "Failed to fetch user"
19+
msgstr "Failed to fetch user"
20+
21+
#. js-lingui-explicit-id
22+
#: src/routes/users.route.tsx:18
23+
msgid "Failed to fetch users"
24+
msgstr "Failed to fetch users"
25+
26+
#. js-lingui-explicit-id
27+
#: src/routes/api/users.$id.ts:22
28+
msgid "User not found"
29+
msgstr "User not found"
30+
1631
#: src/routes/posts_.$postId.deep.tsx:24
1732
msgid "← All Posts"
1833
msgstr "← All Posts"
1934

20-
#: src/routes/deferred.tsx:57
35+
#: src/routes/deferred.tsx:70
2136
msgid "Count: {count}"
2237
msgstr "Count: {count}"
2338

24-
#: src/routes/posts.$postId.tsx:31
39+
#: src/routes/posts.$postId.tsx:35
2540
msgid "Deep View"
2641
msgstr "Deep View"
2742

2843
#: src/routes/__root.tsx:138
2944
msgid "Deferred"
3045
msgstr "Deferred"
3146

32-
#: src/routes/users.$userId.tsx:16
33-
msgid "Failed to fetch user"
34-
msgstr "Failed to fetch user"
35-
36-
#: src/routes/users.route.tsx:14
37-
msgid "Failed to fetch users"
38-
msgstr "Failed to fetch users"
39-
40-
#: src/components/NotFound.tsx:15
47+
#: src/components/NotFound.tsx:19
4148
msgid "Go back"
4249
msgstr "Go back"
4350

4451
#: src/components/DefaultCatchBoundary.tsx:48
4552
msgid "Go Back"
4653
msgstr "Go Back"
4754

48-
#: src/routes/_pathlessLayout/_nested-layout.tsx:19
55+
#: src/routes/_pathlessLayout/_nested-layout.tsx:21
4956
msgid "Go to route A"
5057
msgstr "Go to route A"
5158

52-
#: src/routes/_pathlessLayout/_nested-layout.tsx:27
59+
#: src/routes/_pathlessLayout/_nested-layout.tsx:29
5360
msgid "Go to route B"
5461
msgstr "Go to route B"
5562

@@ -58,67 +65,67 @@ msgstr "Go to route B"
5865
msgid "Home"
5966
msgstr "Home"
6067

61-
#: src/routes/_pathlessLayout.tsx:11
68+
#: src/routes/_pathlessLayout.tsx:12
6269
msgid "I'm a layout"
6370
msgstr "I'm a layout"
6471

65-
#: src/routes/_pathlessLayout/_nested-layout.tsx:11
72+
#: src/routes/_pathlessLayout/_nested-layout.tsx:12
6673
msgid "I'm a nested layout"
6774
msgstr "I'm a nested layout"
6875

69-
#: src/routes/_pathlessLayout/_nested-layout/route-a.tsx:11
76+
#: src/routes/_pathlessLayout/_nested-layout/route-a.tsx:13
7077
msgid "I'm A!"
7178
msgstr "I'm A!"
7279

73-
#: src/routes/_pathlessLayout/_nested-layout/route-b.tsx:11
80+
#: src/routes/_pathlessLayout/_nested-layout/route-b.tsx:13
7481
msgid "I'm B!"
7582
msgstr "I'm B!"
7683

77-
#: src/routes/deferred.tsx:59
84+
#: src/routes/deferred.tsx:74
7885
msgid "Increment"
7986
msgstr "Increment"
8087

81-
#: src/routes/deferred.tsx:41
88+
#: src/routes/deferred.tsx:44
8289
msgid "Loading person..."
8390
msgstr "Loading person..."
8491

85-
#: src/routes/deferred.tsx:51
92+
#: src/routes/deferred.tsx:60
8693
msgid "Loading stuff..."
8794
msgstr "Loading stuff..."
8895

8996
#: src/routes/posts.route.tsx:17
9097
msgid "Non-existent Post"
9198
msgstr "Non-existent Post"
9299

93-
#: src/routes/users.route.tsx:29
100+
#: src/routes/users.route.tsx:33
94101
msgid "Non-existent User"
95102
msgstr "Non-existent User"
96103

97104
#: src/routes/__root.tsx:130
98105
msgid "Pathless Layout"
99106
msgstr "Pathless Layout"
100107

101-
#: src/routes/posts.$postId.tsx:12
108+
#: src/routes/posts.$postId.tsx:14
102109
msgid "Post not found"
103110
msgstr "Post not found"
104111

105112
#: src/routes/__root.tsx:114
106113
msgid "Posts"
107114
msgstr "Posts"
108115

109-
#: src/routes/posts.index.tsx:9
116+
#: src/routes/posts.index.tsx:11
110117
msgid "Select a post."
111118
msgstr "Select a post."
112119

113-
#: src/routes/users.index.tsx:9
120+
#: src/routes/users.index.tsx:11
114121
msgid "Select a user."
115122
msgstr "Select a user."
116123

117-
#: src/components/NotFound.tsx:21
124+
#: src/components/NotFound.tsx:25
118125
msgid "Start Over"
119126
msgstr "Start Over"
120127

121-
#: src/components/NotFound.tsx:8
128+
#: src/components/NotFound.tsx:10
122129
msgid "The page you are looking for does not exist."
123130
msgstr "The page you are looking for does not exist."
124131

@@ -130,14 +137,14 @@ msgstr "This Route Does Not Exist"
130137
msgid "Try Again"
131138
msgstr "Try Again"
132139

133-
#: src/routes/users.$userId.tsx:22
140+
#: src/routes/users.$userId.tsx:26
134141
msgid "User not found"
135142
msgstr "User not found"
136143

137144
#: src/routes/__root.tsx:122
138145
msgid "Users"
139146
msgstr "Users"
140147

141-
#: src/routes/index.tsx:11
148+
#: src/routes/index.tsx:12
142149
msgid "Welcome Home!!!"
143150
msgstr "Welcome Home!!!"

examples/tanstack-start/src/locales/fr/messages.po

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,50 @@ msgstr ""
1313
"Plural-Forms: \n"
1414
"X-Generator: Poedit 3.5\n"
1515

16+
#. js-lingui-explicit-id
17+
#: src/routes/users.$userId.tsx:18
18+
msgid "Failed to fetch user"
19+
msgstr "Échec du chargement de l'utilisateur"
20+
21+
#. js-lingui-explicit-id
22+
#: src/routes/users.route.tsx:18
23+
msgid "Failed to fetch users"
24+
msgstr "Échec du chargement des utilisateurs"
25+
26+
#. js-lingui-explicit-id
27+
#: src/routes/api/users.$id.ts:22
28+
msgid "User not found"
29+
msgstr "Utilisateur non trouvé"
30+
1631
#: src/routes/posts_.$postId.deep.tsx:24
1732
msgid "← All Posts"
1833
msgstr "← Tous les articles"
1934

20-
#: src/routes/deferred.tsx:57
35+
#: src/routes/deferred.tsx:70
2136
msgid "Count: {count}"
2237
msgstr "Compte: {count}"
2338

24-
#: src/routes/posts.$postId.tsx:31
39+
#: src/routes/posts.$postId.tsx:35
2540
msgid "Deep View"
2641
msgstr "Vue détaillée"
2742

2843
#: src/routes/__root.tsx:138
2944
msgid "Deferred"
3045
msgstr "Différé"
3146

32-
#: src/routes/users.$userId.tsx:16
33-
msgid "Failed to fetch user"
34-
msgstr "Échec du chargement de l'utilisateur"
35-
36-
#: src/routes/users.route.tsx:14
37-
msgid "Failed to fetch users"
38-
msgstr "Échec du chargement des utilisateurs"
39-
40-
#: src/components/NotFound.tsx:15
47+
#: src/components/NotFound.tsx:19
4148
msgid "Go back"
4249
msgstr "Retour"
4350

4451
#: src/components/DefaultCatchBoundary.tsx:48
4552
msgid "Go Back"
4653
msgstr "Retour"
4754

48-
#: src/routes/_pathlessLayout/_nested-layout.tsx:19
55+
#: src/routes/_pathlessLayout/_nested-layout.tsx:21
4956
msgid "Go to route A"
5057
msgstr "Aller à la route A"
5158

52-
#: src/routes/_pathlessLayout/_nested-layout.tsx:27
59+
#: src/routes/_pathlessLayout/_nested-layout.tsx:29
5360
msgid "Go to route B"
5461
msgstr "Aller à la route B"
5562

@@ -58,67 +65,67 @@ msgstr "Aller à la route B"
5865
msgid "Home"
5966
msgstr "Accueil"
6067

61-
#: src/routes/_pathlessLayout.tsx:11
68+
#: src/routes/_pathlessLayout.tsx:12
6269
msgid "I'm a layout"
6370
msgstr "Je suis un layout"
6471

65-
#: src/routes/_pathlessLayout/_nested-layout.tsx:11
72+
#: src/routes/_pathlessLayout/_nested-layout.tsx:12
6673
msgid "I'm a nested layout"
6774
msgstr "Je suis un layout imbriqué"
6875

69-
#: src/routes/_pathlessLayout/_nested-layout/route-a.tsx:11
76+
#: src/routes/_pathlessLayout/_nested-layout/route-a.tsx:13
7077
msgid "I'm A!"
7178
msgstr "Je suis A !"
7279

73-
#: src/routes/_pathlessLayout/_nested-layout/route-b.tsx:11
80+
#: src/routes/_pathlessLayout/_nested-layout/route-b.tsx:13
7481
msgid "I'm B!"
7582
msgstr "Je suis B !"
7683

77-
#: src/routes/deferred.tsx:59
84+
#: src/routes/deferred.tsx:74
7885
msgid "Increment"
7986
msgstr "Incrémenter"
8087

81-
#: src/routes/deferred.tsx:41
88+
#: src/routes/deferred.tsx:44
8289
msgid "Loading person..."
8390
msgstr "Chargement de la personne..."
8491

85-
#: src/routes/deferred.tsx:51
92+
#: src/routes/deferred.tsx:60
8693
msgid "Loading stuff..."
8794
msgstr "Chargement de choses..."
8895

8996
#: src/routes/posts.route.tsx:17
9097
msgid "Non-existent Post"
9198
msgstr "Article non-existant"
9299

93-
#: src/routes/users.route.tsx:29
100+
#: src/routes/users.route.tsx:33
94101
msgid "Non-existent User"
95102
msgstr "Utilisateur non-existant"
96103

97104
#: src/routes/__root.tsx:130
98105
msgid "Pathless Layout"
99106
msgstr "Layout sans chemin"
100107

101-
#: src/routes/posts.$postId.tsx:12
108+
#: src/routes/posts.$postId.tsx:14
102109
msgid "Post not found"
103110
msgstr "Article non trouvé"
104111

105112
#: src/routes/__root.tsx:114
106113
msgid "Posts"
107114
msgstr "Articles"
108115

109-
#: src/routes/posts.index.tsx:9
116+
#: src/routes/posts.index.tsx:11
110117
msgid "Select a post."
111118
msgstr "Sélectionnez un article."
112119

113-
#: src/routes/users.index.tsx:9
120+
#: src/routes/users.index.tsx:11
114121
msgid "Select a user."
115122
msgstr "Sélectionnez un utilisateur."
116123

117-
#: src/components/NotFound.tsx:21
124+
#: src/components/NotFound.tsx:25
118125
msgid "Start Over"
119126
msgstr "Recommencer"
120127

121-
#: src/components/NotFound.tsx:8
128+
#: src/components/NotFound.tsx:10
122129
msgid "The page you are looking for does not exist."
123130
msgstr "La page que vous cherchez n'existe pas."
124131

@@ -130,14 +137,14 @@ msgstr "Cette route n'existe pas"
130137
msgid "Try Again"
131138
msgstr "Réessayer"
132139

133-
#: src/routes/users.$userId.tsx:22
140+
#: src/routes/users.$userId.tsx:26
134141
msgid "User not found"
135142
msgstr "Utilisateur non trouvé"
136143

137144
#: src/routes/__root.tsx:122
138145
msgid "Users"
139146
msgstr "Utilisateurs"
140147

141-
#: src/routes/index.tsx:11
148+
#: src/routes/index.tsx:12
142149
msgid "Welcome Home!!!"
143150
msgstr "Bienvenue à la maison !!!"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {
2+
getHeaders,
3+
getWebRequest,
4+
setHeader,
5+
} from "@tanstack/react-start/server"
6+
import { parse, serialize } from "cookie-es"
7+
8+
import { defaultLocale, dynamicActivate, isLocaleValid } from "./i18n"
9+
10+
function getLocaleFromRequest() {
11+
const request = getWebRequest()
12+
const headers = getHeaders()
13+
const cookie = parse(headers.cookie ?? "")
14+
15+
if (request) {
16+
const url = new URL(request.url)
17+
const queryLocale = url.searchParams.get("locale") ?? ""
18+
19+
if (isLocaleValid(queryLocale)) {
20+
setHeader(
21+
"Set-Cookie",
22+
serialize("locale", queryLocale, {
23+
maxAge: 30 * 24 * 60 * 60,
24+
path: "/",
25+
})
26+
)
27+
28+
return queryLocale
29+
}
30+
}
31+
32+
if (cookie.locale && isLocaleValid(cookie.locale)) {
33+
return cookie.locale
34+
}
35+
36+
setHeader(
37+
"Set-Cookie",
38+
serialize("locale", defaultLocale, {
39+
maxAge: 30 * 24 * 60 * 60,
40+
path: "/",
41+
})
42+
)
43+
44+
return defaultLocale
45+
}
46+
47+
export async function setupLocaleFromRequest() {
48+
await dynamicActivate(getLocaleFromRequest())
49+
}

0 commit comments

Comments
 (0)