@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.
- package/README.md +2 -2
- package/package.json +3 -2
- package/src/index.d.ts +1 -1
- package/src/lib/build-server.d.ts +1 -1
- package/src/lib/build-server.js +79 -3
- package/src/lib/build-server.js.map +1 -1
- package/src/lib/hooks/post-rendering-hook.d.ts +1 -1
- package/src/lib/options.d.ts +1 -1
- package/src/lib/plugins/dev-server-plugin.js +4 -2
- package/src/lib/plugins/dev-server-plugin.js.map +1 -1
- package/src/lib/plugins/page-endpoints.js +33 -10
- package/src/lib/plugins/page-endpoints.js.map +1 -1
- package/src/lib/utils/get-content-files.js +1 -4
- package/src/lib/utils/get-content-files.js.map +1 -1
- package/src/lib/utils/get-page-handlers.d.ts +1 -1
- package/src/lib/utils/get-page-handlers.js +5 -3
- package/src/lib/utils/get-page-handlers.js.map +1 -1
- package/src/lib/utils/node-web-bridge.d.ts +3 -0
- package/src/lib/utils/node-web-bridge.js +50 -0
- package/src/lib/utils/node-web-bridge.js.map +1 -0
- package/src/lib/utils/register-dev-middleware.d.ts +4 -32
- package/src/lib/utils/register-dev-middleware.js +22 -43
- package/src/lib/utils/register-dev-middleware.js.map +1 -1
- package/src/lib/utils/renderers.d.ts +42 -3
- package/src/lib/utils/renderers.js +98 -26
- package/src/lib/utils/renderers.js.map +1 -1
- package/src/lib/vite-plugin-nitro.d.ts +1 -1
- package/src/lib/vite-plugin-nitro.js +322 -69
- package/src/lib/vite-plugin-nitro.js.map +1 -1
|
@@ -1,3 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
2
|
-
|
|
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
|
-
|
|
9
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
23
|
-
|
|
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
|
-
|
|
26
|
-
import template from '#analog/index';
|
|
78
|
+
const template = readFileSync(${JSON.stringify(templatePath)}, 'utf8');
|
|
27
79
|
|
|
28
|
-
export default
|
|
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 {
|
|
34
|
-
import { useRuntimeConfig } from '
|
|
106
|
+
import { defineHandler, fetchWithEvent, proxyRequest } from 'nitro/h3';
|
|
107
|
+
import { useRuntimeConfig } from 'nitro/runtime-config';
|
|
35
108
|
|
|
36
|
-
export default
|
|
109
|
+
export default defineHandler(async (event) => {
|
|
37
110
|
const prefix = useRuntimeConfig().prefix;
|
|
38
111
|
const apiPrefix = \`\${prefix}/\${useRuntimeConfig().apiPrefix}\`;
|
|
39
112
|
|
|
40
|
-
if (event.
|
|
41
|
-
const reqUrl = event.
|
|
113
|
+
if (event.path?.startsWith(apiPrefix)) {
|
|
114
|
+
const reqUrl = event.path?.replace(apiPrefix, '');
|
|
42
115
|
|
|
43
116
|
if (
|
|
44
|
-
event.
|
|
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.
|
|
120
|
+
!event.path?.endsWith('.xml')
|
|
48
121
|
) {
|
|
49
|
-
return
|
|
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,
|
|
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"}
|