@modern-js/plugin-data-loader 2.0.0-canary.5 → 2.0.0-canary.6

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.
@@ -46,8 +46,13 @@ import {
46
46
  import {
47
47
  matchRoutes
48
48
  } from "react-router-dom";
49
- import { LOADER_ROUTES_DIR, MAIN_ENTRY_NAME } from "@modern-js/utils";
49
+ import {
50
+ createDebugger,
51
+ LOADER_ROUTES_DIR,
52
+ MAIN_ENTRY_NAME
53
+ } from "@modern-js/utils";
50
54
  import { LOADER_SEARCH_PARAM } from "../common/constants";
55
+ const debug = createDebugger("plugin-data-loader");
51
56
  installGlobals();
52
57
  const redirectStatusCodes = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
53
58
  function isRedirectResponse(response) {
@@ -73,6 +78,7 @@ const callRouteLoader = (_0) => __async(void 0, [_0], function* ({
73
78
  request,
74
79
  loadContext
75
80
  }) {
81
+ debug("callRouteLoader", routeId, params);
76
82
  if (!loader) {
77
83
  throw new Error(
78
84
  `You made a ${request.method} request to ${request.url} but did not provide a default component or \`loader\` for route "${routeId}", so there is no way to handle the request.`
@@ -124,6 +130,7 @@ const createLoaderRequest = (context) => {
124
130
  return new Request(url.href, init);
125
131
  };
126
132
  const sendLoaderResponse = (res, nodeResponse) => __async(void 0, null, function* () {
133
+ debug("sendLoaderResponse");
127
134
  res.statusMessage = nodeResponse.statusText;
128
135
  res.statusCode = nodeResponse.status;
129
136
  for (const [key, value] of nodeResponse.headers.entries()) {
@@ -150,12 +157,15 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
150
157
  distDir
151
158
  }) {
152
159
  const { method, query } = context;
160
+ debug("handleRequest start");
153
161
  const routeId = query[LOADER_SEARCH_PARAM];
154
162
  if (!routeId || method.toLowerCase() !== "get") {
163
+ debug("routId not found");
155
164
  return;
156
165
  }
157
166
  const entry = matchEntry(context.path, serverRoutes);
158
167
  if (!entry) {
168
+ debug("entry not found");
159
169
  return;
160
170
  }
161
171
  const routesPath = path.join(
@@ -165,6 +175,7 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
165
175
  );
166
176
  const { routes } = yield import(routesPath);
167
177
  if (!routes) {
178
+ debug("routes not found");
168
179
  return;
169
180
  }
170
181
  const { res } = context;
@@ -192,6 +203,7 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
192
203
  loadContext: {}
193
204
  });
194
205
  if (isRedirectResponse(response)) {
206
+ debug("redirect response, location is", response.headers.get("Location"));
195
207
  const headers = new Headers(response.headers);
196
208
  headers.set("X-Modernjs-Redirect", headers.get("Location"));
197
209
  headers.delete("Location");
@@ -208,6 +220,7 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
208
220
  "Content-Type": "text/plain"
209
221
  }
210
222
  });
223
+ debug("error response", error);
211
224
  }
212
225
  sendLoaderResponse(res, response);
213
226
  });
@@ -72,6 +72,7 @@ var import_node = require("@remix-run/node");
72
72
  var import_react_router_dom = require("react-router-dom");
73
73
  var import_utils = require("@modern-js/utils");
74
74
  var import_constants = require("../common/constants");
75
+ const debug = (0, import_utils.createDebugger)("plugin-data-loader");
75
76
  (0, import_node.installGlobals)();
76
77
  const redirectStatusCodes = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
77
78
  function isRedirectResponse(response) {
@@ -97,6 +98,7 @@ const callRouteLoader = (_0) => __async(void 0, [_0], function* ({
97
98
  request,
98
99
  loadContext
99
100
  }) {
101
+ debug("callRouteLoader", routeId, params);
100
102
  if (!loader) {
101
103
  throw new Error(
102
104
  `You made a ${request.method} request to ${request.url} but did not provide a default component or \`loader\` for route "${routeId}", so there is no way to handle the request.`
@@ -148,6 +150,7 @@ const createLoaderRequest = (context) => {
148
150
  return new Request(url.href, init);
149
151
  };
150
152
  const sendLoaderResponse = (res, nodeResponse) => __async(void 0, null, function* () {
153
+ debug("sendLoaderResponse");
151
154
  res.statusMessage = nodeResponse.statusText;
152
155
  res.statusCode = nodeResponse.status;
153
156
  for (const [key, value] of nodeResponse.headers.entries()) {
@@ -174,12 +177,15 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
174
177
  distDir
175
178
  }) {
176
179
  const { method, query } = context;
180
+ debug("handleRequest start");
177
181
  const routeId = query[import_constants.LOADER_SEARCH_PARAM];
178
182
  if (!routeId || method.toLowerCase() !== "get") {
183
+ debug("routId not found");
179
184
  return;
180
185
  }
181
186
  const entry = matchEntry(context.path, serverRoutes);
182
187
  if (!entry) {
188
+ debug("entry not found");
183
189
  return;
184
190
  }
185
191
  const routesPath = import_path.default.join(
@@ -189,6 +195,7 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
189
195
  );
190
196
  const { routes } = yield Promise.resolve().then(() => __toESM(require(routesPath)));
191
197
  if (!routes) {
198
+ debug("routes not found");
192
199
  return;
193
200
  }
194
201
  const { res } = context;
@@ -216,6 +223,7 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
216
223
  loadContext: {}
217
224
  });
218
225
  if (isRedirectResponse(response)) {
226
+ debug("redirect response, location is", response.headers.get("Location"));
219
227
  const headers = new Headers(response.headers);
220
228
  headers.set("X-Modernjs-Redirect", headers.get("Location"));
221
229
  headers.delete("Location");
@@ -232,6 +240,7 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
232
240
  "Content-Type": "text/plain"
233
241
  }
234
242
  });
243
+ debug("error response", error);
235
244
  }
236
245
  sendLoaderResponse(res, response);
237
246
  });
@@ -223,8 +223,9 @@ var __generator = this && this.__generator || function(thisArg, body) {
223
223
  import path from "path";
224
224
  import { installGlobals, writeReadableStreamToWritable, Response as NodeResponse } from "@remix-run/node";
225
225
  import { matchRoutes } from "react-router-dom";
226
- import { LOADER_ROUTES_DIR, MAIN_ENTRY_NAME } from "@modern-js/utils";
226
+ import { createDebugger, LOADER_ROUTES_DIR, MAIN_ENTRY_NAME } from "@modern-js/utils";
227
227
  import { LOADER_SEARCH_PARAM } from "../common/constants";
228
+ var debug = createDebugger("plugin-data-loader");
228
229
  installGlobals();
229
230
  var redirectStatusCodes = /* @__PURE__ */ new Set([
230
231
  301,
@@ -259,6 +260,7 @@ var callRouteLoader = function() {
259
260
  switch(_state.label){
260
261
  case 0:
261
262
  routeId = param.routeId, loader = param.loader, params = param.params, request = param.request, loadContext = param.loadContext;
263
+ debug("callRouteLoader", routeId, params);
262
264
  if (!loader) {
263
265
  throw new Error("You made a ".concat(request.method, " request to ").concat(request.url, ' but did not provide a default component or `loader` for route "').concat(routeId, '", so there is no way to handle the request.'));
264
266
  }
@@ -374,6 +376,7 @@ var sendLoaderResponse = function() {
374
376
  return __generator(this, function(_state) {
375
377
  switch(_state.label){
376
378
  case 0:
379
+ debug("sendLoaderResponse");
377
380
  res.statusMessage = nodeResponse.statusText;
378
381
  res.statusCode = nodeResponse.status;
379
382
  _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
@@ -443,14 +446,17 @@ var handleRequest = function() {
443
446
  case 0:
444
447
  context = param.context, serverRoutes = param.serverRoutes, distDir = param.distDir;
445
448
  method = context.method, query = context.query;
449
+ debug("handleRequest start");
446
450
  routeId = query[LOADER_SEARCH_PARAM];
447
451
  if (!routeId || method.toLowerCase() !== "get") {
452
+ debug("routId not found");
448
453
  return [
449
454
  2
450
455
  ];
451
456
  }
452
457
  entry = matchEntry(context.path, serverRoutes);
453
458
  if (!entry) {
459
+ debug("entry not found");
454
460
  return [
455
461
  2
456
462
  ];
@@ -463,6 +469,7 @@ var handleRequest = function() {
463
469
  case 1:
464
470
  routes = _state.sent().routes;
465
471
  if (!routes) {
472
+ debug("routes not found");
466
473
  return [
467
474
  2
468
475
  ];
@@ -509,6 +516,7 @@ var handleRequest = function() {
509
516
  case 3:
510
517
  response = _state.sent();
511
518
  if (isRedirectResponse(response)) {
519
+ debug("redirect response, location is", response.headers.get("Location"));
512
520
  headers = new Headers(response.headers);
513
521
  headers.set("X-Modernjs-Redirect", headers.get("Location"));
514
522
  headers.delete("Location");
@@ -530,6 +538,7 @@ var handleRequest = function() {
530
538
  "Content-Type": "text/plain"
531
539
  }
532
540
  });
541
+ debug("error response", error);
533
542
  return [
534
543
  3,
535
544
  5
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.0.0-canary.5",
14
+ "version": "2.0.0-canary.6",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",