@shuvi/platform-web 2.0.0-dev.31 → 2.0.0-dev.33

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.
@@ -11,6 +11,7 @@ export interface CreateAppServer {
11
11
  req: ShuviRequest;
12
12
  ssr: boolean;
13
13
  basename: string;
14
+ strictTrailingSlash?: boolean;
14
15
  }): InternalApplication;
15
16
  }
16
17
  export interface CreateAppClient {
@@ -1,6 +1,13 @@
1
1
  import { IRouterHistoryMode, IPageRouteConfig } from '@shuvi/platform-shared/shared';
2
2
  export interface IRouterConfig {
3
3
  history: IRouterHistoryMode | 'auto';
4
+ /**
5
+ * When true, path matching treats trailing slashes as significant. A request
6
+ * whose pathname differs only by a trailing slash from a real route is
7
+ * redirected (308) to the canonical URL instead of rendering the route.
8
+ * Defaults to false for backward compatibility.
9
+ */
10
+ strictTrailingSlash?: boolean;
4
11
  }
5
12
  export interface PlatformWebCustomConfig {
6
13
  ssr: boolean;
@@ -23,7 +23,7 @@ export const createApp = ({ routes, appData, appComponent }) => {
23
23
  if (app) {
24
24
  return app;
25
25
  }
26
- const { appState, ssr, basename } = appData;
26
+ const { appState, ssr, basename, strictTrailingSlash = false } = appData;
27
27
  let history;
28
28
  if (historyMode === 'hash') {
29
29
  history = createHashHistory({ basename });
@@ -33,7 +33,8 @@ export const createApp = ({ routes, appData, appComponent }) => {
33
33
  }
34
34
  const router = createRouter({
35
35
  history,
36
- routes: getRoutes(routes)
36
+ routes: getRoutes(routes),
37
+ strictTrailingSlash
37
38
  });
38
39
  app = application({
39
40
  initialState: appState,
@@ -17,7 +17,7 @@ import logger from '@shuvi/utils/logger';
17
17
  import { serializeServerError } from '../helper/serializeServerError';
18
18
  const { SHUVI_SERVER_RUN_LOADERS } = SERVER_CREATE_APP.events;
19
19
  export const createApp = options => {
20
- const { req, ssr, basename } = options;
20
+ const { req, ssr, basename, strictTrailingSlash = false } = options;
21
21
  const history = createMemoryHistory({
22
22
  initialEntries: [(req && req.url) || '/'],
23
23
  initialIndex: 0,
@@ -25,7 +25,8 @@ export const createApp = options => {
25
25
  });
26
26
  const router = createRouter({
27
27
  history,
28
- routes: getRoutes(routes)
28
+ routes: getRoutes(routes),
29
+ strictTrailingSlash
29
30
  });
30
31
  let app;
31
32
  if (ssr) {
@@ -54,7 +54,13 @@ export class ReactServerView {
54
54
  : router.resolve(location, pathname).href, status);
55
55
  }
56
56
  // handle router internal redirect
57
- return redirect(router.resolve(router.current).href);
57
+ // handle router internal redirect. A `status` carried in `state` (e.g.
58
+ // from strict trailing-slash routing) takes precedence; otherwise fall
59
+ // back to 302 to preserve existing behavior for `route.redirect`.
60
+ const internalStatus = state && typeof state.status === 'number'
61
+ ? state.status
62
+ : 302;
63
+ return redirect(router.resolve(router.current).href, internalStatus);
58
64
  }
59
65
  const loadableModules = [];
60
66
  let htmlContent = undefined;
@@ -19,10 +19,12 @@ const renderer_1 = require("./renderer");
19
19
  const AppConfigManager_1 = __importDefault(require("../../setup-app-config/AppConfigManager"));
20
20
  function renderToHTML(_a) {
21
21
  return __awaiter(this, arguments, void 0, function* ({ req, serverPluginContext }) {
22
+ var _b;
22
23
  let result;
23
24
  const renderer = new renderer_1.Renderer({ serverPluginContext });
24
- const { config: { ssr } } = serverPluginContext;
25
+ const { config: { ssr, router } } = serverPluginContext;
25
26
  const { basename } = AppConfigManager_1.default.getAppConfig(req).router;
27
+ const strictTrailingSlash = (_b = router === null || router === void 0 ? void 0 : router.strictTrailingSlash) !== null && _b !== void 0 ? _b : false;
26
28
  const { serverCreateAppTrace } = req._traces;
27
29
  const { application } = resources_1.default.server;
28
30
  const app = serverCreateAppTrace
@@ -32,7 +34,8 @@ function renderToHTML(_a) {
32
34
  .traceFn(() => application.createApp({
33
35
  req,
34
36
  ssr,
35
- basename
37
+ basename,
38
+ strictTrailingSlash
36
39
  }));
37
40
  try {
38
41
  yield serverCreateAppTrace
@@ -40,7 +40,7 @@ class SsrRenderer extends base_1.BaseRenderer {
40
40
  Object.assign(acc, data);
41
41
  return acc;
42
42
  }, {});
43
- const appData = Object.assign(Object.assign({}, result.appData), { ssr: true, appState: store.getState(), pageData, basename: router.basename, runtimeConfig: (0, shuvi_singleton_runtimeConfig_1.getPublicRuntimeConfig)() || {} });
43
+ const appData = Object.assign(Object.assign({}, result.appData), { ssr: true, appState: store.getState(), pageData, basename: router.basename, strictTrailingSlash: router.strictTrailingSlash, runtimeConfig: (0, shuvi_singleton_runtimeConfig_1.getPublicRuntimeConfig)() || {} });
44
44
  const document = {
45
45
  htmlAttrs: Object.assign({}, result.htmlAttrs),
46
46
  headTags: [
@@ -88,7 +88,9 @@ class OnDemandRouteManager {
88
88
  ensureRoutes(pathname, req) {
89
89
  return __awaiter(this, void 0, void 0, function* () {
90
90
  const { basename } = AppConfigManager_1.default.getAppConfig(req).router;
91
- const matchedRoutes = (0, router_1.matchRoutes)(resources_1.default.server.pageRoutes, pathname, (0, utils_1.normalizeBase)(basename)) || [];
91
+ const { strictTrailingSlash = false } = this._serverPluginContext.config.router ||
92
+ {};
93
+ const matchedRoutes = (0, router_1.matchRoutes)(resources_1.default.server.pageRoutes, pathname, (0, utils_1.normalizeBase)(basename), { strict: strictTrailingSlash }) || [];
92
94
  const modulesToActivate = matchedRoutes
93
95
  .map(({ route: { __componentRawRequest__ } }) => __componentRawRequest__)
94
96
  .filter(Boolean);
@@ -11,6 +11,7 @@ export interface CreateAppServer {
11
11
  req: ShuviRequest;
12
12
  ssr: boolean;
13
13
  basename: string;
14
+ strictTrailingSlash?: boolean;
14
15
  }): InternalApplication;
15
16
  }
16
17
  export interface CreateAppClient {
@@ -1,6 +1,13 @@
1
1
  import { IRouterHistoryMode, IPageRouteConfig } from '@shuvi/platform-shared/shared';
2
2
  export interface IRouterConfig {
3
3
  history: IRouterHistoryMode | 'auto';
4
+ /**
5
+ * When true, path matching treats trailing slashes as significant. A request
6
+ * whose pathname differs only by a trailing slash from a real route is
7
+ * redirected (308) to the canonical URL instead of rendering the route.
8
+ * Defaults to false for backward compatibility.
9
+ */
10
+ strictTrailingSlash?: boolean;
4
11
  }
5
12
  export interface PlatformWebCustomConfig {
6
13
  ssr: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/platform-web",
3
- "version": "2.0.0-dev.31",
3
+ "version": "2.0.0-dev.33",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -72,15 +72,15 @@
72
72
  },
73
73
  "dependencies": {
74
74
  "@rspack/plugin-react-refresh": "^1.4.3",
75
- "@shuvi/error-overlay": "2.0.0-dev.31",
76
- "@shuvi/hook": "2.0.0-dev.31",
77
- "@shuvi/platform-shared": "2.0.0-dev.31",
78
- "@shuvi/router": "2.0.0-dev.31",
79
- "@shuvi/router-react": "2.0.0-dev.31",
80
- "@shuvi/runtime": "2.0.0-dev.31",
81
- "@shuvi/shared": "2.0.0-dev.31",
82
- "@shuvi/toolpack": "2.0.0-dev.31",
83
- "@shuvi/utils": "2.0.0-dev.31",
75
+ "@shuvi/error-overlay": "2.0.0-dev.33",
76
+ "@shuvi/hook": "2.0.0-dev.33",
77
+ "@shuvi/platform-shared": "2.0.0-dev.33",
78
+ "@shuvi/router": "2.0.0-dev.33",
79
+ "@shuvi/router-react": "2.0.0-dev.33",
80
+ "@shuvi/runtime": "2.0.0-dev.33",
81
+ "@shuvi/shared": "2.0.0-dev.33",
82
+ "@shuvi/toolpack": "2.0.0-dev.33",
83
+ "@shuvi/utils": "2.0.0-dev.33",
84
84
  "content-type": "1.0.4",
85
85
  "core-js": "3.6.5",
86
86
  "doura": "0.0.13",
@@ -98,10 +98,10 @@
98
98
  "whatwg-fetch": "3.0.0"
99
99
  },
100
100
  "peerDependencies": {
101
- "@shuvi/service": "2.0.0-dev.31"
101
+ "@shuvi/service": "2.0.0-dev.33"
102
102
  },
103
103
  "devDependencies": {
104
- "@shuvi/service": "2.0.0-dev.31",
104
+ "@shuvi/service": "2.0.0-dev.33",
105
105
  "@testing-library/react": "^13.2.0",
106
106
  "@types/content-type": "^1.1.5",
107
107
  "@types/ejs": "^3.1.0",