Skip to content

Commit bb532b4

Browse files
committed
Implemented hmr path
Updating People works but updating stateNavigator doesn't quite work. The update lags behind, so update 1 is only processed after update 2 - think it's because the rsc stream update happens before the updated state navigator loads in. With Person it's a server component so doesn't need loading - the rsc update contains the update
1 parent e522409 commit bb532b4

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

packages/plugin-rsc/examples/navigation/src/HmrProvider.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const HmrProvider = ({ children }: any) => {
66
const { setRoot, deserialize } = useContext(BundlerContext)
77
const { stateNavigator } = useNavigationEvent()
88
useEffect(() => {
9-
const onHmrReload = (e: any) => {
10-
e.preventDefault()
9+
const onHmrReload = () => {
1110
const {
1211
stateContext: {
1312
state,
@@ -34,8 +33,8 @@ const HmrProvider = ({ children }: any) => {
3433
stateNavigator.historyManager.stop()
3534
setRoot(root)
3635
}
37-
window.addEventListener('parcelhmrreload', onHmrReload)
38-
return () => window.removeEventListener('parcelhmrreload', onHmrReload)
36+
import.meta.hot?.on("rsc:update", onHmrReload);
37+
return () => import.meta.hot?.off("rsc:update", onHmrReload);
3938
})
4039
return children
4140
}

packages/plugin-rsc/examples/navigation/src/client.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { BundlerContext } from 'navigation-react';
1212
async function hydrate() {
1313
const initialPayload = await createFromReadableStream(rscStream);
1414
function Shell() {
15-
const [payload, setPayload] = useState(initialPayload);
15+
const [payload, setPayload] = useState(initialPayload.root);
1616
const bundler = useMemo(() => ({setRoot: setPayload, deserialize: fetchRSC}), []);
1717
return (
1818
<BundlerContext.Provider value={bundler}>
19-
{payload.root}
19+
{payload}
2020
</BundlerContext.Provider>
2121
);
2222
}

packages/plugin-rsc/examples/navigation/src/server.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,35 @@ import stateNavigator from './stateNavigator.ts'
55
export default async function handler(request: Request): Promise<Response> {
66
let url: string;
77
let view: any;
8+
const { NavigationHandler } = await import('navigation-react');
9+
const serverNavigator = new StateNavigator(stateNavigator);
810
if (request.method === 'PUT') {
911
const sceneViews: any = {
1012
people: await import('./People.tsx'),
1113
person: await import('./Person.tsx'),
1214
friends: await import('./Friends.tsx')
1315
};
14-
const {url: reqUrl, sceneViewKey} = await request.json();
15-
url = reqUrl;
16-
const SceneView = sceneViews[sceneViewKey].default;
17-
view = <SceneView />;
16+
const {url: reqUrl, sceneViewKey, state, data, crumbs} = await request.json();
17+
if (reqUrl) {
18+
url = reqUrl;
19+
const SceneView = sceneViews[sceneViewKey].default;
20+
view = <SceneView />;
21+
} else {
22+
let fluentNavigator = serverNavigator.fluent();
23+
for (let i = 0; i < crumbs.length; i++) {
24+
fluentNavigator = fluentNavigator.navigate(crumbs[i].state, crumbs[i].data);
25+
}
26+
fluentNavigator = fluentNavigator.navigate(state, data);
27+
url = fluentNavigator.url;
28+
const App = (await import('./App.tsx')).default;
29+
view = <App url={url} />;
30+
}
1831
} else {
1932
let reqUrl = new URL(request.url);
2033
url = `${reqUrl.pathname}${reqUrl.search}`;
2134
const App = (await import('./App.tsx')).default;
2235
view = <App url={url} />;
2336
}
24-
const { NavigationHandler } = await import('navigation-react');
25-
const serverNavigator = new StateNavigator(stateNavigator);
2637
serverNavigator.navigateLink(url)
2738
const root = (
2839
<>

0 commit comments

Comments
 (0)