Skip to content

Commit

Permalink
fix: runtime should compat with old browsers (#6478)
Browse files Browse the repository at this point in the history
* fix: runtime should compat with old browsers

* fix: optimize code

* fix: lint

* chore: optimize code

* chore: optimize code
  • Loading branch information
ClarkXia authored Aug 24, 2023
1 parent 47ba84d commit d4904e9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-rules-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/runtime': patch
---

fix: runtime should compatible with old browsers
2 changes: 1 addition & 1 deletion packages/runtime/src/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const Data: DataType = (props: DataProps) => {
// Should merge global context when there are multiple <Data />.
<ScriptElement
suppressHydrationWarning={documentOnly}
dangerouslySetInnerHTML={{ __html: `window.__ICE_APP_CONTEXT__=Object.assign(${JSON.stringify(windowContext)}, window.__ICE_APP_CONTEXT__ || {});` }}
dangerouslySetInnerHTML={{ __html: `!(function () {var a = window.__ICE_APP_CONTEXT__ || {};var b = ${JSON.stringify(windowContext)};for (var k in a) {b[k] = a[k]}window.__ICE_APP_CONTEXT__=b;})();` }}
/>
);
};
Expand Down
17 changes: 13 additions & 4 deletions packages/runtime/src/Suspense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ const SuspenseContext = React.createContext<SuspenseState | undefined>(undefined

const getHydrateData = (id: string) => {
let data = null;
const hasHydrateData = isClient && window[LOADER] && window[LOADER].has(id);
if (hasHydrateData) {
data = window[LOADER].get(id);
const loaderData = isClient && window[LOADER];
let hasHydrateData: boolean;
if (loaderData) {
// Compatible with the old version which use Map to store data.
if (loaderData.has) {
hasHydrateData = loaderData.has(id);
data = window[LOADER].get(id);
} else {
hasHydrateData = Object.prototype.hasOwnProperty.call(loaderData, id);
// If hasHydrateData is false, data will be undefined.
data = window[LOADER][id];
}
}
return {
hasHydrateData,
Expand Down Expand Up @@ -157,6 +166,6 @@ function Data(props) {
const data = useSuspenseData();

return (
<script dangerouslySetInnerHTML={{ __html: `if (!window.${LOADER}) { window.${LOADER} = new Map();} window.${LOADER}.set('${props.id}', ${JSON.stringify(data)})` }} />
<script dangerouslySetInnerHTML={{ __html: `!function(){window['${LOADER}'] = window['${LOADER}'] || {};window['${LOADER}']['${props.id}'] = ${JSON.stringify(data)}}();` }} />
);
}

0 comments on commit d4904e9

Please sign in to comment.