Skip to content

Commit d4546a2

Browse files
authored
Merge branch 'main' into route-params-possibility
2 parents 1217389 + df3e39c commit d4546a2

File tree

7 files changed

+62
-22
lines changed

7 files changed

+62
-22
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# preact-iso
22

3+
## 2.0.3
4+
5+
### Patch Changes
6+
7+
- [`62840d8`](https://github.com/preactjs/wmr/commit/62840d82eddf1bda9447c08750fe6886751edee7) [#665](https://github.com/preactjs/wmr/pull/665) Thanks [@rschristian](https://github.com/rschristian)! - Removing unused imports (and associated console warnings) in preact-iso
8+
9+
## 2.0.2
10+
11+
### Patch Changes
12+
13+
- [`c1f3f0e`](https://github.com/preactjs/wmr/commit/c1f3f0e42a053811fe894f91a7bd702247e550d5) [#608](https://github.com/preactjs/wmr/pull/608) Thanks [@marvinhagemeister](https://github.com/marvinhagemeister)! - Fix `useRoute` missing in main entry exports
14+
15+
## 2.0.1
16+
17+
### Patch Changes
18+
19+
- [`79c7fd6`](https://github.com/preactjs/wmr/commit/79c7fd6a5cd46904779a630ac42207e9bf918f1c) [#594](https://github.com/preactjs/wmr/pull/594) Thanks [@developit](https://github.com/developit)! - Fixes a race condition in preact-iso `Router` by registering the link click handler immediately.
20+
321
## 2.0.0
422

523
### Major Changes

hydrate.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { ComponentChild, VNode } from 'preact';
1+
import { ComponentChild } from 'preact';
22

33
export default function hydrate(jsx: ComponentChild, parent?: Element | Document | ShadowRoot | DocumentFragment): void;

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { Router, LocationProvider, useLocation, Route } from './router.js';
1+
export { Router, LocationProvider, useLocation, Route, useRoute } from './router.js';
22
export { default as lazy, ErrorBoundary } from './lazy.js';
33
export { default as hydrate } from './hydrate.js';
44

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "preact-iso",
3-
"version": "2.0.0",
3+
"version": "2.0.3",
44
"description": "Isomorphic utilities for Preact",
55
"main": "./index.js",
66
"module": "./index.js",

router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { h, createContext, cloneElement, toChildArray } from 'preact';
2-
import { useContext, useMemo, useReducer, useEffect, useLayoutEffect, useRef } from 'preact/hooks';
2+
import { useContext, useMemo, useReducer, useLayoutEffect, useRef } from 'preact/hooks';
33

44
let push;
55
const UPDATE = (state, url) => {
@@ -62,7 +62,7 @@ export function LocationProvider(props) {
6262
return { url, path, query: Object.fromEntries(u.searchParams), route, wasPush };
6363
}, [url]);
6464

65-
useEffect(() => {
65+
useLayoutEffect(() => {
6666
addEventListener('click', route);
6767
addEventListener('popstate', route);
6868

test/match.test.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,75 @@
11
import { exec } from '../router.js';
22

3-
function doExec(path, route, opts) {
4-
return exec(path, route, { path, query: {}, params: {}, ...(opts || {}) });
3+
function execPath(path, pattern, opts) {
4+
return exec(path, pattern, { path, query: {}, params: {}, ...(opts || {}) });
55
}
66

77
describe('match', () => {
88
it('Base route', () => {
9-
const accurateResult = doExec('/', '/');
9+
const accurateResult = execPath('/', '/');
1010
expect(accurateResult).toEqual({ path: '/', params: {}, query: {} });
1111

12-
const inaccurateResult = doExec('/user/1', '/');
12+
const inaccurateResult = execPath('/user/1', '/');
1313
expect(inaccurateResult).toEqual(undefined);
1414
});
1515

1616
it('Param route', () => {
17-
const accurateResult = doExec('/user/2', '/user/:id');
18-
expect(accurateResult).toEqual({ path: '/user/2', params: { id: '2' }, query: {}, id: '2' });
17+
const accurateResult = execPath('/user/2', '/user/:id');
18+
expect(accurateResult).toEqual({ path: '/user/2', params: { id: '2' }, id: '2', query: {} });
1919

20-
const inaccurateResult = doExec('/', '/user/:id');
20+
const inaccurateResult = execPath('/', '/user/:id');
2121
expect(inaccurateResult).toEqual(undefined);
2222
});
2323

2424
it('Optional param route', () => {
25-
const accurateResult = doExec('/user', '/user/:id?');
26-
expect(accurateResult).toEqual({ path: '/user', params: { id: undefined }, query: {} });
25+
const accurateResult = execPath('/user', '/user/:id?');
26+
expect(accurateResult).toEqual({ path: '/user', params: { id: undefined }, id: undefined, query: {} });
2727

28-
const inaccurateResult = doExec('/', '/user/:id?');
28+
const inaccurateResult = execPath('/', '/user/:id?');
2929
expect(inaccurateResult).toEqual(undefined);
3030
});
3131

32+
it('Optional rest param route "/:x*"', () => {
33+
const accurateResult = execPath('/user', '/user/:id?');
34+
expect(accurateResult).toEqual({ path: '/user', params: { id: undefined }, id: undefined, query: {} });
35+
36+
const inaccurateResult = execPath('/', '/user/:id?');
37+
expect(inaccurateResult).toEqual(undefined);
38+
});
39+
40+
it('Rest param route "/:x+"', () => {
41+
const matchedResult = execPath('/user/foo', '/user/:id+');
42+
expect(matchedResult).toEqual({ path: '/user/foo', params: { id: 'foo' } });
43+
44+
const matchedResultWithSlash = execPath('/user/foo/bar', '/user/:id+');
45+
expect(matchedResultWithSlash).toEqual({ path: '/user/foo/bar', params: { id: 'foo/bar' }, id: 'foo/bar' });
46+
47+
const emptyResult = execPath('/user', '/user/:id+');
48+
expect(emptyResult).toEqual(undefined);
49+
50+
const mismatchedResult = execPath('/', '/user/:id+');
51+
expect(mismatchedResult).toEqual(undefined);
52+
});
53+
3254
it('Handles leading/trailing slashes', () => {
33-
const result = doExec('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/');
55+
const result = execPath('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/');
3456
expect(result).toEqual({
57+
path: '/about-late/_SEGMENT1_/_SEGMENT2_/',
3558
params: {
3659
seg1: '_SEGMENT1_',
3760
seg2: '_SEGMENT2_'
3861
},
39-
path: '/about-late/_SEGMENT1_/_SEGMENT2_/',
40-
query: {},
4162
seg1: '_SEGMENT1_',
42-
seg2: '_SEGMENT2_'
63+
seg2: '_SEGMENT2_',
64+
query: {},
4365
});
4466
});
4567

4668
it('should not overwrite existing properties', () => {
47-
const result = doExec('/foo/bar', '/:path/:query');
69+
const result = execPath('/foo/bar', '/:path/:query', { path: '/custom-path' });
4870
expect(result).toEqual({
4971
params: { path: 'foo', query: 'bar' },
50-
path: '/foo/bar',
72+
path: '/custom-path',
5173
query: {}
5274
});
5375
});

test/router.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ describe('Router', () => {
429429
await sleep(20);
430430

431431
scratch.querySelector('a[href="/foo#foo"]').click();
432-
await sleep(100);
432+
await sleep(20);
433433
expect(Route).toHaveBeenCalledTimes(1);
434434
expect(loc).toMatchObject({ url: '/foo#foo', path: '/foo' });
435435
expect(pushState).toHaveBeenCalled();

0 commit comments

Comments
 (0)