Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix(ssr): provide Intl.PluralRules so crawler renders don't crash

Matt Roberts6ab04dc2773b
diff --git a/app/pkg/web/react_test.go b/app/pkg/web/react_test.go
index 2d5589e2d..341cac0e1 100644
--- a/app/pkg/web/react_test.go
+++ b/app/pkg/web/react_test.go
@@ -125,3 +125,57 @@ func TestReactRenderer_RenderEmptyHomeHTML_Portuguese(t *testing.T) {
Expect(html).ContainsSubstring(`Powered by Fider`)
Expect(err).IsNil()
}
+
+func renderHomeWithVotes(t *testing.T, locale string) string {
+ r, err := web.NewReactRenderer("ssr.js")
+ Expect(err).IsNil()
+
+ posts := make([]web.Map, 0)
+ for i, votes := range []int{1, 3, 5} {
+ posts = append(posts, web.Map{
+ "id": i + 1,
+ "number": i + 1,
+ "slug": "post",
+ "title": "Post",
+ "description": "",
+ "status": "open",
+ "votesCount": votes,
+ "commentsCount": 0,
+ "tags": []string{},
+ "user": web.Map{"id": 1, "name": "Jon Snow"},
+ })
+ }
+
+ u, _ := url.Parse("https://demo.test.fider.io")
+ html, err := r.Render(u, web.Map{
+ "page": "Home/Home.page",
+ "tenant": &entity.Tenant{Locale: locale},
+ "settings": web.Map{"locale": locale},
+ "props": web.Map{
+ "posts": posts,
+ "tags": make([]web.Map, 0),
+ "countPerStatus": web.Map{"open": len(posts)},
+ },
+ })
+ Expect(err).IsNil()
+ return html
+}
+
+// Vote counts use plural messages, which need Intl.PluralRules (missing in v8go)
+func TestReactRenderer_RenderHomeWithPosts_Plurals(t *testing.T) {
+ RegisterT(t)
+
+ html := renderHomeWithVotes(t, "en")
+ Expect(html).ContainsSubstring(`<span class="text-gray-700">Vote</span>`)
+ Expect(html).ContainsSubstring(`<span class="text-gray-700">Votes</span>`)
+}
+
+func TestReactRenderer_RenderHomeWithPosts_Plurals_Polish(t *testing.T) {
+ RegisterT(t)
+
+ // Polish has distinct forms for 1 (one), 3 (few) and 5 (many)
+ html := renderHomeWithVotes(t, "pl")
+ Expect(html).ContainsSubstring(`<span class="text-gray-700">Głos</span>`)
+ Expect(html).ContainsSubstring(`<span class="text-gray-700">Głosy</span>`)
+ Expect(html).ContainsSubstring(`<span class="text-gray-700">Głosów</span>`)
+}
diff --git a/esbuild-shim.js b/esbuild-shim.js
index c77a90478..f6d99124f 100644
--- a/esbuild-shim.js
+++ b/esbuild-shim.js
@@ -1,3 +1,5 @@
+import * as plurals from "make-plural/plurals"
+
const global = (1, eval)("this")
global.global = global
global.globalThis = global
@@ -41,9 +43,23 @@ class NoopFormat {
}
}
+// Lingui formats plural messages with Intl.PluralRules, so back it with the CLDR rules from make-plural
+class PluralRules {
+ constructor(locales, options) {
+ const locale = [].concat(locales || [])[0] || "en"
+ this.rule = plurals[locale.replace("-", "_")] || plurals[locale.split("-")[0]] || plurals.en
+ this.ordinal = !!options && options.type === "ordinal"
+ }
+
+ select(n) {
+ return this.rule(n, this.ordinal)
+ }
+}
+
global.Intl = {
NumberFormat: NoopFormat,
DateTimeFormat: NoopFormat,
+ PluralRules,
}
class TextEncoder {
diff --git a/package-lock.json b/package-lock.json
index 17c0e5de5..b6c3e1a99 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -69,6 +69,7 @@
"isomorphic-fetch": "3.0.0",
"jest": "28.1.3",
"jest-environment-jsdom": "28.1.3",
+ "make-plural": "8.1.0",
"mini-css-extract-plugin": "2.10.2",
"prettier": "2.6.2",
"purgecss-webpack-plugin": "8.0.0",
@@ -14637,6 +14638,13 @@
"dev": true,
"license": "ISC"
},
+ "node_modules/make-plural": {
+ "version": "8.1.0",
+ "resolved": "https://registry.npmjs.org/make-plural/-/make-plural-8.1.0.tgz",
+ "integrity": "sha512-p8EfQ2LFxnU4KGc82hOYdUplQw0eoWJLzJVKyv2GR9sd4zkjA8bLSXLEmp/qE08c/cFTZOK8j6Ex80+Wey+4PA==",
+ "dev": true,
+ "license": "Unicode-DFS-2016"
+ },
"node_modules/makeerror": {
"version": "1.0.12",
"resolved": "https://registry.npmjs.org/makeerror/-/makeerror-1.0.12.tgz",
diff --git a/package.json b/package.json
index 0a312ced2..7d1e2c4de 100644
--- a/package.json
+++ b/package.json
@@ -65,6 +65,7 @@
"isomorphic-fetch": "3.0.0",
"jest": "28.1.3",
"jest-environment-jsdom": "28.1.3",
+ "make-plural": "8.1.0",
"mini-css-extract-plugin": "2.10.2",
"prettier": "2.6.2",
"purgecss-webpack-plugin": "8.0.0",