@ossy/app 1.4.1 → 1.5.0

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.
@@ -0,0 +1,26 @@
1
+ import React, { cloneElement } from 'react'
2
+ import { prerenderToNodeStream } from 'react-dom/static'
3
+
4
+ export const BuildPage = {
5
+ async handle ({ route, appConfig, isDevReloadEnabled }) {
6
+ const rootElement = cloneElement(route.element, appConfig)
7
+ const devReloadScript = isDevReloadEnabled
8
+ ? `(function(){try{var es=new EventSource('/__ossy_reload');es.addEventListener('reload',function(){location.reload();});}catch(e){}})();`
9
+ : ``
10
+
11
+ const hydrateUrl = `/static/hydrate-${route.id}.js`
12
+ const { prelude } = await prerenderToNodeStream(rootElement, {
13
+ bootstrapScriptContent: `window.__INITIAL_APP_CONFIG__ = ${JSON.stringify(appConfig)};${devReloadScript}`,
14
+ bootstrapModules: [hydrateUrl],
15
+ })
16
+
17
+ return new Promise((resolve, reject) => {
18
+ let data = ''
19
+ prelude.on('data', (chunk) => {
20
+ data += chunk
21
+ })
22
+ prelude.on('end', () => resolve(data))
23
+ prelude.on('error', reject)
24
+ })
25
+ },
26
+ }
package/cli/server.js CHANGED
@@ -1,12 +1,11 @@
1
1
  import path from 'path';
2
2
  import url from 'url'
3
- import React, { cloneElement } from 'react';
4
3
  import express from 'express'
5
4
  import morgan from 'morgan'
6
5
  import { Router as OssyRouter } from '@ossy/router'
7
- import { prerenderToNodeStream } from 'react-dom/static'
8
6
  import { ProxyInternal } from './proxy-internal.js'
9
7
  import cookieParser from 'cookie-parser'
8
+ import { BuildPage } from './render-page.task.js'
10
9
 
11
10
  import pages from './.ossy/pages.bundle.js'
12
11
  import ApiRoutes from './.ossy/api.bundle.js'
@@ -112,6 +111,25 @@ const Router = OssyRouter.of({
112
111
  pages: [...apiRouteList, ...pageList]
113
112
  })
114
113
 
114
+ function resolveAppConfig ({ req, buildTimeConfig }) {
115
+ const userAppSettings = req.userAppSettings || {}
116
+ const pages = pageList.map((page) => ({
117
+ id: page?.id,
118
+ path: page?.path,
119
+ }))
120
+ return {
121
+ ...buildTimeConfig,
122
+ url: req.originalUrl,
123
+ theme: userAppSettings.theme || buildTimeConfig.theme || 'light',
124
+ isAuthenticated: req.isAuthenticated || false,
125
+ workspaceId: userAppSettings.workspaceId || buildTimeConfig.workspaceId,
126
+ apiUrl: buildTimeConfig.apiUrl,
127
+ pages,
128
+ /** Primary app shell sidebar: icon rail when true (persisted in `x-ossy-user-settings`). */
129
+ sidebarPrimaryCollapsed: userAppSettings.sidebarPrimaryCollapsed === true,
130
+ }
131
+ }
132
+
115
133
  app.all('*all', (req, res) => {
116
134
  const pathname = req.originalUrl
117
135
 
@@ -123,25 +141,14 @@ app.all('*all', (req, res) => {
123
141
  return
124
142
  }
125
143
 
126
- const userAppSettings = req.userAppSettings || {}
127
-
128
- const appConfig = {
129
- ...buildTimeConfig,
130
- url: req.originalUrl,
131
- theme: userAppSettings.theme || buildTimeConfig.theme || 'light',
132
- isAuthenticated: req.isAuthenticated || false,
133
- workspaceId: userAppSettings.workspaceId || buildTimeConfig.workspaceId,
134
- apiUrl: buildTimeConfig.apiUrl,
135
- /** Primary app shell sidebar: icon rail when true (persisted in `x-ossy-user-settings`). */
136
- sidebarPrimaryCollapsed: userAppSettings.sidebarPrimaryCollapsed === true,
137
- }
144
+ const appConfig = resolveAppConfig({ req, buildTimeConfig })
138
145
 
139
146
  if (!route?.element) {
140
147
  res.status(404).send('Not found')
141
148
  return
142
149
  }
143
150
 
144
- prerenderHtmlDocument(cloneElement(route.element, appConfig), appConfig, route.id)
151
+ BuildPage.handle({ route, appConfig, isDevReloadEnabled })
145
152
  .then(html => { res.send(html) })
146
153
  .catch(err => { res.send(err) })
147
154
 
@@ -149,28 +156,4 @@ app.all('*all', (req, res) => {
149
156
 
150
157
  app.listen(port, () => {
151
158
  console.log(`[@ossy/app][server] Running on http://localhost:${port}`);
152
- });
153
-
154
- async function prerenderHtmlDocument (rootElement, config, pageId) {
155
-
156
- const devReloadScript = isDevReloadEnabled
157
- ? `(function(){try{var es=new EventSource('/__ossy_reload');es.addEventListener('reload',function(){location.reload();});}catch(e){}})();`
158
- : ``
159
-
160
- const hydrateUrl = `/static/hydrate-${pageId}.js`
161
-
162
- const { prelude } = await prerenderToNodeStream(rootElement, {
163
- bootstrapScriptContent: `window.__INITIAL_APP_CONFIG__ = ${JSON.stringify(config)};${devReloadScript}`,
164
- bootstrapModules: [hydrateUrl]
165
- });
166
-
167
- return new Promise((resolve, reject) => {
168
- let data = '';
169
- prelude.on('data', chunk => {
170
- data += chunk;
171
- });
172
- prelude.on('end', () => resolve(data));
173
- prelude.on('error', reject);
174
- });
175
-
176
- }
159
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ossy/app",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "",
5
5
  "source": "./src/index.js",
6
6
  "main": "./src/index.js",
@@ -27,14 +27,14 @@
27
27
  "@babel/eslint-parser": "^7.15.8",
28
28
  "@babel/preset-react": "^7.26.3",
29
29
  "@babel/register": "^7.25.9",
30
- "@ossy/connected-components": "^1.4.1",
31
- "@ossy/design-system": "^1.4.1",
32
- "@ossy/pages": "^1.4.1",
33
- "@ossy/router": "^1.4.1",
34
- "@ossy/router-react": "^1.4.1",
35
- "@ossy/sdk": "^1.4.1",
36
- "@ossy/sdk-react": "^1.4.1",
37
- "@ossy/themes": "^1.4.1",
30
+ "@ossy/connected-components": "^1.5.0",
31
+ "@ossy/design-system": "^1.5.0",
32
+ "@ossy/pages": "^1.5.0",
33
+ "@ossy/router": "^1.5.0",
34
+ "@ossy/router-react": "^1.5.0",
35
+ "@ossy/sdk": "^1.5.0",
36
+ "@ossy/sdk-react": "^1.5.0",
37
+ "@ossy/themes": "^1.5.0",
38
38
  "@rollup/plugin-alias": "^6.0.0",
39
39
  "@rollup/plugin-babel": "6.1.0",
40
40
  "@rollup/plugin-commonjs": "^29.0.0",
@@ -67,5 +67,5 @@
67
67
  "README.md",
68
68
  "tsconfig.json"
69
69
  ],
70
- "gitHead": "ffccd09f5b13bddbf48e2b88ebb8b9d1e02c43f1"
70
+ "gitHead": "b1a36350ae22ae6776163952e3bf03b2a6cb6498"
71
71
  }