@analogjs/vite-plugin-nitro 2.4.0-beta.9 → 3.0.0-alpha.2

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,3 +1,42 @@
1
- export declare const ssrRenderer = "\nimport { eventHandler, getResponseHeader } from 'h3';\n// @ts-ignore\nimport renderer from '#analog/ssr';\n// @ts-ignore\nimport template from '#analog/index';\n\nexport default eventHandler(async (event) => {\n const noSSR = getResponseHeader(event, 'x-analog-no-ssr');\n\n if (noSSR === 'true') {\n return template;\n }\n\n const html = await renderer(event.node.req.url, template, {\n req: event.node.req,\n res: event.node.res,\n });\n\n return html;\n});";
2
- export declare const clientRenderer = "\nimport { eventHandler } from 'h3';\n\n// @ts-ignore\nimport template from '#analog/index';\n\nexport default eventHandler(async () => {\n return template;\n});\n";
3
- export declare const apiMiddleware = "\nimport { eventHandler, proxyRequest } from 'h3';\nimport { useRuntimeConfig } from '#imports';\n\nexport default eventHandler(async (event) => {\n const prefix = useRuntimeConfig().prefix;\n const apiPrefix = `${prefix}/${useRuntimeConfig().apiPrefix}`;\n\n if (event.node.req.url?.startsWith(apiPrefix)) {\n const reqUrl = event.node.req.url?.replace(apiPrefix, '');\n\n if (\n event.node.req.method === 'GET' &&\n // in the case of XML routes, we want to proxy the request so that nitro gets the correct headers\n // and can render the XML correctly as a static asset\n !event.node.req.url?.endsWith('.xml')\n ) {\n return $fetch(reqUrl, { headers: event.node.req.headers });\n }\n\n return proxyRequest(event, reqUrl, {\n // @ts-ignore\n fetch: $fetch.native,\n });\n }\n});";
1
+ /**
2
+ * SSR renderer virtual module content.
3
+ *
4
+ * This code runs inside Nitro's server runtime (Node.js context) where
5
+ * event.node is always populated. In h3 v2, event.node is typed as optional,
6
+ * so we use h3's first-class event properties (event.path, event.method) where
7
+ * possible and apply optional chaining when accessing the Node.js context for
8
+ * the Angular renderer which requires raw req/res objects.
9
+ *
10
+ * h3 v2 idiomatic APIs used:
11
+ * - defineHandler (replaces defineEventHandler / eventHandler)
12
+ * - event.path (replaces event.node.req.url)
13
+ * - getResponseHeader compat shim (still available in h3 v2)
14
+ */
15
+ export declare function ssrRenderer(templatePath: string): string;
16
+ /**
17
+ * Client-only renderer virtual module content.
18
+ *
19
+ * Used when SSR is disabled — simply serves the static index.html template
20
+ * for every route, letting the client-side Angular router handle navigation.
21
+ */
22
+ export declare function clientRenderer(templatePath: string): string;
23
+ /**
24
+ * API middleware virtual module content.
25
+ *
26
+ * Intercepts requests matching the configured API prefix and either:
27
+ * - Uses event-bound internal forwarding for GET requests (except .xml routes)
28
+ * - Uses request proxying for all other methods to forward the full request
29
+ *
30
+ * h3 v2 idiomatic APIs used:
31
+ * - defineHandler (replaces defineEventHandler / eventHandler)
32
+ * - event.path (replaces event.node.req.url)
33
+ * - event.method (replaces event.node.req.method)
34
+ * - proxyRequest is retained internally because it preserves Nitro route
35
+ * matching for event-bound server requests during SSR/prerender
36
+ * - Object.fromEntries(event.req.headers.entries()) replaces direct event.node.req.headers access
37
+ *
38
+ * `fetchWithEvent` keeps the active event context while forwarding to a
39
+ * rewritten path, which avoids falling through to the HTML renderer when
40
+ * SSR code makes relative API requests.
41
+ */
42
+ export declare const apiMiddleware = "\nimport { defineHandler, fetchWithEvent, proxyRequest } from 'nitro/h3';\nimport { useRuntimeConfig } from 'nitro/runtime-config';\n\nexport default defineHandler(async (event) => {\n const prefix = useRuntimeConfig().prefix;\n const apiPrefix = `${prefix}/${useRuntimeConfig().apiPrefix}`;\n\n if (event.path?.startsWith(apiPrefix)) {\n const reqUrl = event.path?.replace(apiPrefix, '');\n\n if (\n event.method === 'GET' &&\n // in the case of XML routes, we want to proxy the request so that nitro gets the correct headers\n // and can render the XML correctly as a static asset\n !event.path?.endsWith('.xml')\n ) {\n return fetchWithEvent(event, reqUrl, {\n headers: Object.fromEntries(event.req.headers.entries()),\n });\n }\n\n return proxyRequest(event, reqUrl);\n }\n});";
@@ -1,58 +1,130 @@
1
- export const ssrRenderer = `
2
- import { eventHandler, getResponseHeader } from 'h3';
1
+ /**
2
+ * SSR renderer virtual module content.
3
+ *
4
+ * This code runs inside Nitro's server runtime (Node.js context) where
5
+ * event.node is always populated. In h3 v2, event.node is typed as optional,
6
+ * so we use h3's first-class event properties (event.path, event.method) where
7
+ * possible and apply optional chaining when accessing the Node.js context for
8
+ * the Angular renderer which requires raw req/res objects.
9
+ *
10
+ * h3 v2 idiomatic APIs used:
11
+ * - defineHandler (replaces defineEventHandler / eventHandler)
12
+ * - event.path (replaces event.node.req.url)
13
+ * - getResponseHeader compat shim (still available in h3 v2)
14
+ */
15
+ export function ssrRenderer(templatePath) {
16
+ return `
17
+ import { readFileSync } from 'node:fs';
18
+ import { createFetch } from 'ofetch';
19
+ import { defineHandler, fetchWithEvent } from 'nitro/h3';
3
20
  // @ts-ignore
4
21
  import renderer from '#analog/ssr';
5
- // @ts-ignore
6
- import template from '#analog/index';
7
22
 
8
- export default eventHandler(async (event) => {
9
- const noSSR = getResponseHeader(event, 'x-analog-no-ssr');
23
+ const template = readFileSync(${JSON.stringify(templatePath)}, 'utf8');
24
+ const normalizeHtmlRequestUrl = (url) =>
25
+ url.replace(/\\/index\\.html(?=$|[?#])/, '/');
26
+
27
+ export default defineHandler(async (event) => {
28
+ event.res.headers.set('content-type', 'text/html; charset=utf-8');
29
+ const noSSR = event.res.headers.get('x-analog-no-ssr');
30
+ const requestPath = normalizeHtmlRequestUrl(event.path);
10
31
 
11
32
  if (noSSR === 'true') {
12
33
  return template;
13
34
  }
14
35
 
15
- const html = await renderer(event.node.req.url, template, {
16
- req: event.node.req,
17
- res: event.node.res,
36
+ // event.path is the canonical h3 v2 way to access the request URL.
37
+ // event.node?.req and event.node?.res are needed by the Angular SSR renderer
38
+ // which operates on raw Node.js request/response objects.
39
+ // During prerendering (Nitro v3 fetch-based pipeline), event.node is undefined.
40
+ // The Angular renderer requires a req object with at least { headers, url },
41
+ // so we provide a minimal stub to avoid runtime errors in prerender context.
42
+ const req = event.node?.req
43
+ ? {
44
+ ...event.node.req,
45
+ url: requestPath,
46
+ originalUrl: requestPath,
47
+ }
48
+ : {
49
+ headers: { host: 'localhost' },
50
+ url: requestPath,
51
+ originalUrl: requestPath,
52
+ connection: {},
53
+ };
54
+ const res = event.node?.res;
55
+ const fetch = createFetch({
56
+ fetch: (resource, init) => {
57
+ const url = resource instanceof Request ? resource.url : resource.toString();
58
+ return fetchWithEvent(event, url, init);
59
+ }
18
60
  });
19
61
 
62
+ const html = await renderer(requestPath, template, { req, res, fetch });
63
+
20
64
  return html;
21
65
  });`;
22
- export const clientRenderer = `
23
- import { eventHandler } from 'h3';
66
+ }
67
+ /**
68
+ * Client-only renderer virtual module content.
69
+ *
70
+ * Used when SSR is disabled — simply serves the static index.html template
71
+ * for every route, letting the client-side Angular router handle navigation.
72
+ */
73
+ export function clientRenderer(templatePath) {
74
+ return `
75
+ import { readFileSync } from 'node:fs';
76
+ import { defineHandler } from 'nitro/h3';
24
77
 
25
- // @ts-ignore
26
- import template from '#analog/index';
78
+ const template = readFileSync(${JSON.stringify(templatePath)}, 'utf8');
27
79
 
28
- export default eventHandler(async () => {
80
+ export default defineHandler(async (event) => {
81
+ event.res.headers.set('content-type', 'text/html; charset=utf-8');
29
82
  return template;
30
83
  });
31
84
  `;
85
+ }
86
+ /**
87
+ * API middleware virtual module content.
88
+ *
89
+ * Intercepts requests matching the configured API prefix and either:
90
+ * - Uses event-bound internal forwarding for GET requests (except .xml routes)
91
+ * - Uses request proxying for all other methods to forward the full request
92
+ *
93
+ * h3 v2 idiomatic APIs used:
94
+ * - defineHandler (replaces defineEventHandler / eventHandler)
95
+ * - event.path (replaces event.node.req.url)
96
+ * - event.method (replaces event.node.req.method)
97
+ * - proxyRequest is retained internally because it preserves Nitro route
98
+ * matching for event-bound server requests during SSR/prerender
99
+ * - Object.fromEntries(event.req.headers.entries()) replaces direct event.node.req.headers access
100
+ *
101
+ * `fetchWithEvent` keeps the active event context while forwarding to a
102
+ * rewritten path, which avoids falling through to the HTML renderer when
103
+ * SSR code makes relative API requests.
104
+ */
32
105
  export const apiMiddleware = `
33
- import { eventHandler, proxyRequest } from 'h3';
34
- import { useRuntimeConfig } from '#imports';
106
+ import { defineHandler, fetchWithEvent, proxyRequest } from 'nitro/h3';
107
+ import { useRuntimeConfig } from 'nitro/runtime-config';
35
108
 
36
- export default eventHandler(async (event) => {
109
+ export default defineHandler(async (event) => {
37
110
  const prefix = useRuntimeConfig().prefix;
38
111
  const apiPrefix = \`\${prefix}/\${useRuntimeConfig().apiPrefix}\`;
39
112
 
40
- if (event.node.req.url?.startsWith(apiPrefix)) {
41
- const reqUrl = event.node.req.url?.replace(apiPrefix, '');
113
+ if (event.path?.startsWith(apiPrefix)) {
114
+ const reqUrl = event.path?.replace(apiPrefix, '');
42
115
 
43
116
  if (
44
- event.node.req.method === 'GET' &&
117
+ event.method === 'GET' &&
45
118
  // in the case of XML routes, we want to proxy the request so that nitro gets the correct headers
46
119
  // and can render the XML correctly as a static asset
47
- !event.node.req.url?.endsWith('.xml')
120
+ !event.path?.endsWith('.xml')
48
121
  ) {
49
- return $fetch(reqUrl, { headers: event.node.req.headers });
122
+ return fetchWithEvent(event, reqUrl, {
123
+ headers: Object.fromEntries(event.req.headers.entries()),
124
+ });
50
125
  }
51
126
 
52
- return proxyRequest(event, reqUrl, {
53
- // @ts-ignore
54
- fetch: $fetch.native,
55
- });
127
+ return proxyRequest(event, reqUrl);
56
128
  }
57
129
  });`;
58
130
  //# sourceMappingURL=renderers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"renderers.js","sourceRoot":"","sources":["../../../../../../packages/vite-plugin-nitro/src/lib/utils/renderers.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG;;;;;;;;;;;;;;;;;;;;IAoBvB,CAAC;AAEL,MAAM,CAAC,MAAM,cAAc,GAAG;;;;;;;;;CAS7B,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;IAyBzB,CAAC"}
1
+ {"version":3,"file":"renderers.js","sourceRoot":"","sources":["../../../../../../packages/vite-plugin-nitro/src/lib/utils/renderers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,WAAW,CAAC,YAAoB;IAC9C,OAAO;;;;;;;gCAOuB,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA0CxD,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,YAAoB;IACjD,OAAO;;;;gCAIuB,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC;;;;;;CAM3D,CAAC;AACF,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG;;;;;;;;;;;;;;;;;;;;;;;;IAwBzB,CAAC"}
@@ -1,4 +1,4 @@
1
- import { NitroConfig } from 'nitropack';
1
+ import type { NitroConfig } from 'nitro/types';
2
2
  import type { Plugin } from 'vite';
3
3
  import { Options } from './options.js';
4
4
  export declare function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[];