dinou 9.0.3 → 9.2.0

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,209 +1,209 @@
1
- /**
2
- * esm-hmr/client.js
3
- * A client-side implementation of the ESM-HMR spec, for reference.
4
- */
5
-
6
- function debug(...args) {
7
- console.log("[ESM-HMR]", ...args);
8
- }
9
-
10
- function reload() {
11
- location.reload(true);
12
- }
13
-
14
- let SOCKET_MESSAGE_QUEUE = [];
15
-
16
- function _sendSocketMessage(msg) {
17
- socket.send(JSON.stringify(msg));
18
- }
19
-
20
- function sendSocketMessage(msg) {
21
- if (socket.readyState !== socket.OPEN) {
22
- SOCKET_MESSAGE_QUEUE.push(msg);
23
- } else {
24
- _sendSocketMessage(msg);
25
- }
26
- }
27
-
28
- const socketURL =
29
- window.HMR_WEBSOCKET_URL ||
30
- (location.protocol === "http:" ? "ws://" : "wss://") + location.host + "/";
31
- const socket = new WebSocket(socketURL, "esm-hmr");
32
-
33
- socket.addEventListener("open", () => {
34
- SOCKET_MESSAGE_QUEUE.forEach(_sendSocketMessage);
35
- SOCKET_MESSAGE_QUEUE = [];
36
- });
37
-
38
- const REGISTERED_MODULES = {};
39
- class HotModuleState {
40
- constructor(id) {
41
- this.id = id;
42
- this.data = {};
43
- this.isLocked = false;
44
- this.isDeclined = false;
45
- this.isAccepted = false;
46
- this.acceptCallbacks = [];
47
- this.disposeCallbacks = [];
48
- }
49
-
50
- lock() {
51
- this.isLocked = true;
52
- }
53
-
54
- dispose(callback) {
55
- this.disposeCallbacks.push(callback);
56
- }
57
-
58
- invalidate() {
59
- reload();
60
- }
61
-
62
- decline() {
63
- this.isDeclined = true;
64
- }
65
-
66
- accept(_deps = [], callback = true) {
67
- if (this.isLocked) {
68
- return;
69
- }
70
- if (!this.isAccepted) {
71
- sendSocketMessage({ id: this.id, type: "hotAccept" });
72
- this.isAccepted = true;
73
- }
74
- if (!Array.isArray(_deps)) {
75
- callback = _deps || callback;
76
- _deps = [];
77
- }
78
- if (callback === true) {
79
- callback = () => {};
80
- }
81
- const deps = _deps.map((dep) => {
82
- const ext = dep.split(".").pop();
83
- if (!ext) {
84
- dep += ".js";
85
- } else if (ext !== "js") {
86
- dep += ".proxy.js";
87
- }
88
- return new URL(dep, `${window.location.origin}${this.id}`).pathname;
89
- });
90
- this.acceptCallbacks.push({
91
- deps,
92
- callback,
93
- });
94
- }
95
- }
96
-
97
- export function createHotContext(id) {
98
- const normId = id.startsWith("/") ? id : "/" + id;
99
- const existing = REGISTERED_MODULES[normId] || REGISTERED_MODULES[id];
100
- if (existing) {
101
- existing.isLocked = false;
102
- return existing;
103
- }
104
- const state = new HotModuleState(normId);
105
- REGISTERED_MODULES[normId] = state;
106
- REGISTERED_MODULES[id] = state;
107
- return state;
108
- }
109
-
110
- async function applyUpdate(id) {
111
- const normId = id.startsWith("/") ? id : "/" + id;
112
- const state = REGISTERED_MODULES[normId] || REGISTERED_MODULES[id];
113
- if (!state) {
114
- return true;
115
- }
116
- if (state.isDeclined) {
117
- return false;
118
- }
119
-
120
- const acceptCallbacks = state.acceptCallbacks;
121
- const disposeCallbacks = state.disposeCallbacks;
122
- state.disposeCallbacks = [];
123
- state.data = {};
124
-
125
- disposeCallbacks.forEach((callback) => callback());
126
-
127
- const updateID = Date.now();
128
- for (const { deps, callback: acceptCallback } of acceptCallbacks) {
129
- const [module, ...depModules] = await Promise.all([
130
- import(normId + `?mtime=${updateID}`),
131
- ...deps.map((d) => {
132
- const depId = d.startsWith("/") ? d : "/" + d;
133
- return import(depId + `?mtime=${updateID}`);
134
- }),
135
- ]);
136
- acceptCallback({ module, deps: depModules });
137
- }
138
-
139
- return true;
140
- }
141
-
142
- socket.addEventListener("message", ({ data: _data }) => {
143
- if (!_data) return;
144
-
145
- const data = JSON.parse(_data);
146
- if (data.type === "reload") {
147
- reload();
148
- return;
149
- }
150
-
151
- if (data.type === "rsc-update") {
152
- if (typeof window !== "undefined" && typeof window.__DINOU_ROUTER_REFRESH__ === "function") {
153
- window.__DINOU_ROUTER_REFRESH__();
154
- return;
155
- }
156
- reload();
157
- return;
158
- }
159
-
160
- if (
161
- data.type === "style-update" ||
162
- (data.type === "update" && data.url && (data.url.endsWith(".css") || data.url.includes("styles.css")))
163
- ) {
164
- const targetUrl = data.url ? data.url.split("?")[0] : "";
165
- const links = document.querySelectorAll('link[rel="stylesheet"]');
166
- for (const link of links) {
167
- const rawHref = link.getAttribute("href");
168
- if (!rawHref) continue;
169
- const cleanHref = rawHref.split("?")[0];
170
- if (!targetUrl || cleanHref.endsWith(targetUrl) || targetUrl.endsWith(cleanHref) || cleanHref.includes("styles.css")) {
171
- const nextUrl = new URL(link.href, window.location.origin);
172
- nextUrl.searchParams.set("t", String(Date.now()));
173
- link.href = nextUrl.href;
174
- }
175
- }
176
- return;
177
- }
178
-
179
- if (data.type !== "update") {
180
- return;
181
- }
182
-
183
- const tRecv = Date.now();
184
- const isDebug = typeof window !== "undefined" && Boolean(window.__DINOU_DEBUG__);
185
-
186
- if (isDebug) {
187
- console.log(`⏱️ [BROWSER HMR] Received update for ${data.url} at ${new Date().toTimeString().slice(0, 8)}.${String(Date.now() % 1000).padStart(3, '0')}`);
188
- }
189
-
190
- applyUpdate(data.url)
191
- .then((ok) => {
192
- if (!ok) {
193
- if (isDebug) console.warn(`⏱️ [BROWSER HMR] applyUpdate returned false, falling back to full reload!`);
194
- reload();
195
- } else {
196
- if (isDebug) {
197
- console.log(`⏱️ [BROWSER HMR] Fast Refresh finished applying in ${Date.now() - tRecv}ms!`);
198
- } else {
199
- console.log(`[ESM-HMR] hot updated: ${data.url}`);
200
- }
201
- }
202
- })
203
- .catch((err) => {
204
- console.error(`[ESM-HMR] applyUpdate error:`, err);
205
- reload();
206
- });
207
- });
208
-
209
- debug("listening for file changes...");
1
+ /**
2
+ * esm-hmr/client.js
3
+ * A client-side implementation of the ESM-HMR spec, for reference.
4
+ */
5
+
6
+ function debug(...args) {
7
+ console.log("[ESM-HMR]", ...args);
8
+ }
9
+
10
+ function reload() {
11
+ location.reload(true);
12
+ }
13
+
14
+ let SOCKET_MESSAGE_QUEUE = [];
15
+
16
+ function _sendSocketMessage(msg) {
17
+ socket.send(JSON.stringify(msg));
18
+ }
19
+
20
+ function sendSocketMessage(msg) {
21
+ if (socket.readyState !== socket.OPEN) {
22
+ SOCKET_MESSAGE_QUEUE.push(msg);
23
+ } else {
24
+ _sendSocketMessage(msg);
25
+ }
26
+ }
27
+
28
+ const socketURL =
29
+ window.HMR_WEBSOCKET_URL ||
30
+ (location.protocol === "http:" ? "ws://" : "wss://") + location.host + "/";
31
+ const socket = new WebSocket(socketURL, "esm-hmr");
32
+
33
+ socket.addEventListener("open", () => {
34
+ SOCKET_MESSAGE_QUEUE.forEach(_sendSocketMessage);
35
+ SOCKET_MESSAGE_QUEUE = [];
36
+ });
37
+
38
+ const REGISTERED_MODULES = {};
39
+ class HotModuleState {
40
+ constructor(id) {
41
+ this.id = id;
42
+ this.data = {};
43
+ this.isLocked = false;
44
+ this.isDeclined = false;
45
+ this.isAccepted = false;
46
+ this.acceptCallbacks = [];
47
+ this.disposeCallbacks = [];
48
+ }
49
+
50
+ lock() {
51
+ this.isLocked = true;
52
+ }
53
+
54
+ dispose(callback) {
55
+ this.disposeCallbacks.push(callback);
56
+ }
57
+
58
+ invalidate() {
59
+ reload();
60
+ }
61
+
62
+ decline() {
63
+ this.isDeclined = true;
64
+ }
65
+
66
+ accept(_deps = [], callback = true) {
67
+ if (this.isLocked) {
68
+ return;
69
+ }
70
+ if (!this.isAccepted) {
71
+ sendSocketMessage({ id: this.id, type: "hotAccept" });
72
+ this.isAccepted = true;
73
+ }
74
+ if (!Array.isArray(_deps)) {
75
+ callback = _deps || callback;
76
+ _deps = [];
77
+ }
78
+ if (callback === true) {
79
+ callback = () => {};
80
+ }
81
+ const deps = _deps.map((dep) => {
82
+ const ext = dep.split(".").pop();
83
+ if (!ext) {
84
+ dep += ".js";
85
+ } else if (ext !== "js") {
86
+ dep += ".proxy.js";
87
+ }
88
+ return new URL(dep, `${window.location.origin}${this.id}`).pathname;
89
+ });
90
+ this.acceptCallbacks.push({
91
+ deps,
92
+ callback,
93
+ });
94
+ }
95
+ }
96
+
97
+ export function createHotContext(id) {
98
+ const normId = id.startsWith("/") ? id : "/" + id;
99
+ const existing = REGISTERED_MODULES[normId] || REGISTERED_MODULES[id];
100
+ if (existing) {
101
+ existing.isLocked = false;
102
+ return existing;
103
+ }
104
+ const state = new HotModuleState(normId);
105
+ REGISTERED_MODULES[normId] = state;
106
+ REGISTERED_MODULES[id] = state;
107
+ return state;
108
+ }
109
+
110
+ async function applyUpdate(id) {
111
+ const normId = id.startsWith("/") ? id : "/" + id;
112
+ const state = REGISTERED_MODULES[normId] || REGISTERED_MODULES[id];
113
+ if (!state) {
114
+ return true;
115
+ }
116
+ if (state.isDeclined) {
117
+ return false;
118
+ }
119
+
120
+ const acceptCallbacks = state.acceptCallbacks;
121
+ const disposeCallbacks = state.disposeCallbacks;
122
+ state.disposeCallbacks = [];
123
+ state.data = {};
124
+
125
+ disposeCallbacks.forEach((callback) => callback());
126
+
127
+ const updateID = Date.now();
128
+ for (const { deps, callback: acceptCallback } of acceptCallbacks) {
129
+ const [module, ...depModules] = await Promise.all([
130
+ import(normId + `?mtime=${updateID}`),
131
+ ...deps.map((d) => {
132
+ const depId = d.startsWith("/") ? d : "/" + d;
133
+ return import(depId + `?mtime=${updateID}`);
134
+ }),
135
+ ]);
136
+ acceptCallback({ module, deps: depModules });
137
+ }
138
+
139
+ return true;
140
+ }
141
+
142
+ socket.addEventListener("message", ({ data: _data }) => {
143
+ if (!_data) return;
144
+
145
+ const data = JSON.parse(_data);
146
+ if (data.type === "reload") {
147
+ reload();
148
+ return;
149
+ }
150
+
151
+ if (data.type === "rsc-update") {
152
+ if (typeof window !== "undefined" && typeof window.__DINOU_ROUTER_REFRESH__ === "function") {
153
+ window.__DINOU_ROUTER_REFRESH__();
154
+ return;
155
+ }
156
+ reload();
157
+ return;
158
+ }
159
+
160
+ if (
161
+ data.type === "style-update" ||
162
+ (data.type === "update" && data.url && (data.url.endsWith(".css") || data.url.includes("styles.css")))
163
+ ) {
164
+ const targetUrl = data.url ? data.url.split("?")[0] : "";
165
+ const links = document.querySelectorAll('link[rel="stylesheet"]');
166
+ for (const link of links) {
167
+ const rawHref = link.getAttribute("href");
168
+ if (!rawHref) continue;
169
+ const cleanHref = rawHref.split("?")[0];
170
+ if (!targetUrl || cleanHref.endsWith(targetUrl) || targetUrl.endsWith(cleanHref) || cleanHref.includes("styles.css")) {
171
+ const nextUrl = new URL(link.href, window.location.origin);
172
+ nextUrl.searchParams.set("t", String(Date.now()));
173
+ link.href = nextUrl.href;
174
+ }
175
+ }
176
+ return;
177
+ }
178
+
179
+ if (data.type !== "update") {
180
+ return;
181
+ }
182
+
183
+ const tRecv = Date.now();
184
+ const isDebug = typeof window !== "undefined" && Boolean(window.__DINOU_DEBUG__);
185
+
186
+ if (isDebug) {
187
+ console.log(`⏱️ [BROWSER HMR] Received update for ${data.url} at ${new Date().toTimeString().slice(0, 8)}.${String(Date.now() % 1000).padStart(3, '0')}`);
188
+ }
189
+
190
+ applyUpdate(data.url)
191
+ .then((ok) => {
192
+ if (!ok) {
193
+ if (isDebug) console.warn(`⏱️ [BROWSER HMR] applyUpdate returned false, falling back to full reload!`);
194
+ reload();
195
+ } else {
196
+ if (isDebug) {
197
+ console.log(`⏱️ [BROWSER HMR] Fast Refresh finished applying in ${Date.now() - tRecv}ms!`);
198
+ } else {
199
+ console.log(`[ESM-HMR] hot updated: ${data.url}`);
200
+ }
201
+ }
202
+ })
203
+ .catch((err) => {
204
+ console.error(`[ESM-HMR] applyUpdate error:`, err);
205
+ reload();
206
+ });
207
+ });
208
+
209
+ debug("listening for file changes...");
package/dinou/index.d.ts CHANGED
@@ -327,6 +327,16 @@ export declare function defineConfig(config: DinouConfig): DinouConfig;
327
327
  */
328
328
  export declare function getContext(): RequestContextStore | undefined;
329
329
 
330
+ /**
331
+ * Opts the enclosing `<Suspense>` boundary out of becoming a Dynamic PPR Hole
332
+ * when wrapping an async Server Component on a PPR route (`ppr = true`).
333
+ *
334
+ * Call `noDynamic()` synchronously at the top of your async Server Component
335
+ * (before its first `await`) so Dinou resolves its promise at build time and
336
+ * bakes the rendered output directly into the Static Shell (`shell.html`).
337
+ */
338
+ export declare function noDynamic(): void;
339
+
330
340
  import type { ReactNode } from "react";
331
341
 
332
342
  // ====================================================================
package/dinou/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { defineConfig } from "./core/config.js";
2
2
  import { getContext } from "./core/request-context.js";
3
+ import { noDynamic } from "./core/no-dynamic.js";
3
4
  import {
4
5
  usePathname,
5
6
  useSearchParams,
@@ -14,6 +15,7 @@ import { Link } from "./core/link.jsx";
14
15
  const dinou = {
15
16
  defineConfig,
16
17
  getContext,
18
+ noDynamic,
17
19
  usePathname,
18
20
  useSearchParams,
19
21
  useRouter,
@@ -27,6 +29,7 @@ const dinou = {
27
29
  export {
28
30
  defineConfig,
29
31
  getContext,
32
+ noDynamic,
30
33
  usePathname,
31
34
  useSearchParams,
32
35
  useRouter,