Skip to content

Commit

Permalink
fix(prerendering): async h() function
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Bradley <adamdbradley@users.noreply.github.com>
  • Loading branch information
manucorporat and adamdbradley committed Aug 24, 2020
1 parent 03864f2 commit d6eabb9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/hydrate/platform/h-async.ts
@@ -0,0 +1,9 @@
import * as d from '../../declarations';
import { h } from '@runtime';

export const hAsync = async (nodeName: any, vnodeData: any, ...children: d.ChildType[]) => {
if (children) {
children = await Promise.all(children);
}
return h(nodeName, vnodeData, ...children);
};
31 changes: 30 additions & 1 deletion src/hydrate/platform/index.ts
Expand Up @@ -142,4 +142,33 @@ export const modeResolutionChain: d.ResolutionHandler[] = [];
export { BUILD, NAMESPACE } from '@app-data';
export { hydrateApp } from './hydrate-app';

export * from '@runtime';
export {
addHostEventListeners,
attachShadow,
defineCustomElement,
forceModeUpdate,
proxyCustomElement,
bootstrapLazy,
connectedCallback,
createEvent,
disconnectedCallback,
getAssetPath,
setAssetPath,
getConnect,
getContext,
getElement,
getValue,
setValue,
Host,
insertVdomAnnotations,
parsePropertyValue,
forceUpdate,
postUpdateComponent,
getRenderingRef,
proxyComponent,
renderVdom,
setMode,
getMode,
} from '@runtime';

export { hAsync as h } from './h-async';
8 changes: 6 additions & 2 deletions src/runtime/update-component.ts
Expand Up @@ -66,7 +66,7 @@ const dispatchHooks = (hostRef: d.HostRef, isInitialLoad: boolean) => {
return then(promise, () => updateComponent(hostRef, instance, isInitialLoad));
};

const updateComponent = (hostRef: d.HostRef, instance: any, isInitialLoad: boolean) => {
const updateComponent = async (hostRef: d.HostRef, instance: any, isInitialLoad: boolean) => {
// updateComponent
const elm = hostRef.$hostElement$ as d.RenderNode;
const endUpdate = createTime('update', hostRef.$cmpMeta$.$tagName$);
Expand All @@ -86,7 +86,11 @@ const updateComponent = (hostRef: d.HostRef, instance: any, isInitialLoad: boole
// looks like we've got child nodes to render into this host element
// or we need to update the css class/attrs on the host element
// DOM WRITE!
renderVdom(hostRef, callRender(hostRef, instance));
if (BUILD.hydrateServerSide) {
renderVdom(hostRef, await callRender(hostRef, instance));
} else {
renderVdom(hostRef, callRender(hostRef, instance));
}
} else {
elm.textContent = callRender(hostRef, instance);
}
Expand Down
13 changes: 9 additions & 4 deletions test/hello-world/src/components/hello-world.tsx
@@ -1,11 +1,16 @@
import { Component } from '@stencil/core';
import { HelloWorldText } from 'hello-world-text';
import { Component, h } from '@stencil/core';

@Component({
tag: 'hello-world'
tag: 'hello-world',
})
export class HelloWorld {
render() {
return HelloWorldText;
return <div>{getData()}</div>;
}
}

const getData = async () => {
const rsp = await fetch('http://ionic.io/');
const txt = await rsp.text();
return txt;
};

0 comments on commit d6eabb9

Please sign in to comment.