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

breaking(router): string query #799

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ We recommend other frameworks for heavy ecommerce or enterprise applications. Wa
Start a new Waku project with the `create` command for your preferred package manager. It will scaffold a new project with our default [Waku starter](https://github.com/dai-shi/waku/tree/main/examples/01_template).

```
npm create waku@latest
npm create waku@next
```

**Node.js version requirement:** `^20.8.0` or `^18.17.0`
Expand Down Expand Up @@ -215,7 +215,7 @@ export const getConfig = async () => {

### Pages

Pages render a single route, segment route, or catch-all route based on the file system path (conventions below). All page components automatically receive two props related to the rendered route: `path` (string) and `searchParams` ([URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)).
Pages render a single route, segment route, or catch-all route based on the file system path (conventions below). All page components automatically receive two props related to the rendered route: `path` (string) and `query` (string).

#### Single routes

Expand Down Expand Up @@ -493,20 +493,20 @@ The `useRouter` hook can be used to inspect the current route or perform program

#### router properties

The `router` object has two properties related to the current route: `path` (string) and `searchParams` ([URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)).
The `router` object has two properties related to the current route: `path` (string) and `query` (string).

```tsx
'use client';

import { useRouter_UNSTABLE as useRouter } from 'waku';

export const Component = () => {
const { path, searchParams } = useRouter();
const { path, query } = useRouter();

return (
<>
<div>current path: {path}</div>
<div>current searchParams: {searchParams.toString()}</div>
<div>current query: {query}</div>
</>
);
};
Expand Down
36 changes: 8 additions & 28 deletions packages/waku/src/router/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ const normalizeRoutePath = (path: string) => {

const parseRoute = (url: URL): RouteProps => {
if ((globalThis as any).__WAKU_ROUTER_404__) {
return { path: '/404', searchParams: new URLSearchParams() };
return { path: '/404', query: '' };
}
const { pathname, searchParams } = url;
if (searchParams.has(PARAM_KEY_SKIP)) {
console.warn(`The search param "${PARAM_KEY_SKIP}" is reserved`);
}
return { path: normalizeRoutePath(pathname), searchParams };
return { path: normalizeRoutePath(pathname), query: searchParams.toString() };
};

type ChangeRoute = (
Expand Down Expand Up @@ -124,12 +124,6 @@ export function useRouter_UNSTABLE() {
[prefetchRoute],
);
return {
get value() {
console.warn(
'router.value is deprecated. Use router.path and router.searchParams instead.',
);
return route;
},
...route,
push,
replace,
Expand Down Expand Up @@ -259,12 +253,7 @@ const getSkipList = (
if (shouldCheck[0] && route.path !== prevProps.path) {
return false;
}
if (
shouldCheck[1]?.some(
(key) =>
route.searchParams.get(key) !== prevProps.searchParams.get(key),
)
) {
if (shouldCheck[0] && route.query !== prevProps.query) {
return false;
}
return true;
Expand All @@ -275,14 +264,7 @@ const equalRouteProps = (a: RouteProps, b: RouteProps) => {
if (a.path !== b.path) {
return false;
}
if (a.searchParams.size !== b.searchParams.size) {
return false;
}
if (
Array.from(a.searchParams.entries()).some(
([key, val]) => val !== b.searchParams.get(key),
)
) {
if (a.query !== b.query) {
return false;
}
return true;
Expand Down Expand Up @@ -362,7 +344,7 @@ const InnerRouter = ({ routerData }: { routerData: RouterData }) => {
refetch(
input,
new URLSearchParams([
...Array.from(route.searchParams.entries()),
...Array.from(new URLSearchParams(route.query).entries()),
...skip.map((id) => [PARAM_KEY_SKIP, id]),
]),
);
Expand Down Expand Up @@ -396,7 +378,7 @@ const InnerRouter = ({ routerData }: { routerData: RouterData }) => {
}
const input = getInputString(route.path);
const searchParamsString = new URLSearchParams([
...Array.from(route.searchParams.entries()),
...Array.from(new URLSearchParams(route.query).entries()),
...skip.map((id) => [PARAM_KEY_SKIP, id]),
]).toString();
prefetchRSC(input, searchParamsString);
Expand Down Expand Up @@ -470,17 +452,15 @@ const InnerRouter = ({ routerData }: { routerData: RouterData }) => {
// Note: The router data must be a stable mutable object (array).
type RouterData = [
shouldSkip?: ShouldSkip,
locationListners?: Set<
(pathname: string, searchParamsString: string) => void
>,
locationListners?: Set<(path: string, query: string) => void>,
];

const DEFAULT_ROUTER_DATA: RouterData = [];

export function Router({ routerData = DEFAULT_ROUTER_DATA }) {
const route = parseRoute(new URL(window.location.href));
const initialInput = getInputString(route.path);
const initialSearchParamsString = route.searchParams.toString();
const initialSearchParamsString = route.query;
const unstable_onFetchData = (data: unknown) => {
Promise.resolve(data)
.then((data) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/router/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type RouteProps = {
path: string;
searchParams: URLSearchParams;
query: string;
};

export function getComponentIds(path: string): readonly string[] {
Expand Down
4 changes: 2 additions & 2 deletions packages/waku/src/router/define-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { getPathMapping } from '../lib/utils/path.js';
import type { PathSpec } from '../lib/utils/path.js';
import { ServerRouter } from './client.js';

type RoutePropsForLayout = Omit<RouteProps, 'searchParams'> & {
type RoutePropsForLayout = Omit<RouteProps, 'query'> & {
children: ReactNode;
};

Expand Down Expand Up @@ -219,7 +219,7 @@ globalThis.__WAKU_ROUTER_PREFETCH__ = (path) => {
ServerRouter as FunctionComponent<
Omit<ComponentProps<typeof ServerRouter>, 'children'>
>,
{ route: { path: pathname, searchParams } },
{ route: { path: pathname, query: searchParams.toString() } },
componentIds.reduceRight(
(acc: ReactNode, id) => createElement(Slot, { id, fallback: acc }, acc),
null,
Expand Down
Loading