Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: server router #501

Merged
merged 44 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
89ad3ba
init
Aslemammad Feb 13, 2024
5cc8355
rely on httpServer
Aslemammad Feb 15, 2024
edb0a32
update types
Aslemammad Feb 15, 2024
86df2e6
update
Aslemammad Feb 15, 2024
a8c041d
router example waku workspace update
Aslemammad Feb 15, 2024
69e158b
update
Aslemammad Feb 15, 2024
2484fe5
update
Aslemammad Feb 15, 2024
250a6da
update
Aslemammad Feb 15, 2024
071a408
init
Aslemammad Feb 18, 2024
1896d58
format
Aslemammad Feb 18, 2024
7828c58
pnpm lock
Aslemammad Feb 18, 2024
20603e6
failing test (that works)
Aslemammad Feb 18, 2024
b90910a
remove slot
Aslemammad Feb 18, 2024
317b2e3
address reviews
Aslemammad Feb 19, 2024
281ca8f
less changes
Aslemammad Feb 19, 2024
4ddbad3
format
Aslemammad Feb 19, 2024
d76061d
some debugging
Aslemammad Feb 19, 2024
fb9df3b
remove useLocation?
Aslemammad Feb 19, 2024
f28f9c6
restort pnpm lock
Aslemammad Feb 19, 2024
208d6d8
restort pnpm lock
Aslemammad Feb 19, 2024
95bf0b4
Update packages/waku/src/router/client.ts
Aslemammad Feb 19, 2024
45cf37b
update
Aslemammad Feb 19, 2024
513367e
update
Aslemammad Feb 19, 2024
7d78b25
update
Aslemammad Feb 19, 2024
aeb11d5
revert
Aslemammad Feb 19, 2024
ad44a2f
debug ci cd
Aslemammad Feb 19, 2024
95ab523
debug ci cd
Aslemammad Feb 19, 2024
ac1b9c1
debug ci cd
Aslemammad Feb 19, 2024
52aee0d
debug ci cd
Aslemammad Feb 19, 2024
7dfb652
debug ci cd
Aslemammad Feb 19, 2024
937fbd1
debug ci cd
Aslemammad Feb 19, 2024
cc20bb2
debug ci cd
Aslemammad Feb 19, 2024
3e89f6d
debug ci cd
Aslemammad Feb 19, 2024
dcf409a
debug ci cd
Aslemammad Feb 19, 2024
27db39d
debug ci cd
Aslemammad Feb 19, 2024
7e869db
debug ci cd
Aslemammad Feb 19, 2024
e3e0404
debug ci cd
Aslemammad Feb 19, 2024
40dd0e6
debug ci cd
Aslemammad Feb 19, 2024
cad613c
debug ci cd
Aslemammad Feb 20, 2024
4f3b000
debug ci cd
Aslemammad Feb 20, 2024
e3bc738
debug ci cd
Aslemammad Feb 20, 2024
941711c
some removal
Aslemammad Feb 20, 2024
83f5b13
some removal
Aslemammad Feb 20, 2024
57bffa5
some removal
Aslemammad Feb 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/07_router/src/components/Counter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import { useState } from 'react';

import { Link } from 'waku/router/client';
import { Link, useLocation } from 'waku/router/client';

export const Counter = () => {
const { path } = useLocation();
const [count, setCount] = useState(0);
return (
<div style={{ border: '3px blue dashed', margin: '1em', padding: '1em' }}>
<p>Count: {count}</p>
<button onClick={() => setCount((c) => c + 1)}>Increment</button>
<h3>This is a client component.</h3>
<span>path: {path}</span>
<Link to="/">Go to Home</Link>
</div>
);
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@
"eslint-plugin-react-hooks": "^4.6.0",
"prettier": "^3.2.5",
"prettier-plugin-tailwindcss": "^0.5.11",
"react": "18.3.0-canary-14fd9630e-20240213",
"react-dom": "18.3.0-canary-14fd9630e-20240213",
"react-server-dom-webpack": "18.3.0-canary-14fd9630e-20240213",
Comment on lines -74 to -76
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this causing the CI issue?

"terminate": "^2.6.1",
"typescript": "^5.3.3",
"wait-port": "^1.1.0",
Expand Down
34 changes: 34 additions & 0 deletions packages/waku/src/router/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
AnchorHTMLAttributes,
ReactElement,
MouseEvent,
PropsWithChildren,
} from 'react';

import { prefetchRSC, Root, Slot, useRefetch } from '../client.js';
Expand Down Expand Up @@ -324,3 +325,36 @@ export function Router() {
createElement(InnerRouter),
);
}

function notAvailableInServer(name: string) {
return () => {
throw new Error(`${name} is not in the server`);
};
}

/**
* ServerRouter for SSR
* This is not a public API.
*/
export function ServerRouter({
children,
loc,
}: PropsWithChildren<{
loc: ReturnType<typeof parseLocation>;
}>) {
return createElement(
Fragment,
null,
createElement(
RouterContext.Provider,
{
value: {
loc,
changeLocation: notAvailableInServer('changeLocation'),
prefetchLocation: notAvailableInServer('prefetchLocation'),
},
},
children,
),
);
}
9 changes: 5 additions & 4 deletions packages/waku/src/router/defineRouter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement, Fragment } from 'react';
import { createElement } from 'react';
import type { FunctionComponent, ReactNode } from 'react';

import { defineEntries } from '../server.js';
Expand All @@ -14,6 +14,7 @@ import {
import type { RouteProps, ShouldSkip } from './common.js';
import { getPathMapping } from '../lib/utils/path.js';
import type { PathSpec } from '../lib/utils/path.js';
import { ServerRouter } from './client.js';

const ShoudSkipComponent = ({ shouldSkip }: { shouldSkip: ShouldSkip }) =>
createElement('meta', {
Expand Down Expand Up @@ -142,7 +143,7 @@ globalThis.__WAKU_ROUTER_PREFETCH__ = (path) => {
return buildConfig;
};

const getSsrConfig: GetSsrConfig = async (pathname) => {
const getSsrConfig: GetSsrConfig = async (pathname, { searchParams }) => {
if (!(await existsPath(pathname))) {
if (await has404Promise) {
pathname = '/404';
Expand All @@ -153,8 +154,8 @@ globalThis.__WAKU_ROUTER_PREFETCH__ = (path) => {
const componentIds = getComponentIds(pathname);
const input = getInputString(pathname);
const body = createElement(
Fragment,
null,
ServerRouter,
{ loc: { path: pathname, searchParams } },
createElement(Slot, { id: SHOULD_SKIP_ID }),
componentIds.reduceRight(
(acc: ReactNode, id) => createElement(Slot, { id, fallback: acc }, acc),
Expand Down
Loading
Loading