@monkeyplus/flow 5.0.0-rc.9 → 5.0.0-rc.91

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,6 +1,6 @@
1
- import { defineEventHandler } from "h3";
2
1
  import "node-fetch-native/polyfill";
3
- import { useRuntimeConfig } from "#internal/nitro";
2
+ import { defineEventHandler } from "h3";
3
+ import { defineCachedFunction, useRuntimeConfig } from "#internal/nitro";
4
4
  const getServerApp = cachedImport(() => import("#server"));
5
5
  const getFlowRenderer = cachedResult(async () => {
6
6
  const createFlowApp = await getServerApp();
@@ -12,6 +12,7 @@ export default defineEventHandler(async ({ context }) => {
12
12
  const flow = await getFlowRenderer();
13
13
  context.flow = flow;
14
14
  context.render = flow.render;
15
+ context.defineCachedFunction = defineCachedFunction;
15
16
  });
16
17
  function _interopDefault(e) {
17
18
  return e && typeof e === "object" && "default" in e ? e.default : e;
@@ -1,4 +1,10 @@
1
1
  import { eventHandler, useQuery } from "h3";
2
+ import { joinURL } from "ufo";
3
+ const normalizeCall = async (seo, ctx) => {
4
+ if (typeof seo === "function")
5
+ return await seo(ctx);
6
+ return seo || {};
7
+ };
2
8
  export default eventHandler(async (event) => {
3
9
  const url = event.req.url?.split("?")[0];
4
10
  const flow = event.context.flow;
@@ -9,38 +15,64 @@ export default eventHandler(async (event) => {
9
15
  const scripts = { head: [], bodyStart: [], bodyEnd: [] };
10
16
  const chunks = { head: [], body: [] };
11
17
  const generate = {};
12
- const { page, locale, params, view } = flow.router.byUrl.lookup(url) || {};
18
+ const { page, locale, params, view, name: pageName } = flow.router.byUrl.lookup(url) || {};
13
19
  if (!page)
14
20
  return;
15
- await flow.callHook("page:scripts", scripts);
16
21
  if (view.bundle)
17
22
  await flow.callHook("page:chunks", view.bundle, chunks);
18
23
  const templateContext = {};
19
- const dynamyc = params?._;
20
- const contextPage = {};
24
+ const dynamic = params?._;
25
+ const contextPage = {
26
+ chunks,
27
+ locale
28
+ };
21
29
  const contextInject = {};
22
- const utils = Object.assign({ getLocale: () => locale, injectContext: (key, value) => contextInject[key] = value }, flow.app.utils);
30
+ const utils = Object.assign({ getLocale: () => locale, injectContext: (key, value) => contextInject[key] = value, defineCachedFunction: event.context.defineCachedFunction }, flow.app.utils);
31
+ const contextHooks = {};
32
+ await flow.callHook("page:context", { localeCode: locale.code, page: pageName }, utils, contextHooks);
23
33
  templateContext.url = url;
34
+ templateContext.baseURL = flow.$config.app.baseURL;
35
+ const pageObject = {
36
+ name: pageName,
37
+ pathname: url,
38
+ url: joinURL(flow.site.origin, templateContext.baseURL, `${url}`),
39
+ origin: flow.site.origin,
40
+ joinOrigin: (_url, includeBase) => joinURL(flow.site.origin, includeBase ? templateContext.baseURL : "/", _url)
41
+ };
42
+ templateContext.page = pageObject;
43
+ contextPage.page = pageObject;
24
44
  templateContext.locale = locale;
25
45
  templateContext.view = view;
26
- if (dynamyc && page.dynamic) {
46
+ if (dynamic && page.dynamic) {
27
47
  const pages = await page.dynamic.method({ locale, utils });
28
- const dynamicPage = pages.find((_page) => _page.url === dynamyc);
48
+ const dynamicPage = pages.find((_page) => _page.url === dynamic);
29
49
  if (!dynamicPage)
30
50
  return;
31
51
  if (page.dynamic.assign)
32
52
  templateContext[page.dynamic.assign] = dynamicPage.context;
33
- contextPage.dynamyc = dynamicPage;
53
+ contextPage.dynamic = dynamicPage;
34
54
  }
35
55
  templateContext.seo = {};
36
- templateContext.context = {};
37
- templateContext.sharedContext = {};
56
+ const sharedContext = await utils.getSharedContext({
57
+ ...contextHooks,
58
+ ...contextPage,
59
+ utils
60
+ });
61
+ templateContext.sharedContext = sharedContext || {};
38
62
  templateContext.utils = utils;
39
- templateContext.seo = await page.seo?.({ ...contextPage, utils }) || {};
40
- templateContext.context = await page.context?.({ ...contextPage, utils }) || {};
63
+ templateContext.seo = await normalizeCall(page?.seo, { ...contextHooks, ...contextPage, utils, sharedContext });
64
+ const contexData = await page.context?.({ ...contextHooks, ...contextPage, utils, sharedContext }) || {};
65
+ templateContext.context = {
66
+ ...contextHooks,
67
+ ...contexData
68
+ };
69
+ await flow.callHook("page:scripts", scripts, templateContext);
41
70
  templateContext.getHeadScripts = () => {
42
71
  return scripts.head.join("\n");
43
72
  };
73
+ templateContext.getBodyEndScripts = () => {
74
+ return scripts.bodyEnd.join("\n");
75
+ };
44
76
  templateContext.getHeadChunks = () => {
45
77
  return chunks.head.join("\n");
46
78
  };
@@ -53,8 +85,14 @@ export default eventHandler(async (event) => {
53
85
  const query = useQuery(event);
54
86
  if (query?.context)
55
87
  return { ...templateContext, utils: Object.keys(utils) };
56
- const html = await flow.render(view, templateContext);
57
- event.res.setHeader("Content-Type", "text/html;charset=UTF-8");
88
+ let html;
89
+ if (view.render)
90
+ html = await view.render(templateContext, event);
91
+ else
92
+ html = await flow.render(view, templateContext);
93
+ event.res.setHeader("Content-Type", view.contentType || "text/html;charset=UTF-8");
94
+ if (view.postRender)
95
+ html = await view.postRender(html, event);
58
96
  if (flow.generate) {
59
97
  await flow.callHook("page:generate", generate);
60
98
  globalThis.generate = generate;