rwsdk 1.0.0-beta.9-test.20251008025236 → 1.0.0-beta.9-test.20251008040832
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.
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { DefaultAppContext, RequestInfo } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* A custom error class to signal that a request is being processed
|
|
4
|
+
* on a stale context, likely due to an HMR update.
|
|
5
|
+
*/
|
|
6
|
+
export declare class StaleHmrRequestError extends Error {
|
|
7
|
+
constructor();
|
|
8
|
+
}
|
|
2
9
|
type DefaultRequestInfo = RequestInfo<DefaultAppContext>;
|
|
3
10
|
export declare const requestInfo: DefaultRequestInfo;
|
|
4
11
|
export declare function getRequestInfo(): RequestInfo;
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
+
/**
|
|
3
|
+
* A custom error class to signal that a request is being processed
|
|
4
|
+
* on a stale context, likely due to an HMR update.
|
|
5
|
+
*/
|
|
6
|
+
export class StaleHmrRequestError extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super("Stale HMR Request");
|
|
9
|
+
this.name = "StaleHmrRequestError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
2
12
|
const requestInfoStore = new AsyncLocalStorage();
|
|
3
13
|
const requestInfoBase = {};
|
|
4
14
|
const REQUEST_INFO_KEYS = [
|
|
@@ -18,6 +28,17 @@ REQUEST_INFO_KEYS.forEach((key) => {
|
|
|
18
28
|
const store = requestInfoStore.getStore();
|
|
19
29
|
return store ? store[key] : undefined;
|
|
20
30
|
},
|
|
31
|
+
set: function (value) {
|
|
32
|
+
const store = requestInfoStore.getStore();
|
|
33
|
+
if (!store) {
|
|
34
|
+
// If the store is missing, it means this setter is being called on a
|
|
35
|
+
// request that has been orphaned by an HMR update. We throw a
|
|
36
|
+
// specific error to signal this condition so the main request
|
|
37
|
+
// handler can catch it and short-circuit the request.
|
|
38
|
+
throw new StaleHmrRequestError();
|
|
39
|
+
}
|
|
40
|
+
store[key] = value;
|
|
41
|
+
},
|
|
21
42
|
});
|
|
22
43
|
});
|
|
23
44
|
export const requestInfo = Object.freeze(requestInfoBase);
|
package/dist/runtime/worker.js
CHANGED
|
@@ -5,7 +5,7 @@ import { renderToRscStream } from "./render/renderToRscStream";
|
|
|
5
5
|
import { injectRSCPayload } from "rsc-html-stream/server";
|
|
6
6
|
import { ErrorResponse } from "./error";
|
|
7
7
|
import { rscActionHandler } from "./register/worker";
|
|
8
|
-
import { getRequestInfo, runWithRequestInfo, runWithRequestInfoOverrides, } from "./requestInfo/worker";
|
|
8
|
+
import { getRequestInfo, runWithRequestInfo, runWithRequestInfoOverrides, StaleHmrRequestError, } from "./requestInfo/worker";
|
|
9
9
|
import { ssrWebpackRequire } from "./imports/worker";
|
|
10
10
|
import { defineRoutes } from "./lib/router";
|
|
11
11
|
import { generateNonce } from "./lib/utils";
|
|
@@ -160,6 +160,11 @@ export const defineApp = (routes) => {
|
|
|
160
160
|
}));
|
|
161
161
|
}
|
|
162
162
|
catch (e) {
|
|
163
|
+
if (e instanceof StaleHmrRequestError) {
|
|
164
|
+
console.warn("RWSDK: A request was short-circuited due to a stale HMR context.");
|
|
165
|
+
// Return a simple, empty response to gracefully terminate the request.
|
|
166
|
+
return new Response(null, { status: 204 });
|
|
167
|
+
}
|
|
163
168
|
reject(e);
|
|
164
169
|
}
|
|
165
170
|
}));
|
package/package.json
CHANGED