@quark-fw/plugin-react 0.1.25 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quark-fw/plugin-react",
3
- "version": "0.1.25",
3
+ "version": "0.1.26",
4
4
  "main": "src/index.ts",
5
5
  "types": "src/index.ts",
6
6
  "type": "module",
@@ -22,8 +22,8 @@
22
22
  "access": "public"
23
23
  },
24
24
  "dependencies": {
25
- "@quark-fw/atom": "0.1.25",
26
- "@quark-fw/plugin": "0.1.25",
25
+ "@quark-fw/atom": "0.1.26",
26
+ "@quark-fw/plugin": "0.1.26",
27
27
  "@trpc/client": "~11.18.0",
28
28
  "@types/react-dom": "^19.2.3",
29
29
  "express": "^5.2.1",
@@ -36,8 +36,8 @@
36
36
  "@mantine/dates": "^9.0.0",
37
37
  "@mantine/modals": "^9.0.0",
38
38
  "@mantine/notifications": "^9.0.0",
39
- "@quark-fw/core": "0.1.25",
40
- "@quark-fw/store": "0.1.25",
39
+ "@quark-fw/core": "0.1.26",
40
+ "@quark-fw/store": "0.1.26",
41
41
  "@vitejs/plugin-react": "^5.1.0",
42
42
  "react": "^19.2.4",
43
43
  "react-dom": "^19.2.4",
@@ -0,0 +1,16 @@
1
+ import { expect, test } from "vitest";
2
+ import { buildLocalePath } from "./config.js";
3
+
4
+ // Корень локали — без завершающего слэша: так его рендерит react-router в
5
+ // ссылках (href="/ru") и так его перечисляет sitemap приложения. Форма со
6
+ // слэшем превращалась у Google в дубликат с другим canonical.
7
+ test("buildLocalePath: корень локали без слэша", () => {
8
+ expect(buildLocalePath("ru", "/")).toBe("/ru");
9
+ expect(buildLocalePath("ru", "")).toBe("/ru");
10
+ });
11
+
12
+ test("buildLocalePath: вложенные пути с префиксом и без", () => {
13
+ expect(buildLocalePath("en", "/tbilisi")).toBe("/en/tbilisi");
14
+ expect(buildLocalePath("en", "tbilisi")).toBe("/en/tbilisi");
15
+ expect(buildLocalePath("ka", "/tbilisi?page=2")).toBe("/ka/tbilisi?page=2");
16
+ });
@@ -79,6 +79,9 @@ export function parseLocalePath(
79
79
  }
80
80
 
81
81
  export function buildLocalePath(locale: string, strippedPath: string): string {
82
+ // корень локали — без завершающего слэша: так его рендерит react-router
83
+ // в ссылках и перечисляет sitemap; /ru/ Google считал дубликатом /ru
84
+ if (strippedPath === "/" || strippedPath === "") return `/${locale}`;
82
85
  return `/${locale}${strippedPath.startsWith("/") ? strippedPath : `/${strippedPath}`}`;
83
86
  }
84
87
 
@@ -24,6 +24,7 @@ import { createRendererLoader } from "./createRendererLoader.js";
24
24
  import { createPreloadTagWriter } from "./createPreloadTagWriter.js";
25
25
  import { createRequestTrpcClient } from "./createRequestTrpcClient.js";
26
26
  import { createStreamingStore } from "./createStreamingStore.js";
27
+ import { stripTrailingSlash } from "../utils/stripTrailingSlash.js";
27
28
 
28
29
  export type GetRenderMiddlewareArgs = {
29
30
  /** dev: модули грузятся через vite; в prod — из собранного бандла */
@@ -43,6 +44,21 @@ export default function getRenderMiddleware({ vite }: GetRenderMiddlewareArgs) {
43
44
  const i18nConfig = (rendererModule as { i18nConfig?: I18nConfig })
44
45
  .i18nConfig;
45
46
  const urlWithoutSearch = removeSearchParamsFromUrl(req.originalUrl);
47
+
48
+ // одна каноническая форма пути — без завершающего слэша: /ru/ и
49
+ // /ru/tbilisi/ отдавали 200 со своим canonical и дублировали страницы
50
+ const normalized = stripTrailingSlash(urlWithoutSearch);
51
+ if (normalized) {
52
+ const searchIndex = req.originalUrl.indexOf("?");
53
+ res.redirect(
54
+ 301,
55
+ normalized +
56
+ (searchIndex === -1
57
+ ? ""
58
+ : req.originalUrl.slice(searchIndex)),
59
+ );
60
+ return;
61
+ }
46
62
  const { locale, strippedPathname, basename, redirect } =
47
63
  parseLocalePath(urlWithoutSearch, i18nConfig);
48
64
 
@@ -0,0 +1,14 @@
1
+ import { expect, test } from "vitest";
2
+ import { stripTrailingSlash } from "./stripTrailingSlash.js";
3
+
4
+ test("stripTrailingSlash: путь без слэша и корень не меняются", () => {
5
+ expect(stripTrailingSlash("/")).toBeNull();
6
+ expect(stripTrailingSlash("/ru")).toBeNull();
7
+ expect(stripTrailingSlash("/ru/tbilisi")).toBeNull();
8
+ });
9
+
10
+ test("stripTrailingSlash: завершающие слэши срезаются", () => {
11
+ expect(stripTrailingSlash("/ru/")).toBe("/ru");
12
+ expect(stripTrailingSlash("/ru/tbilisi/")).toBe("/ru/tbilisi");
13
+ expect(stripTrailingSlash("/ru/tbilisi//")).toBe("/ru/tbilisi");
14
+ });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Путь без завершающих слэшей, если они были; null — если менять нечего.
3
+ * Корень "/" остаётся как есть. Нужен SSR-мидлваре: /ru/ и /ru/tbilisi/
4
+ * рендерились как отдельные страницы со своим canonical, и Google считал
5
+ * их дубликатами.
6
+ */
7
+ export function stripTrailingSlash(pathname: string): string | null {
8
+ if (pathname.length <= 1 || !pathname.endsWith("/")) return null;
9
+ return pathname.replace(/\/+$/, "") || "/";
10
+ }