pawa-ssr 1.3.9 → 1.3.11

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/index.js CHANGED
@@ -5,7 +5,7 @@ import { propsValidator, evaluateExpr,extractAtExpressions, reArrangeAttri,resum
5
5
  import {AsyncLocalStorage} from'node:async_hooks'
6
6
  import { If,For,State,Switch, Key } from'./power.js';
7
7
  import PawaElement from'./pawaElement.js'
8
- import { pluginsMap } from "pawajs";
8
+ import { pluginsMap, lazyComponents } from "pawajs";
9
9
 
10
10
  const PAWA_STORE_SYMBOL = Symbol.for('pawa.ssr.store');
11
11
 
@@ -783,8 +783,9 @@ const textContentHandler = async(el) => {
783
783
  const nodesMap = new Map();
784
784
  const currentHtmlString = el.outerHTML;
785
785
  el.setAttribute('c-t',true)
786
+ const newString=el.innerHTML.replace('-','(//)')
786
787
  const document = el.ownerDocument
787
- const comment=document.createComment(`textEvaluator-${el.innerHTML}`)
788
+ const comment=document.createComment(`textEvaluator-${newString}`)
788
789
  el.appendChild(comment)
789
790
  // Get all text nodes and store their original content
790
791
  const textNodes = Array.from(el.childNodes).filter(node => node.nodeType === 3);
@@ -889,7 +890,7 @@ const attributeHandler =async (el, attr) => {
889
890
  partlyPawajsDirective.add(v)
890
891
  })
891
892
  }
892
- addToPartlyDirective('else','else-if','case','default')
893
+ addToPartlyDirective('else','else-if','case','s-default')
893
894
  const checkIfRemove=(el)=>{
894
895
  for (const v of partlyPawajsDirective) {
895
896
  if(el.hasAttribute(v)) return true
@@ -1016,6 +1017,16 @@ export const render =async (el, contexts = {},stream) => {
1016
1017
  return
1017
1018
  }
1018
1019
  if (el._componentName) {
1020
+ if(el._lazy && lazyComponents.has(el.tagName)){
1021
+ const lazyComponent=lazyComponents.get(el.tagName)
1022
+ const {name,component} =lazyComponent()
1023
+ const compo=await component()
1024
+ if(compo[name]){
1025
+ components.set(name,compo[name])
1026
+ el._component.component=compo[name]
1027
+ lazyComponents.delete(el.tagName)
1028
+ }
1029
+ }
1019
1030
  if(isStream){
1020
1031
  await streamingComponent(el,stream)
1021
1032
  return
@@ -1206,7 +1217,7 @@ const resolvesAsync=async({component,
1206
1217
  const bufferStream=(string)=>{
1207
1218
  chunk+=string
1208
1219
  }
1209
- bufferStream(`<div id="p${id}" hidden>`)
1220
+ bufferStream(`<div id="res-${id}" hidden>`)
1210
1221
  store.getStore().stateContext=appContext
1211
1222
  const compo=await component.then((res)=>res)
1212
1223
  const div=root.createElement('div')
@@ -1245,36 +1256,31 @@ const resolvesAsync=async({component,
1245
1256
  bufferStream(`
1246
1257
  <script class="p${id}">
1247
1258
  const s${id}=()=>{
1248
- let p=document.querySelectorAll("#p${id}")
1249
- if(p.length < 2){
1250
- if(p[0]) p[0].remove()
1251
- document.querySelector('.p${id}').remove()
1252
- return
1253
- }
1254
- let c=p[0].previousSibling
1255
- c.data='${commentData}'
1256
- p[0].remove()
1257
- const sc=p[0]?._stateContext
1258
- let ec=c.nextSibling
1259
- let fc=p[1].firstElementChild
1260
- p[0].removeAttribute('pawa-avoid')
1261
- Array.from(p[0].attributes).forEach(a => {
1262
- if(a.name === 'p-async') return
1263
- if (a.name === 'p:c') {
1264
- let o=a.value + (fc.getAttribute('p:c') || '')
1265
- fc.setAttribute('p:c',o)
1266
- }else{
1267
- fc.setAttribute(a.name,a.value)
1259
+ const placeholder = document.getElementById("p${id}");
1260
+ const resolved = document.getElementById("res-${id}");
1261
+ if(!placeholder || !resolved) return;
1262
+
1263
+ const comment = placeholder.previousSibling;
1264
+ if(comment && comment.nodeType === 8) comment.data = '${commentData}';
1265
+
1266
+ const firstChild = resolved.firstElementChild;
1267
+ Array.from(placeholder.attributes).forEach(a => {
1268
+ if(a.name === 'p-async') return;
1269
+ if (a.name === 'p:c') {
1270
+ firstChild.setAttribute('p:c', a.value + (firstChild.getAttribute('p:c') || ''));
1271
+ } else {
1272
+ firstChild.setAttribute(a.name, a.value);
1268
1273
  }
1269
- });
1270
- p[1].childNodes.forEach(e => {
1271
- c.parentElement.insertBefore(e,ec)
1272
- });
1273
- if (window?.__pawaDev) {
1274
- window.__pawaStream(fc,p[0]._context,sc)
1275
- }
1276
- p[1].remove()
1274
+ });
1275
+
1276
+ while(resolved.firstChild) placeholder.parentNode.insertBefore(resolved.firstChild, placeholder);
1277
+ placeholder.remove();
1278
+ resolved.remove();
1279
+
1280
+ if (window?.__pawaDev) {
1281
+ window.__pawaStream(firstChild, {}, {});
1277
1282
  }
1283
+ }
1278
1284
  s${id}()
1279
1285
  document.querySelector('.p${id}').remove()
1280
1286
  </script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pawa-ssr",
3
- "version": "1.3.9",
3
+ "version": "1.3.11",
4
4
  "type":"module",
5
5
  "description": "pawajs ssr libary",
6
6
  "main": "index.js",
@@ -25,6 +25,6 @@
25
25
  "homepage": "https://github.com/Allisboy/pawajs-ssr#readme",
26
26
  "dependencies": {
27
27
  "linkedom": "^0.18.11",
28
- "pawajs": "^1.4.28"
28
+ "pawajs": "^1.4.36"
29
29
  }
30
30
  }
package/pawaElement.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { HTMLElement, parseHTML } from "linkedom"
2
2
  import {getPawaAttributes, getDevelopment } from "./index.js"
3
- import {components} from 'pawajs'
3
+ import {components,lazyComponents} from 'pawajs'
4
4
  import PawaComponent from "./pawaComponent.js"
5
5
  import { evaluateExpr, splitAndAdd,replaceTemplateOperators } from "./utils.js"
6
6
 
@@ -32,6 +32,7 @@ class PawaElement {
32
32
  this._pawaAlready=element.hasAttribute('p:c')
33
33
  /**@type {Array<{message:string,stack:string}>} */
34
34
  this._error=[]
35
+ this._lazy=false
35
36
  this._running=false
36
37
  this._hasForOrIf=this.hasForOrIf
37
38
  this._createError=this.createError
@@ -77,6 +78,11 @@ class PawaElement {
77
78
  this._arrangeAttribute[value.name]=value.value
78
79
  })
79
80
  }
81
+ checkLazy(){
82
+ if (lazyComponents.has(splitAndAdd(this._el.tagName))) {
83
+ this._lazy=true
84
+ }
85
+ }
80
86
  setResumeAttr(name){
81
87
  if(name.startsWith(':')) return
82
88
  this._resumeAttr+=`${name};`
@@ -149,9 +155,10 @@ class PawaElement {
149
155
  }
150
156
 
151
157
  getComponent(){
152
- if (components.has(splitAndAdd(this._el.tagName.toUpperCase())) && !this._client) {
158
+ if (components.has(splitAndAdd(this._el.tagName.toUpperCase())) && !this._client || this._lazy) {
153
159
  this._componentName=splitAndAdd(this._el.tagName.toUpperCase())
154
- this._component=new PawaComponent(components.get(splitAndAdd(this._el.tagName.toUpperCase())))
160
+ const fakeCompo=()=>true
161
+ this._component=new PawaComponent(components.get(splitAndAdd(this._el.tagName.toUpperCase())),fakeCompo)
155
162
  Array.from(this._el.children).forEach(slot =>{
156
163
 
157
164
  if (slot.tagName === 'TEMPLATE' && slot.getAttribute('prop')) {
package/power.js CHANGED
@@ -126,8 +126,8 @@ export const Switch = async(el, attr,stream) => {
126
126
  const getChained = (sibling) => {
127
127
  while (sibling) {
128
128
  const next = sibling.nextElementSibling;
129
- const isCase = sibling.hasAttribute('case');
130
- const isDefault = sibling.hasAttribute('default');
129
+ const isCase = sibling.hasAttribute('case') && !sibling.hasAttribute('switch');
130
+ const isDefault = sibling.hasAttribute('s-default');
131
131
 
132
132
  if (isCase) {
133
133
  const exp = sibling.getAttribute('case');
@@ -255,7 +255,7 @@ export const For=async(el,attr,stream)=>{
255
255
  if (at.name.startsWith('c-')) {
256
256
  store.push(at)
257
257
  copyElement.removeAttribute(at.name)
258
- }
258
+ }
259
259
  })
260
260
 
261
261
  const template=document.createElement('template')
@@ -334,6 +334,7 @@ export const For=async(el,attr,stream)=>{
334
334
  }
335
335
  }
336
336
 
337
+
337
338
  export const State=async(el,attr)=>{
338
339
  if (el._running) {
339
340
  return
package/utils.js CHANGED
@@ -187,7 +187,12 @@ export const convertToNumber=(str)=>{
187
187
  return hash
188
188
  };
189
189
  export const ComponentProps=(somes,message,name)=>{
190
- let some=somes?.() || somes
190
+ let some
191
+ if (typeof somes === 'function') {
192
+ some=somes()
193
+ }else{
194
+ some=somes
195
+ }
191
196
  return({
192
197
  Array:()=>{
193
198