@vindo/react 0.0.3 → 0.0.5

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/lib/client.js CHANGED
@@ -246,7 +246,19 @@ export function render({head, body}, chunk) {
246
246
  var head = ReactDom.createRoot(head)
247
247
  var body = ReactDom.createRoot(body)
248
248
 
249
-
249
+ /**
250
+ * Proxy handler
251
+ */
252
+ function getter(target, key) {
253
+ switch(key) {
254
+ case 'set':
255
+ case 'update':
256
+ case 'render':
257
+ return update
258
+ }
259
+ return target.data.state[key]
260
+ }
261
+
250
262
  /**
251
263
  * Re-render DOM
252
264
  */
@@ -274,18 +286,10 @@ export function render({head, body}, chunk) {
274
286
  * Reference for mouse event functions
275
287
  */
276
288
  chunk.refs = {
277
- meta: args.meta,
278
289
  state: new Proxy(event, {
279
- get(target, key) {
280
- switch(key) {
281
- case 'set':
282
- case 'update':
283
- case 'render':
284
- return update
285
- }
286
- return target.data[key]
287
- }
288
- })
290
+ get: getter
291
+ }),
292
+ meta: args.meta,
289
293
  }
290
294
 
291
295
  /**
@@ -297,7 +301,7 @@ export function render({head, body}, chunk) {
297
301
  /**
298
302
  * Set only for dynamic content coming from server
299
303
  */
300
- if(args.data.children) {
304
+ if(args.data && args.data.children) {
301
305
  args.data.children = transform(args.data.children, chunk)[0]
302
306
  }
303
307
  body.render(chunk.body(args))
@@ -312,8 +316,12 @@ export function render({head, body}, chunk) {
312
316
  path: new URL(location.href)
313
317
  })
314
318
  }
315
-
316
- _http.get({type: 'hydrate'}).then(data => event.render(data))
319
+ /**
320
+ * Make sure the entire page has finished loading, including all dependent resources (images, scripts, CSS files, etc.).
321
+ */
322
+ window.onload = function onload() {
323
+ _http.get({type: 'hydrate'}).then(data => event.render(data))
324
+ }
317
325
  }
318
326
 
319
327
 
package/lib/server.js CHANGED
@@ -89,7 +89,20 @@ function dom(data) {
89
89
  * @param {object} html
90
90
  */
91
91
  function html(obj) {
92
- return '<!DOCTYPE html>'.concat(ReactDom.renderToString(obj))
92
+ const str = ReactDom.renderToString(obj)
93
+ /**
94
+ * Capture unchanged charSet attribute
95
+ */
96
+ const charset = /(charSet)=("utf-8")/
97
+ /**
98
+ * renderToString() failed to convert JSX charSet attribute to lower case.
99
+ *
100
+ * We'll convert the charset to lowe case manually to avoid W3C validation errors and
101
+ * potential parsing issues for strict crawlers, impacting SEO.
102
+ */
103
+ return '<!DOCTYPE html>'.concat(
104
+ str.replace(charset, (_, g1, g2) => g1.toLowerCase().concat(`=${g2}`))
105
+ )
93
106
  }
94
107
 
95
108
 
@@ -101,12 +114,18 @@ function html(obj) {
101
114
  function head(children, {meta}) {
102
115
  const env = process.env
103
116
 
117
+ /**
118
+ * Show script only on development
119
+ */
104
120
  if(env.NODE_ENV == 'dev' || env.NODE_ENV == 'develop' || env.NODE_ENV == 'development') {
105
121
  children = children.concat(
106
122
  create('script', {key: 0, src: env.DEV_SERVER}
107
123
  ))
108
124
  }
109
125
 
126
+ /**
127
+ * Inject bundle script by default. It can be disabled using meta.bundle = false
128
+ */
110
129
  return children.concat(
111
130
  meta.bundle && create('script', {
112
131
  key: 1,
@@ -127,6 +146,9 @@ function body(children, data) {
127
146
  if(!isArr(children)) {
128
147
  children = [children]
129
148
  }
149
+ /**
150
+ * Add meta data
151
+ */
130
152
  return children.map((child, key) => clone(child, {key, ...data}))
131
153
  }
132
154
 
@@ -149,7 +171,9 @@ function reducer(children) {
149
171
  if(!props) {
150
172
  return
151
173
  }
152
-
174
+ /**
175
+ * Component function
176
+ */
153
177
  if(isFunc(type)) {
154
178
  if(/^default_1/.test(type.name)) {
155
179
  throw new ReferenceError(`Component function requires a name. Currently have a default name of '${type.name}'.`)
@@ -182,7 +206,7 @@ function reducer(children) {
182
206
  if(isFunc(v)) {
183
207
  var f = v.toString()
184
208
  var m = [
185
- ...f.matchAll(/\((.*)\)(\s{|{)((.|\n)*)\}/g)
209
+ ...f.matchAll(/\((.*)\)(\s{|\s=>\s{|{)((.|\n)*)\}/g)
186
210
  ][0]
187
211
  p[i] = {
188
212
  name: i,
@@ -257,7 +281,7 @@ function HTTPState(req, events) {
257
281
 
258
282
  state.on = function on(name, cb) {
259
283
  events.on(mkId(name, req.name), async function(data) {
260
- if(!cb) {
284
+ if(typeof cb !== 'function') {
261
285
  throw new ReferenceError('Callback function is required.')
262
286
  }
263
287
  return await cb(data)
@@ -386,7 +410,7 @@ exports.server = function server() {
386
410
  }
387
411
 
388
412
 
389
- return function(req, res, next, {meta, events, exception}) {
413
+ return function(req, res, next, {meta, events}) {
390
414
  const state = HTTPState(req, events)
391
415
  const store = persist(state.type, req.body)
392
416
  /**
@@ -416,36 +440,29 @@ exports.server = function server() {
416
440
  * Render on first request
417
441
  */
418
442
  events.on('__render', function(comp) {
419
- if(isValid(comp)) {
420
- var args = prepare(comp, {
421
- meta,
422
- store,
423
- state: state.data,
424
- })
425
- /**
426
- * Initial content
427
- */
428
- if(state.type == 'initial') {
429
- data[manif.hash] = args.data
430
- }
431
- /**
432
- * Update content
433
- */
434
- if(state.type == 'route' || state.type == 'update' || state.type == 'store') {
435
- return dispatch(args.data)
436
- }
437
-
438
- /**
439
- * If the request not matched.
440
- */
441
- const errors = Object.keys(exception.statuses).concat('error')
442
- if(args.name && req.name) {
443
- if(req.route.back && args.name !== req.name && !errors.includes(args.name)) {
444
- return
445
- }
446
- }
447
- return args
443
+ if(!isValid(comp)) {
444
+ return
445
+ }
446
+
447
+ var args = prepare(comp, {
448
+ meta,
449
+ store,
450
+ state: state.data,
451
+ })
452
+ /**
453
+ * Initial content
454
+ */
455
+ if(state.type == 'initial') {
456
+ data[manif.hash] = args.data
457
+ }
458
+ /**
459
+ * Update content
460
+ */
461
+ if(state.type == 'route' || state.type == 'update' || state.type == 'store') {
462
+ return dispatch(args.data)
448
463
  }
464
+
465
+ return args
449
466
  })
450
467
 
451
468
  if(req.is(manif.hash)) {
package/lib/util.js CHANGED
@@ -6,33 +6,33 @@ export const merge = Object.assign
6
6
  export const isArr = Array.isArray
7
7
 
8
8
 
9
+
9
10
  export function isEmpty(obj) {
10
11
  if(obj && Object.keys(obj).length == 0) {
11
12
  return true
12
13
  }
13
14
  return false
14
15
  }
15
-
16
16
  export function isMore(val) {
17
17
  return isObj(val) || val == undefined ? 'jsx' : 'jsxs'
18
18
  }
19
-
20
19
  export function isNum(val) {
21
20
  return val && typeof val === 'number' && {}.toString.call(val) === '[object Number]'
22
21
  }
23
-
24
22
  export function isStr(val) {
25
23
  return val && typeof val === 'string' && {}.toString.call(val) === '[object String]'
26
24
  }
27
-
28
25
  export function isFunc(val) {
29
26
  return val && typeof val === 'function' && {}.toString.call(val) === '[object Function]'
30
27
  }
31
-
32
28
  export function isObj(val) {
33
29
  return val && typeof val === 'object' && val.constructor === Object && Object.prototype === Object.getPrototypeOf(val)
34
30
  }
35
31
 
32
+
33
+ /**
34
+ * Create function
35
+ */
36
36
  export function toFunc(name, {code, args = [], refs}) {
37
37
  var arr = ['return', 'function', name]
38
38
 
@@ -66,7 +66,12 @@ export function transform(children, data) {
66
66
  return
67
67
  }
68
68
  if(isArr(type)) {
69
- type = data[type[0]]
69
+ const key = type[0]
70
+
71
+ type = data[key]
72
+ if(!type) {
73
+ throw TypeError(`Cannot read property '${key}'.`)
74
+ }
70
75
  }
71
76
 
72
77
  for(var i in props) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vindo/react",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "React SSR for @vindo/core framework.",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",
@@ -22,18 +22,10 @@
22
22
  "react-dom": "*"
23
23
  },
24
24
  "exports": {
25
- ".": {
26
- "default": "./index.js"
27
- },
28
- "./util": {
29
- "default": "./lib/util.js"
30
- },
31
- "./client": {
32
- "default": "./lib/client.js"
33
- },
34
- "./request": {
35
- "default": "./lib/request.js"
36
- }
25
+ ".": "./index.js",
26
+ "./util": "./lib/util.js",
27
+ "./client": "./lib/client.js",
28
+ "./request": "./lib/request.js"
37
29
  },
38
30
  "author": "Ruel Mindo",
39
31
  "license": "MIT",