Skip to content

Commit 798afc0

Browse files
committed
Prepared the client for offline handling
1 parent 23435ce commit 798afc0

File tree

8 files changed

+38
-30
lines changed

8 files changed

+38
-30
lines changed

src/js/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{
2-
"description": "reactpy-django client",
3-
"main": "src/index.ts",
2+
"description": "ReactPy-Django Client",
3+
"main": "src/index.tsx",
44
"type": "module",
5-
"files": [
6-
"src/**/*.js"
7-
],
85
"scripts": {
96
"build": "rollup --config",
107
"format": "prettier --ignore-path .gitignore --write ."

src/js/rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import replace from "@rollup/plugin-replace";
44
import typescript from "@rollup/plugin-typescript";
55

66
export default {
7-
input: "src/index.ts",
7+
input: "src/index.tsx",
88
output: {
99
file: "../reactpy_django/static/reactpy_django/client.js",
1010
format: "esm",

src/js/src/client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export class ReactPyDjangoClient
1212
{
1313
urls: ReactPyUrls;
1414
socket: { current?: WebSocket };
15+
mountElement: HTMLElement | null = null;
16+
prerenderElement: HTMLElement | null = null;
17+
offlineElement: HTMLElement | null = null;
1518

1619
constructor(props: ReactPyDjangoClientProps) {
1720
super();
@@ -23,6 +26,9 @@ export class ReactPyDjangoClient
2326
this.handleIncoming(JSON.parse(data)),
2427
...props.reconnectOptions,
2528
});
29+
this.mountElement = props.mountElement;
30+
this.prerenderElement = props.prerenderElement;
31+
this.offlineElement = props.offlineElement;
2632
}
2733

2834
sendMessage(message: any): void {

src/js/src/index.ts renamed to src/js/src/index.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { mount } from "./mount";
1+
22
import { ReactPyDjangoClient } from "./client";
3+
import React from "react";
4+
import { render } from "react-dom";
5+
import { Layout } from "@reactpy/client/src/components";
36

47
export function mountComponent(
58
mountElement: HTMLElement,
@@ -59,8 +62,24 @@ export function mountComponent(
5962
backoffMultiplier: reconnectBackoffMultiplier,
6063
maxRetries: reconnectMaxRetries,
6164
},
65+
mountElement: mountElement,
66+
prerenderElement: document.getElementById(
67+
mountElement.id + "-prerender"
68+
),
69+
offlineElement: document.getElementById(mountElement.id + "-offline"),
6270
});
6371

72+
73+
// Replace the prerender element with the real element on the first layout update
74+
if (client.prerenderElement) {
75+
client.onMessage("layout-update", ({ path, model }) => {
76+
if (client.prerenderElement) {
77+
client.prerenderElement.replaceWith(client.mountElement);
78+
client.prerenderElement = null;
79+
}
80+
});
81+
}
82+
6483
// Start rendering the component
65-
mount(mountElement, client);
84+
render(<Layout client={client} />, client.mountElement);
6685
}

src/js/src/mount.tsx

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/js/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ export type ReactPyUrls = {
1414
export type ReactPyDjangoClientProps = {
1515
urls: ReactPyUrls;
1616
reconnectOptions: ReconnectOptions;
17+
mountElement: HTMLElement | null;
18+
prerenderElement: HTMLElement | null;
19+
offlineElement: HTMLElement | null;
1720
};

src/js/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ export function createReconnectingWebSocket(props: {
3232
};
3333
socket.current.onmessage = props.onMessage;
3434
socket.current.onclose = () => {
35+
if (props.onClose) {
36+
props.onClose();
37+
}
3538
if (!everConnected) {
3639
console.info("ReactPy failed to connect!");
3740
return;
3841
}
3942
console.info("ReactPy disconnected!");
40-
if (props.onClose) {
41-
props.onClose();
42-
}
4343
if (retries >= maxRetries) {
4444
console.info("ReactPy connection max retries exhausted!");
4545
return;

src/js/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2017",
3+
"target": "ES2022",
44
"module": "esnext",
55
"moduleResolution": "node",
66
"jsx": "react",
7+
"allowSyntheticDefaultImports": true
78
},
89
"paths": {
910
"react": [

0 commit comments

Comments
 (0)