Skip to content

Commit 7b4ad1f

Browse files
authored
chore: Throw a clear error when the LocationProvider is missing (#87)
1 parent 68273b8 commit 7b4ad1f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/router.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ export function Router(props) {
118118
const [c, update] = useReducer(c => c + 1, 0);
119119

120120
const { url, query, wasPush, path } = useLocation();
121+
if (!url) {
122+
throw new Error(`preact-iso's <Router> must be used within a <LocationProvider>, see: https://github.com/preactjs/preact-iso#locationprovider`);
123+
}
121124
const { rest = path, params = {} } = useContext(RouteContext);
122125

123126
const isLoading = useRef(false);

test/router.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ describe('Router', () => {
4242
});
4343

4444

45+
it('should throw a clear error if the LocationProvider is missing', () => {
46+
const Home = () => <h1>Home</h1>;
47+
48+
try {
49+
render(
50+
<Router>
51+
<Home path="/" test="2" />
52+
</Router>,
53+
scratch
54+
);
55+
expect.fail('should have thrown');
56+
} catch (e) {
57+
expect(e.message).to.include('must be used within a <LocationProvider>');
58+
}
59+
});
60+
4561
it('should strip trailing slashes from path', async () => {
4662
render(
4763
<LocationProvider url="/a/">
@@ -953,7 +969,7 @@ describe('Router', () => {
953969
);
954970

955971
shadowlink.click();
956-
972+
957973
await sleep(1);
958974

959975
expect(loc).to.deep.include({ url: '/shadow' });

0 commit comments

Comments
 (0)