@maravilla-labs/adapter-react-router 0.1.26 → 0.1.27

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.
@@ -3,6 +3,11 @@
3
3
  *
4
4
  * The __SERVER_BUILD__ placeholder is replaced at build time with the path
5
5
  * to the React Router server build output.
6
+ *
7
+ * NOTE: No top-level await or ES module import statements — our runtime
8
+ * wraps bundles in an IIFE which doesn't support them. esbuild resolves
9
+ * all imports at bundle time. Lazy initialization in handleRequest avoids
10
+ * top-level await.
6
11
  */
7
- export declare const WORKER_TEMPLATE = "// Maravilla Runtime Worker Polyfills\n// Shared across all framework adapters\n\nconst originalFetch = globalThis.fetch;\nconst OriginalRequest = globalThis.Request;\n\nclass EnhancedRequest extends OriginalRequest {\n constructor(input, init) {\n if (typeof input === 'string') {\n try {\n super(input, init);\n } catch (e) {\n if (globalThis.location && e.message && e.message.includes('relative')) {\n try {\n const absoluteUrl = new URL(input, globalThis.location.href);\n super(absoluteUrl.href, init);\n } catch (e2) {\n throw e;\n }\n } else {\n throw e;\n }\n }\n return;\n }\n\n if (input && typeof input === 'object' && !(input instanceof OriginalRequest)) {\n const url = input.url || input.href || input.toString();\n if (!url) {\n throw new TypeError('Request must have a URL');\n }\n const newInit = {\n method: input.method || init?.method || 'GET',\n headers: input.headers || init?.headers,\n body: input.body || init?.body,\n mode: input.mode || init?.mode,\n credentials: input.credentials || init?.credentials,\n cache: input.cache || init?.cache,\n redirect: input.redirect || init?.redirect,\n referrer: input.referrer || init?.referrer,\n referrerPolicy: input.referrerPolicy || init?.referrerPolicy,\n integrity: input.integrity || init?.integrity,\n keepalive: input.keepalive || init?.keepalive,\n signal: input.signal || init?.signal,\n ...init\n };\n super(url, newInit);\n return;\n }\n\n super(input, init);\n }\n}\n\nglobalThis.Request = EnhancedRequest;\n\nglobalThis.fetch = function enhancedFetch(input, init) {\n let request;\n\n if (input instanceof Request) {\n request = input;\n } else if (typeof input === 'string') {\n if (input.startsWith('http://') || input.startsWith('https://')) {\n request = new Request(input, init);\n } else if (globalThis.location) {\n try {\n const absoluteUrl = new URL(input, globalThis.location.href);\n request = new Request(absoluteUrl.href, init);\n } catch (e) {\n console.error('Failed to resolve relative URL:', input, 'against', globalThis.location.href);\n throw e;\n }\n } else {\n console.error('Cannot resolve relative URL without globalThis.location:', input);\n throw new Error('Cannot resolve relative URL without a base URL');\n }\n } else if (input && typeof input === 'object') {\n request = new Request(input, init);\n } else {\n throw new TypeError('First argument must be a string, Request, or URL');\n }\n\n return originalFetch(request);\n};\n\n\n// Import the React Router server build\nimport * as build from '__SERVER_BUILD__';\n\n// createRequestHandler comes from the React Router core package.\n// It accepts a server build and returns a fetch-compatible handler.\nconst { createRequestHandler } = await import('react-router');\n\nconst handler = createRequestHandler(build, 'production');\n\n/**\n * Main request handler for the Maravilla runtime.\n * Called by the runtime for each incoming HTTP request.\n */\nasync function handleRequest(request) {\n // Re-apply polyfills if needed (worker reuse)\n if (globalThis.Request !== EnhancedRequest) {\n globalThis.Request = EnhancedRequest;\n globalThis.fetch = enhancedFetch;\n }\n\n // Build the load context that React Router passes to loaders/actions.\n // Users access this via: export async function loader({ context }) { ... }\n const loadContext = {\n platform: globalThis.platform,\n env: globalThis.platform?.env || {},\n kv: globalThis.platform?.kv,\n db: globalThis.platform?.db,\n storage: globalThis.platform?.storage,\n queue: globalThis.platform?.queue,\n };\n\n return handler(request, loadContext);\n}\n\nglobalThis.handleRequest = handleRequest;\n";
12
+ export declare const WORKER_TEMPLATE = "// Maravilla Runtime Worker Polyfills\n// Shared across all framework adapters\n\nconst originalFetch = globalThis.fetch;\nconst OriginalRequest = globalThis.Request;\n\nclass EnhancedRequest extends OriginalRequest {\n constructor(input, init) {\n if (typeof input === 'string') {\n try {\n super(input, init);\n } catch (e) {\n if (globalThis.location && e.message && e.message.includes('relative')) {\n try {\n const absoluteUrl = new URL(input, globalThis.location.href);\n super(absoluteUrl.href, init);\n } catch (e2) {\n throw e;\n }\n } else {\n throw e;\n }\n }\n return;\n }\n\n if (input && typeof input === 'object' && !(input instanceof OriginalRequest)) {\n const url = input.url || input.href || input.toString();\n if (!url) {\n throw new TypeError('Request must have a URL');\n }\n const newInit = {\n method: input.method || init?.method || 'GET',\n headers: input.headers || init?.headers,\n body: input.body || init?.body,\n mode: input.mode || init?.mode,\n credentials: input.credentials || init?.credentials,\n cache: input.cache || init?.cache,\n redirect: input.redirect || init?.redirect,\n referrer: input.referrer || init?.referrer,\n referrerPolicy: input.referrerPolicy || init?.referrerPolicy,\n integrity: input.integrity || init?.integrity,\n keepalive: input.keepalive || init?.keepalive,\n signal: input.signal || init?.signal,\n ...init\n };\n super(url, newInit);\n return;\n }\n\n super(input, init);\n }\n}\n\nglobalThis.Request = EnhancedRequest;\n\nglobalThis.fetch = function enhancedFetch(input, init) {\n let request;\n\n if (input instanceof Request) {\n request = input;\n } else if (typeof input === 'string') {\n if (input.startsWith('http://') || input.startsWith('https://')) {\n request = new Request(input, init);\n } else if (globalThis.location) {\n try {\n const absoluteUrl = new URL(input, globalThis.location.href);\n request = new Request(absoluteUrl.href, init);\n } catch (e) {\n console.error('Failed to resolve relative URL:', input, 'against', globalThis.location.href);\n throw e;\n }\n } else {\n console.error('Cannot resolve relative URL without globalThis.location:', input);\n throw new Error('Cannot resolve relative URL without a base URL');\n }\n } else if (input && typeof input === 'object') {\n request = new Request(input, init);\n } else {\n throw new TypeError('First argument must be a string, Request, or URL');\n }\n\n return originalFetch(request);\n};\n\n\n// Import the React Router server build (resolved by esbuild at bundle time)\nimport * as build from '__SERVER_BUILD__';\nimport { createRequestHandler } from 'react-router';\n\n// Lazy-init the handler on first request (avoids top-level await)\nlet handler;\n\nasync function handleRequest(request) {\n if (!handler) {\n handler = createRequestHandler(build, 'production');\n }\n\n // Re-apply polyfills if needed (worker reuse)\n if (globalThis.Request !== EnhancedRequest) {\n globalThis.Request = EnhancedRequest;\n globalThis.fetch = enhancedFetch;\n }\n\n // Build the load context that React Router passes to loaders/actions.\n // Users access this via: export async function loader({ context }) { ... }\n const loadContext = {\n platform: globalThis.platform,\n env: globalThis.platform?.env || {},\n kv: globalThis.platform?.kv,\n db: globalThis.platform?.db,\n storage: globalThis.platform?.storage,\n queue: globalThis.platform?.queue,\n };\n\n try {\n const response = await handler(request, loadContext);\n console.log('[maravilla] Response status:', response.status);\n if (response.status >= 500) {\n const body = await response.clone().text();\n console.error('[maravilla] 500 response body:', body || '(empty)');\n }\n return response;\n } catch (error) {\n console.error('[maravilla] handleRequest error:', error?.message || error);\n console.error('[maravilla] stack:', error?.stack || 'no stack');\n throw error;\n }\n}\n\nglobalThis.handleRequest = handleRequest;\n";
8
13
  //# sourceMappingURL=worker-template.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"worker-template.d.ts","sourceRoot":"","sources":["../src/worker-template.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,eAAO,MAAM,eAAe,q6HAqC3B,CAAC"}
1
+ {"version":3,"file":"worker-template.d.ts","sourceRoot":"","sources":["../src/worker-template.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,swIA+C3B,CAAC"}
@@ -4,23 +4,26 @@ import { WORKER_POLYFILLS } from '@maravilla-labs/adapter-core';
4
4
  *
5
5
  * The __SERVER_BUILD__ placeholder is replaced at build time with the path
6
6
  * to the React Router server build output.
7
+ *
8
+ * NOTE: No top-level await or ES module import statements — our runtime
9
+ * wraps bundles in an IIFE which doesn't support them. esbuild resolves
10
+ * all imports at bundle time. Lazy initialization in handleRequest avoids
11
+ * top-level await.
7
12
  */
8
13
  export const WORKER_TEMPLATE = `${WORKER_POLYFILLS}
9
14
 
10
- // Import the React Router server build
15
+ // Import the React Router server build (resolved by esbuild at bundle time)
11
16
  import * as build from '__SERVER_BUILD__';
17
+ import { createRequestHandler } from 'react-router';
12
18
 
13
- // createRequestHandler comes from the React Router core package.
14
- // It accepts a server build and returns a fetch-compatible handler.
15
- const { createRequestHandler } = await import('react-router');
16
-
17
- const handler = createRequestHandler(build, 'production');
19
+ // Lazy-init the handler on first request (avoids top-level await)
20
+ let handler;
18
21
 
19
- /**
20
- * Main request handler for the Maravilla runtime.
21
- * Called by the runtime for each incoming HTTP request.
22
- */
23
22
  async function handleRequest(request) {
23
+ if (!handler) {
24
+ handler = createRequestHandler(build, 'production');
25
+ }
26
+
24
27
  // Re-apply polyfills if needed (worker reuse)
25
28
  if (globalThis.Request !== EnhancedRequest) {
26
29
  globalThis.Request = EnhancedRequest;
@@ -38,7 +41,19 @@ async function handleRequest(request) {
38
41
  queue: globalThis.platform?.queue,
39
42
  };
40
43
 
41
- return handler(request, loadContext);
44
+ try {
45
+ const response = await handler(request, loadContext);
46
+ console.log('[maravilla] Response status:', response.status);
47
+ if (response.status >= 500) {
48
+ const body = await response.clone().text();
49
+ console.error('[maravilla] 500 response body:', body || '(empty)');
50
+ }
51
+ return response;
52
+ } catch (error) {
53
+ console.error('[maravilla] handleRequest error:', error?.message || error);
54
+ console.error('[maravilla] stack:', error?.stack || 'no stack');
55
+ throw error;
56
+ }
42
57
  }
43
58
 
44
59
  globalThis.handleRequest = handleRequest;
@@ -1 +1 @@
1
- {"version":3,"file":"worker-template.js","sourceRoot":"","sources":["../src/worker-template.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAEhE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqCjD,CAAC"}
1
+ {"version":3,"file":"worker-template.js","sourceRoot":"","sources":["../src/worker-template.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAEhE;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+CjD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maravilla-labs/adapter-react-router",
3
- "version": "0.1.26",
3
+ "version": "0.1.27",
4
4
  "description": "React Router 7 adapter for Maravilla Runtime",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",