Skip to content

Commit

Permalink
fix: fix update of snapshot cache
Browse files Browse the repository at this point in the history
  • Loading branch information
answershuto committed Nov 6, 2023
1 parent 54b85a2 commit 92cd154
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/with-first-chunk-cache-ssr/src/document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function Document() {
<meta charSet="utf-8" />
<meta name="description" content="ICE 3.0 Demo" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style dangerouslySetInnerHTML={{ __html: ' body {margin: 0px;}' }} />
<Meta />
<Title />
<Links />
Expand Down
1 change: 1 addition & 0 deletions packages/ice/src/middlewares/renderMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default function createRenderMiddleware(options: Options): Middleware {
req,
res,
};
res.setHeader('fcc-enable', 'true');
serverModule.renderToResponse(requestContext, {
renderMode,
documentOnly,
Expand Down
20 changes: 14 additions & 6 deletions packages/runtime/src/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,31 @@ export const FirstChunkCache: FirstChunkCacheType = () => {
export type SnapshotCacheType = () => JSX.Element;

function Snapshot(rootId) {
// Run before </html>.
const SNAPSHOT_ID = 'fcc-snapshot';
const FCC_FLAG = decodeURIComponent('%3C!--fcc--%3E');
// Template before stream.
// Cache template before stream.
const iceContainerEle = document.getElementById(rootId);
let snapshot = document.documentElement.outerHTML;

window.addEventListener('stream-end', () => {
// Remove snapshot when stream-end.
// Remove snapshot when stream-end event.
const snapshotEle = document.getElementById(SNAPSHOT_ID);
snapshotEle && (snapshotEle.innerHTML = null);
const snapshotOuterHTML = snapshotEle.outerHTML;
if (snapshotEle) {
snapshotEle.removeAttribute('style');
snapshotEle.innerHTML = '';
}

const hasCacheFlag = snapshot.indexOf(FCC_FLAG) >= 0;
if (hasCacheFlag) {
let cacheStr = snapshot.split(FCC_FLAG)[0] + FCC_FLAG;
const style = 'position:absolute;width:100%;top:0;left:0;z-index:999';
cacheStr = cacheStr.replace(`<div id="${SNAPSHOT_ID}"></div>`, `<div id="${SNAPSHOT_ID}" style="${style}">${iceContainerEle.innerHTML}</div>`);
cacheStr = cacheStr.replace(snapshotOuterHTML, `<div id="${SNAPSHOT_ID}" style="${style}">${iceContainerEle.innerHTML}</div>`);
console.log('cacheStr', cacheStr);
if (!window.__windvane__) return;
window.__windvane__.call('WVClient.setCache', {
let mega = (window as any).__megability_bridge__;
if (!mega) return;
mega.asyncCall('ssr', 'setFirstChunkCache', {
html: cacheStr,
});
console.log('cache success');
Expand Down

0 comments on commit 92cd154

Please sign in to comment.