Skip to content

Commit

Permalink
Merge pull request #291 from fastrodev/react
Browse files Browse the repository at this point in the history
update #initHtml
  • Loading branch information
ynwd authored Oct 31, 2023
2 parents f586df9 + abb1e12 commit d6d799c
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions http/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class Render {

const plaintext = JSON.stringify(this.#options.props);
const encryptedData = await encryptData(importedKey, plaintext);
return `${env}window.__INITIAL_DATA__ = "${encryptedData}";`;
return `<script>${env}window.__INITIAL_DATA__ = "${encryptedData}";</script>`;
};

#initHtml = async (element: Component, html?: any) => {
Expand Down Expand Up @@ -199,6 +199,13 @@ export class Render {
>
{el}
</div>
{this.#options.props && (
<div
dangerouslySetInnerHTML={{
__html: await this.#createInitScript(),
}}
/>
)}
{this.#options.html?.body?.script &&
this.#options.html?.body?.script.map((s) => (
<script
Expand Down Expand Up @@ -244,7 +251,6 @@ es.onmessage = function(e) {
compID = `${fc.name}${this.#reqUrl}`;
if (cached && this.#nest[compID]) return this.#nest[compID];

this.#injectInitScript();
await this.#handleComponent(fc, this.#options.hydrate);
const html = await this.#initHtml(e, this.#options.html);
return this.#nest[compID] = html;
Expand All @@ -270,31 +276,6 @@ es.onmessage = function(e) {
}
};

#injectInitScript = () => {
if (!this.#options.html) return;
const initPath = `${this.#staticPath}/init.js`;
this.#server.push(
"GET",
initPath,
async (req: Request) => {
const ref = checkReferer(req);
if (ref) return ref;

return new Response(await this.#createInitScript(this.#options.props), {
headers: {
"Content-Type": "application/javascript",
},
});
},
);

if (this.#options.html?.body?.script) {
this.#options.html?.body.script?.push({
src: initPath,
});
}
};

#handleDevelopment = () => {
if (!this.#development) return;
const refreshPath = `${this.#staticPath}/refresh.js`;
Expand Down

0 comments on commit d6d799c

Please sign in to comment.