@modern-js/server-core 2.68.19 → 2.68.20

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.
@@ -19,7 +19,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var ssrCache_exports = {};
20
20
  __export(ssrCache_exports, {
21
21
  getCacheResult: () => getCacheResult,
22
- matchCacheControl: () => matchCacheControl
22
+ matchCacheControl: () => matchCacheControl,
23
+ shouldUseCache: () => shouldUseCache
23
24
  });
24
25
  module.exports = __toCommonJS(ssrCache_exports);
25
26
  var import_storer = require("@modern-js/runtime-utils/storer");
@@ -31,6 +32,20 @@ const NO_SSR_CACHE = /<meta\s+[^>]*name=["']no-ssr-cache["'][^>]*>/i;
31
32
  async function processCache({ request, key, requestHandler, requestHandlerOptions, ttl, container, cacheStatus }) {
32
33
  const response = await requestHandler(request, requestHandlerOptions);
33
34
  const { onError } = requestHandlerOptions;
35
+ const nonCacheableStatusCodes = [
36
+ 204,
37
+ 305,
38
+ 404,
39
+ 405,
40
+ 500,
41
+ 501,
42
+ 502,
43
+ 503,
44
+ 504
45
+ ];
46
+ if (nonCacheableStatusCodes.includes(response.status)) {
47
+ return response;
48
+ }
34
49
  const decoder = new TextDecoder();
35
50
  if (response.body) {
36
51
  const stream = (0, import_utils.createTransformStream)();
@@ -91,6 +106,13 @@ function computedKey(req, cacheControl) {
91
106
  return defaultKey;
92
107
  }
93
108
  }
109
+ function shouldUseCache(request) {
110
+ const url = new URL(request.url);
111
+ const hasRSCAction = request.headers.has("x-rsc-action");
112
+ const hasRSCTree = request.headers.has("x-rsc-tree");
113
+ const hasLoaderQuery = url.searchParams.has("__loader");
114
+ return !(hasRSCAction || hasRSCTree || hasLoaderQuery);
115
+ }
94
116
  function matchCacheControl(cacheOption, req) {
95
117
  if (!cacheOption || !req) {
96
118
  return void 0;
@@ -189,5 +211,6 @@ async function getCacheResult(request, options) {
189
211
  // Annotate the CommonJS export names for ESM import in node:
190
212
  0 && (module.exports = {
191
213
  getCacheResult,
192
- matchCacheControl
214
+ matchCacheControl,
215
+ shouldUseCache
193
216
  });
@@ -71,7 +71,7 @@ async function ssrRender(request, { routeInfo, html, config: userConfig, staticG
71
71
  };
72
72
  const cacheControl = await (0, import_ssrCache.matchCacheControl)(cacheConfig === null || cacheConfig === void 0 ? void 0 : cacheConfig.strategy, nodeReq || new IncomingMessgeProxy(request));
73
73
  let response;
74
- if (cacheControl) {
74
+ if (cacheControl && (0, import_ssrCache.shouldUseCache)(request)) {
75
75
  response = await (0, import_ssrCache.getCacheResult)(request, {
76
76
  cacheControl,
77
77
  container: cacheConfig === null || cacheConfig === void 0 ? void 0 : cacheConfig.container,
@@ -16,7 +16,7 @@ function processCache(_) {
16
16
  }
17
17
  function _processCache() {
18
18
  _processCache = _async_to_generator(function(param) {
19
- var request, key, requestHandler, requestHandlerOptions, ttl, container, cacheStatus, response, onError, decoder, stream, reader, writer, html, push;
19
+ var request, key, requestHandler, requestHandlerOptions, ttl, container, cacheStatus, response, onError, nonCacheableStatusCodes, decoder, stream, reader, writer, html, push;
20
20
  return _ts_generator(this, function(_state) {
21
21
  switch (_state.label) {
22
22
  case 0:
@@ -28,6 +28,23 @@ function _processCache() {
28
28
  case 1:
29
29
  response = _state.sent();
30
30
  onError = requestHandlerOptions.onError;
31
+ nonCacheableStatusCodes = [
32
+ 204,
33
+ 305,
34
+ 404,
35
+ 405,
36
+ 500,
37
+ 501,
38
+ 502,
39
+ 503,
40
+ 504
41
+ ];
42
+ if (nonCacheableStatusCodes.includes(response.status)) {
43
+ return [
44
+ 2,
45
+ response
46
+ ];
47
+ }
31
48
  decoder = new TextDecoder();
32
49
  if (response.body) {
33
50
  stream = createTransformStream();
@@ -101,6 +118,13 @@ function computedKey(req, cacheControl) {
101
118
  return defaultKey;
102
119
  }
103
120
  }
121
+ function shouldUseCache(request) {
122
+ var url = new URL(request.url);
123
+ var hasRSCAction = request.headers.has("x-rsc-action");
124
+ var hasRSCTree = request.headers.has("x-rsc-tree");
125
+ var hasLoaderQuery = url.searchParams.has("__loader");
126
+ return !(hasRSCAction || hasRSCTree || hasLoaderQuery);
127
+ }
104
128
  function matchCacheControl(cacheOption, req) {
105
129
  if (!cacheOption || !req) {
106
130
  return void 0;
@@ -276,5 +300,6 @@ function _getCacheResult() {
276
300
  }
277
301
  export {
278
302
  getCacheResult,
279
- matchCacheControl
303
+ matchCacheControl,
304
+ shouldUseCache
280
305
  };
@@ -4,7 +4,7 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
5
5
  import { X_MODERNJS_RENDER } from "../../constants";
6
6
  import { getPathname, parseHeaders } from "../../utils";
7
- import { getCacheResult, matchCacheControl } from "./ssrCache";
7
+ import { getCacheResult, matchCacheControl, shouldUseCache } from "./ssrCache";
8
8
  import { createRequestHandlerConfig } from "./utils";
9
9
  var SERVER_RUNTIME_ENTRY = "requestHandler";
10
10
  function ssrRender(request, _) {
@@ -68,7 +68,7 @@ function _ssrRender() {
68
68
  ];
69
69
  case 2:
70
70
  cacheControl = _state.sent();
71
- if (!cacheControl)
71
+ if (!(cacheControl && shouldUseCache(request)))
72
72
  return [
73
73
  3,
74
74
  4
@@ -7,6 +7,20 @@ const NO_SSR_CACHE = /<meta\s+[^>]*name=["']no-ssr-cache["'][^>]*>/i;
7
7
  async function processCache({ request, key, requestHandler, requestHandlerOptions, ttl, container, cacheStatus }) {
8
8
  const response = await requestHandler(request, requestHandlerOptions);
9
9
  const { onError } = requestHandlerOptions;
10
+ const nonCacheableStatusCodes = [
11
+ 204,
12
+ 305,
13
+ 404,
14
+ 405,
15
+ 500,
16
+ 501,
17
+ 502,
18
+ 503,
19
+ 504
20
+ ];
21
+ if (nonCacheableStatusCodes.includes(response.status)) {
22
+ return response;
23
+ }
10
24
  const decoder = new TextDecoder();
11
25
  if (response.body) {
12
26
  const stream = createTransformStream();
@@ -67,6 +81,13 @@ function computedKey(req, cacheControl) {
67
81
  return defaultKey;
68
82
  }
69
83
  }
84
+ function shouldUseCache(request) {
85
+ const url = new URL(request.url);
86
+ const hasRSCAction = request.headers.has("x-rsc-action");
87
+ const hasRSCTree = request.headers.has("x-rsc-tree");
88
+ const hasLoaderQuery = url.searchParams.has("__loader");
89
+ return !(hasRSCAction || hasRSCTree || hasLoaderQuery);
90
+ }
70
91
  function matchCacheControl(cacheOption, req) {
71
92
  if (!cacheOption || !req) {
72
93
  return void 0;
@@ -164,5 +185,6 @@ async function getCacheResult(request, options) {
164
185
  }
165
186
  export {
166
187
  getCacheResult,
167
- matchCacheControl
188
+ matchCacheControl,
189
+ shouldUseCache
168
190
  };
@@ -1,7 +1,7 @@
1
1
  import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
2
2
  import { X_MODERNJS_RENDER } from "../../constants";
3
3
  import { getPathname, parseHeaders } from "../../utils";
4
- import { getCacheResult, matchCacheControl } from "./ssrCache";
4
+ import { getCacheResult, matchCacheControl, shouldUseCache } from "./ssrCache";
5
5
  import { createRequestHandlerConfig } from "./utils";
6
6
  const SERVER_RUNTIME_ENTRY = "requestHandler";
7
7
  async function ssrRender(request, { routeInfo, html, config: userConfig, staticGenerate, nodeReq, serverManifest, rscSSRManifest, rscClientManifest, rscServerManifest, locals, params, loaderContext, reporter, monitors, cacheConfig, logger, metrics, onError, onTiming }) {
@@ -48,7 +48,7 @@ async function ssrRender(request, { routeInfo, html, config: userConfig, staticG
48
48
  };
49
49
  const cacheControl = await matchCacheControl(cacheConfig === null || cacheConfig === void 0 ? void 0 : cacheConfig.strategy, nodeReq || new IncomingMessgeProxy(request));
50
50
  let response;
51
- if (cacheControl) {
51
+ if (cacheControl && shouldUseCache(request)) {
52
52
  response = await getCacheResult(request, {
53
53
  cacheControl,
54
54
  container: cacheConfig === null || cacheConfig === void 0 ? void 0 : cacheConfig.container,
@@ -3,6 +3,10 @@ import type { NodeRequest } from '@modern-js/types/server';
3
3
  import type { RequestHandler, RequestHandlerOptions } from '../../types/requestHandler';
4
4
  export type CacheStatus = 'hit' | 'stale' | 'expired' | 'miss';
5
5
  type MaybeAsync<T> = Promise<T> | T;
6
+ /**
7
+ * Check if a request should be processed through cache logic
8
+ */
9
+ export declare function shouldUseCache(request: Request): boolean;
6
10
  export declare function matchCacheControl(cacheOption?: CacheOption, req?: NodeRequest): MaybeAsync<CacheControl | undefined | false>;
7
11
  export interface GetCacheResultOptions {
8
12
  cacheControl: CacheControl;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.68.19",
18
+ "version": "2.68.20",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -62,10 +62,10 @@
62
62
  "flatted": "^3.3.3",
63
63
  "hono": "^3.12.2",
64
64
  "ts-deepmerge": "7.0.2",
65
- "@modern-js/plugin": "2.68.19",
66
- "@modern-js/plugin-v2": "2.68.19",
67
- "@modern-js/runtime-utils": "2.68.19",
68
- "@modern-js/utils": "2.68.19"
65
+ "@modern-js/plugin": "2.68.20",
66
+ "@modern-js/plugin-v2": "2.68.20",
67
+ "@modern-js/runtime-utils": "2.68.20",
68
+ "@modern-js/utils": "2.68.20"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@types/cloneable-readable": "^2.0.3",
@@ -76,7 +76,7 @@
76
76
  "jest": "^29",
77
77
  "ts-jest": "^29.1.0",
78
78
  "typescript": "^5",
79
- "@modern-js/types": "2.68.19",
79
+ "@modern-js/types": "2.68.20",
80
80
  "@scripts/build": "2.66.0",
81
81
  "@scripts/jest-config": "2.66.0"
82
82
  },