dtwo 2.17.0 → 3.0.0-rc.14
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/LICENSE +1 -1
- package/README.md +2 -41
- package/app.d.ts +1 -0
- package/bin/dtwo.mjs +2 -0
- package/config.cjs +7 -0
- package/config.d.ts +4 -0
- package/config.mjs +5 -0
- package/dist/app/compat/capi.d.ts +4 -0
- package/dist/app/compat/capi.mjs +19 -0
- package/dist/app/compat/kdu-demi.d.ts +4 -0
- package/dist/app/compat/kdu-demi.mjs +4 -0
- package/dist/app/components/client-only.d.mts +22 -0
- package/dist/app/components/client-only.mjs +75 -0
- package/dist/app/components/dev-only.d.mts +4 -0
- package/dist/app/components/dev-only.mjs +11 -0
- package/dist/app/components/dtwo-error-boundary.d.ts +8 -0
- package/dist/app/components/dtwo-error-boundary.mjs +21 -0
- package/dist/app/components/dtwo-error-page.kdu +46 -0
- package/dist/app/components/dtwo-link.d.ts +33 -0
- package/dist/app/components/dtwo-link.mjs +248 -0
- package/dist/app/components/dtwo-loading-indicator.d.ts +43 -0
- package/dist/app/components/dtwo-loading-indicator.mjs +107 -0
- package/dist/app/components/dtwo-root.kdu +35 -0
- package/dist/app/components/index.d.ts +2 -0
- package/dist/app/components/index.mjs +1 -0
- package/dist/app/components/layout.d.ts +17 -0
- package/dist/app/components/layout.mjs +66 -0
- package/dist/app/components/server-placeholder.d.ts +2 -0
- package/dist/app/components/server-placeholder.mjs +7 -0
- package/dist/app/components/utils.d.ts +11 -0
- package/dist/app/components/utils.mjs +9 -0
- package/dist/app/composables/asyncData.d.ts +40 -0
- package/dist/app/composables/asyncData.mjs +169 -0
- package/dist/app/composables/component.d.ts +3 -0
- package/dist/app/composables/component.mjs +46 -0
- package/dist/app/composables/cookie.d.ts +12 -0
- package/dist/app/composables/cookie.mjs +58 -0
- package/dist/app/composables/error.d.ts +17 -0
- package/dist/app/composables/error.mjs +31 -0
- package/dist/app/composables/fetch.d.ts +15 -0
- package/dist/app/composables/fetch.mjs +66 -0
- package/dist/app/composables/hydrate.d.ts +8 -0
- package/dist/app/composables/hydrate.mjs +14 -0
- package/dist/app/composables/index.d.ts +16 -0
- package/dist/app/composables/index.mjs +11 -0
- package/dist/app/composables/payload.d.ts +8 -0
- package/dist/app/composables/payload.mjs +58 -0
- package/dist/app/composables/preload.d.ts +17 -0
- package/dist/app/composables/preload.mjs +41 -0
- package/dist/app/composables/router.d.ts +28 -0
- package/dist/app/composables/router.mjs +111 -0
- package/dist/app/composables/ssr.d.ts +6 -0
- package/dist/app/composables/ssr.mjs +23 -0
- package/dist/app/composables/state.d.ts +9 -0
- package/dist/app/composables/state.mjs +27 -0
- package/dist/app/config.d.ts +13 -0
- package/dist/app/config.mjs +56 -0
- package/dist/app/dtwo.d.ts +122 -0
- package/dist/app/dtwo.mjs +168 -0
- package/dist/app/entry.async.d.ts +2 -0
- package/dist/app/entry.async.mjs +5 -0
- package/dist/app/entry.d.ts +3 -0
- package/dist/app/entry.mjs +59 -0
- package/dist/app/index.d.ts +10 -0
- package/dist/app/index.mjs +7 -0
- package/dist/app/plugins/cross-origin-prefetch.client.d.ts +2 -0
- package/dist/app/plugins/cross-origin-prefetch.client.mjs +28 -0
- package/dist/app/plugins/debug.d.ts +2 -0
- package/dist/app/plugins/debug.mjs +5 -0
- package/dist/app/plugins/payload.client.d.ts +2 -0
- package/dist/app/plugins/payload.client.mjs +22 -0
- package/dist/app/plugins/preload.server.d.ts +2 -0
- package/dist/app/plugins/preload.server.mjs +10 -0
- package/dist/app/plugins/router.d.ts +53 -0
- package/dist/app/plugins/router.mjs +176 -0
- package/dist/app/types/augments.d.ts +28 -0
- package/dist/core/runtime/hole/error.d.ts +3 -0
- package/dist/core/runtime/hole/error.mjs +58 -0
- package/dist/core/runtime/hole/paths.d.ts +4 -0
- package/dist/core/runtime/hole/paths.mjs +15 -0
- package/dist/core/runtime/hole/renderer.d.ts +16 -0
- package/dist/core/runtime/hole/renderer.mjs +219 -0
- package/dist/head/runtime/components.d.ts +796 -0
- package/dist/head/runtime/components.mjs +209 -0
- package/dist/head/runtime/composables.d.ts +10 -0
- package/dist/head/runtime/composables.mjs +4 -0
- package/dist/head/runtime/index.d.ts +4 -0
- package/dist/head/runtime/index.mjs +1 -0
- package/dist/head/runtime/lib/kdu-meta.plugin.d.ts +2 -0
- package/dist/head/runtime/lib/kdu-meta.plugin.mjs +31 -0
- package/dist/head/runtime/lib/kduse-head.plugin.d.ts +2 -0
- package/dist/head/runtime/lib/kduse-head.plugin.mjs +35 -0
- package/dist/head/runtime/types.d.ts +7 -0
- package/dist/head/runtime/types.mjs +0 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.mjs +2274 -0
- package/dist/pages/runtime/app.kdu +5 -0
- package/dist/pages/runtime/composables.d.ts +42 -0
- package/dist/pages/runtime/composables.mjs +8 -0
- package/dist/pages/runtime/index.d.ts +1 -0
- package/dist/pages/runtime/index.mjs +1 -0
- package/dist/pages/runtime/page-placeholder.d.ts +4 -0
- package/dist/pages/runtime/page-placeholder.mjs +10 -0
- package/dist/pages/runtime/page.d.ts +15 -0
- package/dist/pages/runtime/page.mjs +107 -0
- package/dist/pages/runtime/router.d.ts +8 -0
- package/dist/pages/runtime/router.mjs +152 -0
- package/dist/pages/runtime/router.options.d.ts +2 -0
- package/dist/pages/runtime/router.options.mjs +51 -0
- package/dist/pages/runtime/utils.d.ts +9 -0
- package/dist/pages/runtime/utils.mjs +12 -0
- package/dist/pages/runtime/validate.d.ts +2 -0
- package/dist/pages/runtime/validate.mjs +11 -0
- package/package.json +79 -42
- package/types.d.ts +15 -0
- package/bin/dtwo.js +0 -10
- package/dist/dtwo.js +0 -37
- package/webpack.config.js +0 -4
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { joinURL } from "ufo";
|
|
2
|
+
import { useRuntimeConfig } from "#internal/hole";
|
|
3
|
+
export function baseURL() {
|
|
4
|
+
return useRuntimeConfig().app.baseURL;
|
|
5
|
+
}
|
|
6
|
+
export function buildAssetsDir() {
|
|
7
|
+
return useRuntimeConfig().app.buildAssetsDir;
|
|
8
|
+
}
|
|
9
|
+
export function buildAssetsURL(...path) {
|
|
10
|
+
return joinURL(publicAssetsURL(), useRuntimeConfig().app.buildAssetsDir, ...path);
|
|
11
|
+
}
|
|
12
|
+
export function publicAssetsURL(...path) {
|
|
13
|
+
const publicBase = useRuntimeConfig().app.cdnURL || useRuntimeConfig().app.baseURL;
|
|
14
|
+
return path.length ? joinURL(publicBase, ...path) : publicBase;
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface DtwoRenderHTMLContext {
|
|
2
|
+
htmlAttrs: string[];
|
|
3
|
+
head: string[];
|
|
4
|
+
bodyAttrs: string[];
|
|
5
|
+
bodyPrepend: string[];
|
|
6
|
+
body: string[];
|
|
7
|
+
bodyAppend: string[];
|
|
8
|
+
}
|
|
9
|
+
export interface DtwoRenderResponse {
|
|
10
|
+
body: string;
|
|
11
|
+
statusCode: number;
|
|
12
|
+
statusMessage?: string;
|
|
13
|
+
headers: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
declare const _default: any;
|
|
16
|
+
export default _default;
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { createRenderer, renderResourceHeaders } from "kdu-bundle-renderer/runtime";
|
|
2
|
+
import { appendHeader, createError, getQuery, writeEarlyHints } from "h3";
|
|
3
|
+
import devalue from "@d2js/devalue";
|
|
4
|
+
import { joinURL } from "ufo";
|
|
5
|
+
import { renderToString as _renderToString } from "kdu/server-renderer";
|
|
6
|
+
import { useRuntimeConfig, useHoleApp, defineRenderHandler, getRouteRules } from "#internal/hole";
|
|
7
|
+
import { appRootId, appRootTag } from "#internal/dtwo.config.mjs";
|
|
8
|
+
import { buildAssetsURL, publicAssetsURL } from "#paths";
|
|
9
|
+
globalThis.__buildAssetsURL = buildAssetsURL;
|
|
10
|
+
globalThis.__publicAssetsURL = publicAssetsURL;
|
|
11
|
+
const getClientManifest = () => import("#build/dist/server/client.manifest.mjs").then((r) => r.default || r).then((r) => typeof r === "function" ? r() : r);
|
|
12
|
+
const getStaticRenderedHead = () => import("#head-static").then((r) => r.default || r);
|
|
13
|
+
const getServerEntry = () => import("#build/dist/server/server.mjs").then((r) => r.default || r);
|
|
14
|
+
const getSSRStyles = () => import("#build/dist/server/styles.mjs").then((r) => r.default || r);
|
|
15
|
+
const getSSRRenderer = lazyCachedFunction(async () => {
|
|
16
|
+
const manifest = await getClientManifest();
|
|
17
|
+
if (!manifest) {
|
|
18
|
+
throw new Error("client.manifest is not available");
|
|
19
|
+
}
|
|
20
|
+
const createSSRApp = await getServerEntry();
|
|
21
|
+
if (!createSSRApp) {
|
|
22
|
+
throw new Error("Server bundle is not available");
|
|
23
|
+
}
|
|
24
|
+
const options = {
|
|
25
|
+
manifest,
|
|
26
|
+
renderToString,
|
|
27
|
+
buildAssetsURL
|
|
28
|
+
};
|
|
29
|
+
const renderer = createRenderer(createSSRApp, options);
|
|
30
|
+
async function renderToString(input, context) {
|
|
31
|
+
const html = await _renderToString(input, context);
|
|
32
|
+
if (process.dev && process.env.DTWO_WITE_NODE_OPTIONS) {
|
|
33
|
+
renderer.rendererContext.updateManifest(await getClientManifest());
|
|
34
|
+
}
|
|
35
|
+
return `<${appRootTag} id="${appRootId}">${html}</${appRootTag}>`;
|
|
36
|
+
}
|
|
37
|
+
return renderer;
|
|
38
|
+
});
|
|
39
|
+
const getSPARenderer = lazyCachedFunction(async () => {
|
|
40
|
+
const manifest = await getClientManifest();
|
|
41
|
+
const options = {
|
|
42
|
+
manifest,
|
|
43
|
+
renderToString: () => `<${appRootTag} id="${appRootId}"></${appRootTag}>`,
|
|
44
|
+
buildAssetsURL
|
|
45
|
+
};
|
|
46
|
+
const renderer = createRenderer(() => () => {
|
|
47
|
+
}, options);
|
|
48
|
+
const result = await renderer.renderToString({});
|
|
49
|
+
const renderToString = (ssrContext) => {
|
|
50
|
+
const config = useRuntimeConfig();
|
|
51
|
+
ssrContext.payload = {
|
|
52
|
+
serverRendered: false,
|
|
53
|
+
config: {
|
|
54
|
+
public: config.public,
|
|
55
|
+
app: config.app
|
|
56
|
+
},
|
|
57
|
+
data: {},
|
|
58
|
+
state: {}
|
|
59
|
+
};
|
|
60
|
+
ssrContext.renderMeta = ssrContext.renderMeta ?? getStaticRenderedHead;
|
|
61
|
+
return Promise.resolve(result);
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
rendererContext: renderer.rendererContext,
|
|
65
|
+
renderToString
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
const PAYLOAD_CACHE = process.env.DTWO_PAYLOAD_EXTRACTION && process.env.prerender ? /* @__PURE__ */ new Map() : null;
|
|
69
|
+
const PAYLOAD_URL_RE = /\/_payload(\.[a-zA-Z0-9]+)?.js(\?.*)?$/;
|
|
70
|
+
const PRERENDER_NO_SSR_ROUTES = /* @__PURE__ */ new Set(["/index.html", "/200.html", "/404.html"]);
|
|
71
|
+
export default defineRenderHandler(async (event) => {
|
|
72
|
+
const ssrError = event.node.req.url?.startsWith("/__dtwo_error") ? getQuery(event) : null;
|
|
73
|
+
if (ssrError && event.node.req.socket.readyState !== "readOnly") {
|
|
74
|
+
throw createError("Cannot directly render error page!");
|
|
75
|
+
}
|
|
76
|
+
let url = ssrError?.url || event.node.req.url;
|
|
77
|
+
const isRenderingPayload = PAYLOAD_URL_RE.test(url);
|
|
78
|
+
if (isRenderingPayload) {
|
|
79
|
+
url = url.substring(0, url.lastIndexOf("/")) || "/";
|
|
80
|
+
event.node.req.url = url;
|
|
81
|
+
if (process.env.prerender && PAYLOAD_CACHE.has(url)) {
|
|
82
|
+
return PAYLOAD_CACHE.get(url);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const routeOptions = getRouteRules(event);
|
|
86
|
+
const ssrContext = {
|
|
87
|
+
url,
|
|
88
|
+
event,
|
|
89
|
+
runtimeConfig: useRuntimeConfig(),
|
|
90
|
+
noSSR: !!process.env.DTWO_NO_SSR || !!event.node.req.headers["x-dtwo-no-ssr"] || routeOptions.ssr === false || (process.env.prerender ? PRERENDER_NO_SSR_ROUTES.has(url) : false),
|
|
91
|
+
error: !!ssrError,
|
|
92
|
+
dtwo: void 0,
|
|
93
|
+
payload: ssrError ? { error: ssrError } : {}
|
|
94
|
+
};
|
|
95
|
+
const _PAYLOAD_EXTRACTION = process.env.prerender && process.env.DTWO_PAYLOAD_EXTRACTION && !ssrContext.noSSR;
|
|
96
|
+
const payloadURL = _PAYLOAD_EXTRACTION ? joinURL(useRuntimeConfig().app.baseURL, url, "_payload.js") : void 0;
|
|
97
|
+
if (process.env.prerender) {
|
|
98
|
+
ssrContext.payload.prerenderedAt = Date.now();
|
|
99
|
+
}
|
|
100
|
+
const renderer = process.env.DTWO_NO_SSR || ssrContext.noSSR ? await getSPARenderer() : await getSSRRenderer();
|
|
101
|
+
if (process.env.DTWO_EARLY_HINTS && !isRenderingPayload && !process.env.prerender) {
|
|
102
|
+
const { link } = renderResourceHeaders({}, renderer.rendererContext);
|
|
103
|
+
writeEarlyHints(event, link);
|
|
104
|
+
}
|
|
105
|
+
const _rendered = await renderer.renderToString(ssrContext).catch((error) => {
|
|
106
|
+
throw !ssrError && ssrContext.payload?.error || error;
|
|
107
|
+
});
|
|
108
|
+
await ssrContext.dtwo?.hooks.callHook("app:rendered", { ssrContext });
|
|
109
|
+
if (ssrContext.payload?.error && !ssrError) {
|
|
110
|
+
throw ssrContext.payload.error;
|
|
111
|
+
}
|
|
112
|
+
if (isRenderingPayload) {
|
|
113
|
+
const response2 = renderPayloadResponse(ssrContext);
|
|
114
|
+
if (process.env.prerender) {
|
|
115
|
+
PAYLOAD_CACHE.set(url, response2);
|
|
116
|
+
}
|
|
117
|
+
return response2;
|
|
118
|
+
}
|
|
119
|
+
if (_PAYLOAD_EXTRACTION) {
|
|
120
|
+
appendHeader(event, "x-hole-prerender", joinURL(url, "_payload.js"));
|
|
121
|
+
PAYLOAD_CACHE.set(url, renderPayloadResponse(ssrContext));
|
|
122
|
+
}
|
|
123
|
+
const renderedMeta = await ssrContext.renderMeta?.() ?? {};
|
|
124
|
+
const inlinedStyles = process.env.DTWO_INLINE_STYLES ? await renderInlineStyles(ssrContext.modules ?? ssrContext._registeredComponents ?? []) : "";
|
|
125
|
+
const htmlContext = {
|
|
126
|
+
htmlAttrs: normalizeChunks([renderedMeta.htmlAttrs]),
|
|
127
|
+
head: normalizeChunks([
|
|
128
|
+
renderedMeta.headTags,
|
|
129
|
+
_PAYLOAD_EXTRACTION ? `<link rel="modulepreload" href="${payloadURL}">` : null,
|
|
130
|
+
_rendered.renderResourceHints(),
|
|
131
|
+
_rendered.renderStyles(),
|
|
132
|
+
inlinedStyles,
|
|
133
|
+
ssrContext.styles
|
|
134
|
+
]),
|
|
135
|
+
bodyAttrs: normalizeChunks([renderedMeta.bodyAttrs]),
|
|
136
|
+
bodyPrepend: normalizeChunks([
|
|
137
|
+
renderedMeta.bodyScriptsPrepend,
|
|
138
|
+
ssrContext.teleports?.body
|
|
139
|
+
]),
|
|
140
|
+
body: [
|
|
141
|
+
_rendered.html
|
|
142
|
+
],
|
|
143
|
+
bodyAppend: normalizeChunks([
|
|
144
|
+
process.env.DTWO_NO_SCRIPTS ? void 0 : _PAYLOAD_EXTRACTION ? `<script type="module">import p from "${payloadURL}";window.__DTWO__={...p,...(${devalue(splitPayload(ssrContext).initial)})}<\/script>` : `<script>window.__DTWO__=${devalue(ssrContext.payload)}<\/script>`,
|
|
145
|
+
_rendered.renderScripts(),
|
|
146
|
+
renderedMeta.bodyScripts
|
|
147
|
+
])
|
|
148
|
+
};
|
|
149
|
+
const holeApp = useHoleApp();
|
|
150
|
+
await holeApp.hooks.callHook("render:html", htmlContext, { event });
|
|
151
|
+
const response = {
|
|
152
|
+
body: renderHTMLDocument(htmlContext),
|
|
153
|
+
statusCode: event.node.res.statusCode,
|
|
154
|
+
statusMessage: event.node.res.statusMessage,
|
|
155
|
+
headers: {
|
|
156
|
+
"Content-Type": "text/html;charset=UTF-8",
|
|
157
|
+
"X-Powered-By": "Dtwo"
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
return response;
|
|
161
|
+
});
|
|
162
|
+
function lazyCachedFunction(fn) {
|
|
163
|
+
let res = null;
|
|
164
|
+
return () => {
|
|
165
|
+
if (res === null) {
|
|
166
|
+
res = fn().catch((err) => {
|
|
167
|
+
res = null;
|
|
168
|
+
throw err;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
return res;
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function normalizeChunks(chunks) {
|
|
175
|
+
return chunks.filter(Boolean).map((i) => i.trim());
|
|
176
|
+
}
|
|
177
|
+
function joinTags(tags) {
|
|
178
|
+
return tags.join("");
|
|
179
|
+
}
|
|
180
|
+
function joinAttrs(chunks) {
|
|
181
|
+
return chunks.join(" ");
|
|
182
|
+
}
|
|
183
|
+
function renderHTMLDocument(html) {
|
|
184
|
+
return `<!DOCTYPE html>
|
|
185
|
+
<html ${joinAttrs(html.htmlAttrs)}>
|
|
186
|
+
<head>${joinTags(html.head)}</head>
|
|
187
|
+
<body ${joinAttrs(html.bodyAttrs)}>${joinTags(html.bodyPrepend)}${joinTags(html.body)}${joinTags(html.bodyAppend)}</body>
|
|
188
|
+
</html>`;
|
|
189
|
+
}
|
|
190
|
+
async function renderInlineStyles(usedModules) {
|
|
191
|
+
const styleMap = await getSSRStyles();
|
|
192
|
+
const inlinedStyles = /* @__PURE__ */ new Set();
|
|
193
|
+
for (const mod of usedModules) {
|
|
194
|
+
if (mod in styleMap) {
|
|
195
|
+
for (const style of await styleMap[mod]()) {
|
|
196
|
+
inlinedStyles.add(`<style>${style}</style>`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return Array.from(inlinedStyles).join("");
|
|
201
|
+
}
|
|
202
|
+
function renderPayloadResponse(ssrContext) {
|
|
203
|
+
return {
|
|
204
|
+
body: `export default ${devalue(splitPayload(ssrContext).payload)}`,
|
|
205
|
+
statusCode: ssrContext.event.node.res.statusCode,
|
|
206
|
+
statusMessage: ssrContext.event.node.res.statusMessage,
|
|
207
|
+
headers: {
|
|
208
|
+
"content-type": "text/javascript;charset=UTF-8",
|
|
209
|
+
"x-powered-by": "Dtwo"
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function splitPayload(ssrContext) {
|
|
214
|
+
const { data, prerenderedAt, ...initial } = ssrContext.payload;
|
|
215
|
+
return {
|
|
216
|
+
initial: { ...initial, prerenderedAt },
|
|
217
|
+
payload: { data, prerenderedAt }
|
|
218
|
+
};
|
|
219
|
+
}
|