renderer.mjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import process from 'node:process';globalThis._importMeta_=globalThis._importMeta_||{url:"file:///_entry.js",env:process.env};import { getRequestDependencies, getPreloadLinks, getPrefetchLinks, createRenderer } from 'file://D:/MyProject/106/sannongzixunwang_web/node_modules/vue-bundle-renderer/dist/runtime.mjs';
  2. import { getQuery, createError, appendResponseHeader, getResponseStatus, getResponseStatusText } from 'file://D:/MyProject/106/sannongzixunwang_web/node_modules/h3/dist/index.mjs';
  3. import { stringify, uneval } from 'file://D:/MyProject/106/sannongzixunwang_web/node_modules/devalue/index.js';
  4. import { joinRelativeURL, joinURL, withoutTrailingSlash } from 'file://D:/MyProject/106/sannongzixunwang_web/node_modules/ufo/dist/index.mjs';
  5. import { renderToString } from 'file://D:/MyProject/106/sannongzixunwang_web/node_modules/vue/server-renderer/index.mjs';
  6. import { propsToString, renderSSRHead } from 'file://D:/MyProject/106/sannongzixunwang_web/node_modules/@unhead/ssr/dist/index.mjs';
  7. import { createServerHead as createServerHead$1, CapoPlugin } from 'file://D:/MyProject/106/sannongzixunwang_web/node_modules/unhead/dist/index.mjs';
  8. import { u as useRuntimeConfig, d as defineRenderHandler, a as useStorage, g as getRouteRules, b as useNitroApp } from './nitro.mjs';
  9. import { version, unref } from 'file://D:/MyProject/106/sannongzixunwang_web/node_modules/vue/index.mjs';
  10. import { defineHeadPlugin } from 'file://D:/MyProject/106/sannongzixunwang_web/node_modules/@unhead/shared/dist/index.mjs';
  11. const Vue3 = version[0] === "3";
  12. function resolveUnref(r) {
  13. return typeof r === "function" ? r() : unref(r);
  14. }
  15. function resolveUnrefHeadInput(ref) {
  16. if (ref instanceof Promise || ref instanceof Date || ref instanceof RegExp)
  17. return ref;
  18. const root = resolveUnref(ref);
  19. if (!ref || !root)
  20. return root;
  21. if (Array.isArray(root))
  22. return root.map((r) => resolveUnrefHeadInput(r));
  23. if (typeof root === "object") {
  24. const resolved = {};
  25. for (const k in root) {
  26. if (!Object.prototype.hasOwnProperty.call(root, k)) {
  27. continue;
  28. }
  29. if (k === "titleTemplate" || k[0] === "o" && k[1] === "n") {
  30. resolved[k] = unref(root[k]);
  31. continue;
  32. }
  33. resolved[k] = resolveUnrefHeadInput(root[k]);
  34. }
  35. return resolved;
  36. }
  37. return root;
  38. }
  39. const VueReactivityPlugin = defineHeadPlugin({
  40. hooks: {
  41. "entries:resolve": (ctx) => {
  42. for (const entry of ctx.entries)
  43. entry.resolvedInput = resolveUnrefHeadInput(entry.input);
  44. }
  45. }
  46. });
  47. const headSymbol = "usehead";
  48. function vueInstall(head) {
  49. const plugin = {
  50. install(app) {
  51. if (Vue3) {
  52. app.config.globalProperties.$unhead = head;
  53. app.config.globalProperties.$head = head;
  54. app.provide(headSymbol, head);
  55. }
  56. }
  57. };
  58. return plugin.install;
  59. }
  60. function createServerHead(options = {}) {
  61. const head = createServerHead$1(options);
  62. head.use(VueReactivityPlugin);
  63. head.install = vueInstall(head);
  64. return head;
  65. }
  66. const unheadPlugins = true ? [CapoPlugin({ track: true })] : [];
  67. const renderSSRHeadOptions = {"omitLineBreaks":false};
  68. const appHead = {"meta":[{"name":"viewport","content":"width=device-width, initial-scale=1"},{"charset":"utf-8"}],"link":[],"style":[],"script":[],"noscript":[]};
  69. const appRootTag = "div";
  70. const appRootAttrs = {"id":"__nuxt"};
  71. const appTeleportTag = "div";
  72. const appTeleportAttrs = {"id":"teleports"};
  73. const componentIslands = false;
  74. const appId = "nuxt-app";
  75. function baseURL() {
  76. return useRuntimeConfig().app.baseURL;
  77. }
  78. function buildAssetsDir() {
  79. return useRuntimeConfig().app.buildAssetsDir;
  80. }
  81. function buildAssetsURL(...path) {
  82. return joinRelativeURL(publicAssetsURL(), buildAssetsDir(), ...path);
  83. }
  84. function publicAssetsURL(...path) {
  85. const app = useRuntimeConfig().app;
  86. const publicBase = app.cdnURL || app.baseURL;
  87. return path.length ? joinRelativeURL(publicBase, ...path) : publicBase;
  88. }
  89. globalThis.__buildAssetsURL = buildAssetsURL;
  90. globalThis.__publicAssetsURL = publicAssetsURL;
  91. const getClientManifest = () => import('../build/client.manifest.mjs').then((r) => r.default || r).then((r) => typeof r === "function" ? r() : r);
  92. const getEntryIds = () => getClientManifest().then((r) => Object.values(r).filter(
  93. (r2) => (
  94. // @ts-expect-error internal key set by CSS inlining configuration
  95. r2._globalCSS
  96. )
  97. ).map((r2) => r2.src));
  98. const getServerEntry = () => import('../build/server.mjs').then((r) => r.default || r);
  99. const getSSRStyles = lazyCachedFunction(() => import('../build/styles.mjs').then((r) => r.default || r));
  100. const getSSRRenderer = lazyCachedFunction(async () => {
  101. const manifest = await getClientManifest();
  102. if (!manifest) {
  103. throw new Error("client.manifest is not available");
  104. }
  105. const createSSRApp = await getServerEntry();
  106. if (!createSSRApp) {
  107. throw new Error("Server bundle is not available");
  108. }
  109. const options = {
  110. manifest,
  111. renderToString: renderToString$1,
  112. buildAssetsURL
  113. };
  114. const renderer = createRenderer(createSSRApp, options);
  115. async function renderToString$1(input, context) {
  116. const html = await renderToString(input, context);
  117. return APP_ROOT_OPEN_TAG + html + APP_ROOT_CLOSE_TAG;
  118. }
  119. return renderer;
  120. });
  121. const getSPARenderer = lazyCachedFunction(async () => {
  122. const manifest = await getClientManifest();
  123. const spaTemplate = await import('../virtual/_virtual_spa-template.mjs').then((r) => r.template).catch(() => "").then((r) => {
  124. {
  125. return APP_ROOT_OPEN_TAG + r + APP_ROOT_CLOSE_TAG;
  126. }
  127. });
  128. const options = {
  129. manifest,
  130. renderToString: () => spaTemplate,
  131. buildAssetsURL
  132. };
  133. const renderer = createRenderer(() => () => {
  134. }, options);
  135. const result = await renderer.renderToString({});
  136. const renderToString = (ssrContext) => {
  137. const config = useRuntimeConfig(ssrContext.event);
  138. ssrContext.modules = ssrContext.modules || /* @__PURE__ */ new Set();
  139. ssrContext.payload.serverRendered = false;
  140. ssrContext.config = {
  141. public: config.public,
  142. app: config.app
  143. };
  144. return Promise.resolve(result);
  145. };
  146. return {
  147. rendererContext: renderer.rendererContext,
  148. renderToString
  149. };
  150. });
  151. const payloadCache = useStorage("internal:nuxt:prerender:payload") ;
  152. useStorage("internal:nuxt:prerender:island") ;
  153. useStorage("internal:nuxt:prerender:island-props") ;
  154. const HAS_APP_TELEPORTS = !!(appTeleportAttrs.id);
  155. const APP_TELEPORT_OPEN_TAG = HAS_APP_TELEPORTS ? `<${appTeleportTag}${propsToString(appTeleportAttrs)}>` : "";
  156. const APP_TELEPORT_CLOSE_TAG = HAS_APP_TELEPORTS ? `</${appTeleportTag}>` : "";
  157. const APP_ROOT_OPEN_TAG = `<${appRootTag}${propsToString(appRootAttrs)}>`;
  158. const APP_ROOT_CLOSE_TAG = `</${appRootTag}>`;
  159. const PAYLOAD_URL_RE = /\/_payload.json(\?.*)?$/ ;
  160. const PRERENDER_NO_SSR_ROUTES = /* @__PURE__ */ new Set(["/index.html", "/200.html", "/404.html"]);
  161. const renderer = defineRenderHandler(async (event) => {
  162. const nitroApp = useNitroApp();
  163. const ssrError = event.path.startsWith("/__nuxt_error") ? getQuery(event) : null;
  164. if (ssrError && ssrError.statusCode) {
  165. ssrError.statusCode = Number.parseInt(ssrError.statusCode);
  166. }
  167. if (ssrError && !("__unenv__" in event.node.req)) {
  168. throw createError({
  169. statusCode: 404,
  170. statusMessage: "Page Not Found: /__nuxt_error"
  171. });
  172. }
  173. const isRenderingIsland = componentIslands;
  174. const islandContext = undefined;
  175. let url = ssrError?.url || islandContext?.url || event.path;
  176. const isRenderingPayload = PAYLOAD_URL_RE.test(url) && true;
  177. if (isRenderingPayload) {
  178. url = url.substring(0, url.lastIndexOf("/")) || "/";
  179. event._path = url;
  180. event.node.req.url = url;
  181. if (await payloadCache.hasItem(url)) {
  182. return payloadCache.getItem(url);
  183. }
  184. }
  185. const routeOptions = getRouteRules(event);
  186. const head = createServerHead({
  187. plugins: unheadPlugins
  188. });
  189. const headEntryOptions = { mode: "server" };
  190. {
  191. head.push(appHead, headEntryOptions);
  192. }
  193. const ssrContext = {
  194. url,
  195. event,
  196. runtimeConfig: useRuntimeConfig(event),
  197. noSSR: event.context.nuxt?.noSSR || routeOptions.ssr === false && true || (PRERENDER_NO_SSR_ROUTES.has(url) ),
  198. head,
  199. error: !!ssrError,
  200. nuxt: undefined,
  201. /* NuxtApp */
  202. payload: ssrError ? { error: ssrError } : {},
  203. _payloadReducers: /* @__PURE__ */ Object.create(null),
  204. modules: /* @__PURE__ */ new Set(),
  205. islandContext
  206. };
  207. const _PAYLOAD_EXTRACTION = !ssrContext.noSSR && true;
  208. const payloadURL = _PAYLOAD_EXTRACTION ? joinURL(ssrContext.runtimeConfig.app.cdnURL || ssrContext.runtimeConfig.app.baseURL, url, "_payload.json" ) + "?" + ssrContext.runtimeConfig.app.buildId : undefined;
  209. {
  210. ssrContext.payload.prerenderedAt = Date.now();
  211. }
  212. const renderer = ssrContext.noSSR ? await getSPARenderer() : await getSSRRenderer();
  213. {
  214. for (const id of await getEntryIds()) {
  215. ssrContext.modules.add(id);
  216. }
  217. }
  218. const _rendered = await renderer.renderToString(ssrContext).catch(async (error) => {
  219. if (ssrContext._renderResponse && error.message === "skipping render") {
  220. return {};
  221. }
  222. const _err = !ssrError && ssrContext.payload?.error || error;
  223. await ssrContext.nuxt?.hooks.callHook("app:error", _err);
  224. throw _err;
  225. });
  226. await ssrContext.nuxt?.hooks.callHook("app:rendered", { ssrContext, renderResult: _rendered });
  227. if (ssrContext._renderResponse) {
  228. return ssrContext._renderResponse;
  229. }
  230. if (ssrContext.payload?.error && !ssrError) {
  231. throw ssrContext.payload.error;
  232. }
  233. if (isRenderingPayload) {
  234. const response2 = renderPayloadResponse(ssrContext);
  235. {
  236. await payloadCache.setItem(url, response2);
  237. }
  238. return response2;
  239. }
  240. if (_PAYLOAD_EXTRACTION) {
  241. appendResponseHeader(event, "x-nitro-prerender", joinURL(url, "_payload.json" ));
  242. await payloadCache.setItem(withoutTrailingSlash(url), renderPayloadResponse(ssrContext));
  243. }
  244. const inlinedStyles = await renderInlineStyles(ssrContext.modules ?? []) ;
  245. const NO_SCRIPTS = routeOptions.experimentalNoScripts;
  246. const { styles, scripts } = getRequestDependencies(ssrContext, renderer.rendererContext);
  247. if (_PAYLOAD_EXTRACTION && !NO_SCRIPTS && true) {
  248. head.push({
  249. link: [
  250. { rel: "preload", as: "fetch", crossorigin: "anonymous", href: payloadURL }
  251. ]
  252. }, headEntryOptions);
  253. }
  254. if (ssrContext._preloadManifest) {
  255. head.push({
  256. link: [
  257. { rel: "preload", as: "fetch", fetchpriority: "low", crossorigin: "anonymous", href: buildAssetsURL(`builds/meta/${ssrContext.runtimeConfig.app.buildId}.json`) }
  258. ]
  259. }, { ...headEntryOptions, tagPriority: "low" });
  260. }
  261. if (inlinedStyles.length) {
  262. head.push({ style: inlinedStyles });
  263. }
  264. {
  265. const link = [];
  266. for (const resource of Object.values(styles)) {
  267. {
  268. link.push({ rel: "stylesheet", href: renderer.rendererContext.buildAssetsURL(resource.file), crossorigin: "" });
  269. }
  270. }
  271. if (link.length) {
  272. head.push({ link }, headEntryOptions);
  273. }
  274. }
  275. if (!NO_SCRIPTS && true) {
  276. head.push({
  277. link: getPreloadLinks(ssrContext, renderer.rendererContext)
  278. }, headEntryOptions);
  279. head.push({
  280. link: getPrefetchLinks(ssrContext, renderer.rendererContext)
  281. }, headEntryOptions);
  282. head.push({
  283. script: _PAYLOAD_EXTRACTION ? renderPayloadJsonScript({ ssrContext, data: splitPayload(ssrContext).initial, src: payloadURL }) : renderPayloadJsonScript({ ssrContext, data: ssrContext.payload })
  284. }, {
  285. ...headEntryOptions,
  286. // this should come before another end of body scripts
  287. tagPosition: "bodyClose",
  288. tagPriority: "high"
  289. });
  290. }
  291. if (!routeOptions.experimentalNoScripts && true) {
  292. head.push({
  293. script: Object.values(scripts).map((resource) => ({
  294. type: resource.module ? "module" : null,
  295. src: renderer.rendererContext.buildAssetsURL(resource.file),
  296. defer: resource.module ? null : true,
  297. // if we are rendering script tag payloads that import an async payload
  298. // we need to ensure this resolves before executing the Nuxt entry
  299. tagPosition: "head",
  300. crossorigin: ""
  301. }))
  302. }, headEntryOptions);
  303. }
  304. const { headTags, bodyTags, bodyTagsOpen, htmlAttrs, bodyAttrs } = await renderSSRHead(head, renderSSRHeadOptions);
  305. const htmlContext = {
  306. island: isRenderingIsland,
  307. htmlAttrs: htmlAttrs ? [htmlAttrs] : [],
  308. head: normalizeChunks([headTags]),
  309. bodyAttrs: bodyAttrs ? [bodyAttrs] : [],
  310. bodyPrepend: normalizeChunks([bodyTagsOpen, ssrContext.teleports?.body]),
  311. body: [
  312. _rendered.html,
  313. APP_TELEPORT_OPEN_TAG + (HAS_APP_TELEPORTS ? joinTags([ssrContext.teleports?.[`#${appTeleportAttrs.id}`]]) : "") + APP_TELEPORT_CLOSE_TAG
  314. ],
  315. bodyAppend: [bodyTags]
  316. };
  317. await nitroApp.hooks.callHook("render:html", htmlContext, { event });
  318. const response = {
  319. body: renderHTMLDocument(htmlContext),
  320. statusCode: getResponseStatus(event),
  321. statusMessage: getResponseStatusText(event),
  322. headers: {
  323. "content-type": "text/html;charset=utf-8",
  324. "x-powered-by": "Nuxt"
  325. }
  326. };
  327. return response;
  328. });
  329. function lazyCachedFunction(fn) {
  330. let res = null;
  331. return () => {
  332. if (res === null) {
  333. res = fn().catch((err) => {
  334. res = null;
  335. throw err;
  336. });
  337. }
  338. return res;
  339. };
  340. }
  341. function normalizeChunks(chunks) {
  342. return chunks.filter(Boolean).map((i) => i.trim());
  343. }
  344. function joinTags(tags) {
  345. return tags.join("");
  346. }
  347. function joinAttrs(chunks) {
  348. if (chunks.length === 0) {
  349. return "";
  350. }
  351. return " " + chunks.join(" ");
  352. }
  353. function renderHTMLDocument(html) {
  354. return `<!DOCTYPE html><html${joinAttrs(html.htmlAttrs)}><head>${joinTags(html.head)}</head><body${joinAttrs(html.bodyAttrs)}>${joinTags(html.bodyPrepend)}${joinTags(html.body)}${joinTags(html.bodyAppend)}</body></html>`;
  355. }
  356. async function renderInlineStyles(usedModules) {
  357. const styleMap = await getSSRStyles();
  358. const inlinedStyles = /* @__PURE__ */ new Set();
  359. for (const mod of usedModules) {
  360. if (mod in styleMap && styleMap[mod]) {
  361. for (const style of await styleMap[mod]()) {
  362. inlinedStyles.add(style);
  363. }
  364. }
  365. }
  366. return Array.from(inlinedStyles).map((style) => ({ innerHTML: style }));
  367. }
  368. function renderPayloadResponse(ssrContext) {
  369. return {
  370. body: stringify(splitPayload(ssrContext).payload, ssrContext._payloadReducers) ,
  371. statusCode: getResponseStatus(ssrContext.event),
  372. statusMessage: getResponseStatusText(ssrContext.event),
  373. headers: {
  374. "content-type": "application/json;charset=utf-8" ,
  375. "x-powered-by": "Nuxt"
  376. }
  377. };
  378. }
  379. function renderPayloadJsonScript(opts) {
  380. const contents = opts.data ? stringify(opts.data, opts.ssrContext._payloadReducers) : "";
  381. const payload = {
  382. "type": "application/json",
  383. "innerHTML": contents,
  384. "data-nuxt-data": appId,
  385. "data-ssr": !(opts.ssrContext.noSSR)
  386. };
  387. {
  388. payload.id = "__NUXT_DATA__";
  389. }
  390. if (opts.src) {
  391. payload["data-src"] = opts.src;
  392. }
  393. const config = uneval(opts.ssrContext.config);
  394. return [
  395. payload,
  396. {
  397. innerHTML: `window.__NUXT__={};window.__NUXT__.config=${config}`
  398. }
  399. ];
  400. }
  401. function splitPayload(ssrContext) {
  402. const { data, prerenderedAt, ...initial } = ssrContext.payload;
  403. return {
  404. initial: { ...initial, prerenderedAt },
  405. payload: { data, prerenderedAt }
  406. };
  407. }
  408. const renderer$1 = /*#__PURE__*/Object.freeze({
  409. __proto__: null,
  410. default: renderer
  411. });
  412. export { buildAssetsURL as a, baseURL as b, renderer$1 as r };
  413. //# sourceMappingURL=renderer.mjs.map