next-use-contextual-routing 1.0.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -18
- package/dist/index.cjs.js +25 -22
- package/dist/index.esm.js +19 -21
- package/package.json +19 -11
- package/src/__tests__/index.test-d.ts +10 -0
- package/src/__tests__/index.test.js +131 -0
- package/src/index.d.ts +3 -1
- package/src/index.js +26 -19
package/README.md
CHANGED
|
@@ -3,8 +3,11 @@
|
|
|
3
3
|
[![Build status][ci-badge]][ci]
|
|
4
4
|
[![Test coverage report][coveralls-badge]][coveralls]
|
|
5
5
|
[![Npm version][npm-badge]][npm]
|
|
6
|
+
[![Bundle size][bundlephobia-badge]][bundlephobia]
|
|
6
7
|
|
|
7
|
-
React hook that generates round trip hrefs
|
|
8
|
+
React hook that generates round trip hrefs to enable [contextual routing][next-docs-contextual-routing] for [Next.js][next]. Take a look to the [demo 👈][demo].
|
|
9
|
+
|
|
10
|
+
[](https://www.youtube.com/watch?v=4NR2uOBsIu4)
|
|
8
11
|
|
|
9
12
|
### With Next links
|
|
10
13
|
|
|
@@ -14,12 +17,16 @@ import Link from 'next/link';
|
|
|
14
17
|
import { useContextualRouting } from 'next-use-contextual-routing';
|
|
15
18
|
|
|
16
19
|
function MyPage() {
|
|
17
|
-
const {
|
|
20
|
+
const { makeContextualHref, returnHref } = useContextualRouting();
|
|
18
21
|
|
|
19
22
|
return (
|
|
20
23
|
<ul>
|
|
21
24
|
<li>
|
|
22
|
-
<Link
|
|
25
|
+
<Link
|
|
26
|
+
href={makeContextualHref({ id: 33 })}
|
|
27
|
+
as="/route-to-visit-contextually"
|
|
28
|
+
shallow
|
|
29
|
+
>
|
|
23
30
|
Start contextual routing
|
|
24
31
|
</Link>
|
|
25
32
|
</li>
|
|
@@ -36,23 +43,25 @@ function MyPage() {
|
|
|
36
43
|
### With Next router
|
|
37
44
|
|
|
38
45
|
```js
|
|
39
|
-
import React
|
|
40
|
-
import { useRouter } from 'next/
|
|
46
|
+
import React from 'react';
|
|
47
|
+
import { useRouter } from 'next/router';
|
|
41
48
|
import { useContextualRouting } from 'next-use-contextual-routing';
|
|
42
49
|
|
|
43
50
|
function MyPage() {
|
|
44
51
|
const router = useRouter();
|
|
45
|
-
const {
|
|
46
|
-
|
|
47
|
-
const openModal =
|
|
48
|
-
router.push(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
const { makeContextualHref, returnHref } = useContextualRouting();
|
|
53
|
+
|
|
54
|
+
const openModal = () =>
|
|
55
|
+
router.push(
|
|
56
|
+
makeContextualHref({ id: 33 }),
|
|
57
|
+
'/route-to-visit-contextually',
|
|
58
|
+
{
|
|
59
|
+
shallow: true,
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const closeModal = () =>
|
|
54
64
|
router.push(returnHref, undefined, { shallow: true });
|
|
55
|
-
}, [router, returnHref]);
|
|
56
65
|
|
|
57
66
|
return (
|
|
58
67
|
<ul>
|
|
@@ -60,21 +69,58 @@ function MyPage() {
|
|
|
60
69
|
<button onClick={openModal}>Open contextual modal</button>
|
|
61
70
|
</li>
|
|
62
71
|
<li>
|
|
63
|
-
<button onClick={
|
|
72
|
+
<button onClick={closeModal}>Close contextual modal</button>
|
|
64
73
|
</li>
|
|
65
74
|
</ul>
|
|
66
75
|
);
|
|
67
76
|
}
|
|
68
77
|
```
|
|
69
78
|
|
|
79
|
+
## Why?
|
|
80
|
+
|
|
81
|
+
Contextual routing presents 2 challenges:
|
|
82
|
+
|
|
83
|
+
- Persist a href string able to keep the initial page state consistent throughout the whole contextual navigation
|
|
84
|
+
- Persist the url to return to when contextual routing is terminated
|
|
85
|
+
|
|
86
|
+
**Next use contextual routing** abstracts these responsibilities as a React hook.
|
|
87
|
+
|
|
88
|
+
It also works in statically exported apps and supports navigation with browsers' back button.
|
|
89
|
+
|
|
90
|
+
## API
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
const { makeContextualHref, returnHref } = useContextualRouting();
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
#### makeContextualHref
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
const makeContextualHref: (extraQueryParams: { [key: string]: any }) => string;
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The function returns the path to provide as `href` to start or keep alive contextual navigation. The generated path describes the state of the page to keep alive while contextual navigation is active.
|
|
103
|
+
|
|
104
|
+
It optionally accepts an object describing **extra parameters** to append to contextual navigation `href`.
|
|
105
|
+
|
|
106
|
+
#### returnHref
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
const returnHref: string;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The path to return to to close contextual navigation.
|
|
113
|
+
|
|
70
114
|
## Notes
|
|
71
115
|
|
|
72
116
|
### Naming
|
|
73
117
|
|
|
74
118
|
**Contextual routing** pattern is also known as:
|
|
75
119
|
|
|
120
|
+
- Modal route
|
|
76
121
|
- [Route as modal][next-docs-contextual-routing]
|
|
77
122
|
- [Snowlift][twitter-pattern-name]
|
|
123
|
+
- Instagram style modal routing
|
|
78
124
|
|
|
79
125
|
### Scroll behaviour on refresh
|
|
80
126
|
|
|
@@ -84,12 +130,16 @@ When using **Contextual routing**, default browsers scroll behaviour on page ref
|
|
|
84
130
|
history.scrollRestoration = 'manual';
|
|
85
131
|
```
|
|
86
132
|
|
|
87
|
-
[ci]: https://
|
|
88
|
-
[ci
|
|
133
|
+
[ci-badge]: https://github.com/toomuchdesign/next-use-contextual-routing/actions/workflows/ci.yml/badge.svg
|
|
134
|
+
[ci]: https://github.com/toomuchdesign/next-use-contextual-routing/actions/workflows/ci.yml
|
|
89
135
|
[npm]: https://www.npmjs.com/package/next-use-contextual-routing
|
|
90
136
|
[npm-badge]: https://img.shields.io/npm/v/next-use-contextual-routing.svg
|
|
91
137
|
[coveralls-badge]: https://coveralls.io/repos/github/toomuchdesign/next-use-contextual-routing/badge.svg?branch=master
|
|
92
138
|
[coveralls]: https://coveralls.io/github/toomuchdesign/next-use-contextual-routing?branch=master
|
|
139
|
+
[next]: https://nextjs.org/
|
|
93
140
|
[next-docs-contextual-routing]: https://github.com/vercel/next.js/tree/v9.5.3/examples/with-route-as-modal
|
|
94
141
|
[twitter-pattern-name]: https://twitter.com/swyx/status/1245891685927682049
|
|
95
142
|
[history-scroll-restoration-docs]: https://developer.mozilla.org/en-US/docs/Web/API/History/scrollRestoration
|
|
143
|
+
[demo]: https://toomuchdesign.github.io/next-use-contextual-routing/
|
|
144
|
+
[bundlephobia-badge]: https://badgen.net/bundlephobia/minzip/next-use-contextual-routing
|
|
145
|
+
[bundlephobia]: https://bundlephobia.com/result?p=next-use-contextual-routing
|
package/dist/index.cjs.js
CHANGED
|
@@ -4,8 +4,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var react = require('react');
|
|
6
6
|
var router = require('next/router');
|
|
7
|
-
var
|
|
7
|
+
var stringify = require('qs-stringify');
|
|
8
8
|
|
|
9
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
10
|
+
|
|
11
|
+
var stringify__default = /*#__PURE__*/_interopDefaultLegacy(stringify);
|
|
12
|
+
|
|
13
|
+
var RETURN_HREF_QUERY_PARAM = '_UCR_return_href';
|
|
9
14
|
/**
|
|
10
15
|
* During contextual routing browser URL will be controlled by Next Router's "as" prop
|
|
11
16
|
* while the page actually rendered is defined by Next Router's "href" prop.
|
|
@@ -14,34 +19,32 @@ var queryString = require('query-string');
|
|
|
14
19
|
* router.asPath: /item/3 reflects current URL and updates at each page change
|
|
15
20
|
* router.pathname: /search/[terms] stay the same as long as initial page doesn't change
|
|
16
21
|
* router.query: {"terms": "foo-bar"} same as above
|
|
17
|
-
*
|
|
18
|
-
* {Object} result
|
|
19
|
-
* {string} result.returnHref path where contextual routing started from
|
|
20
|
-
* {string} result.contextualHref href value to needed to start a contextual routing session (along with an "as" prop)
|
|
21
|
-
* @returns result
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
function useContextualRouting() {
|
|
25
25
|
var router$1 = router.useRouter();
|
|
26
|
-
|
|
27
|
-
var
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
var
|
|
38
|
-
react.
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
var returnHrefQueryParam = router$1.query[RETURN_HREF_QUERY_PARAM];
|
|
27
|
+
var watchedQuery = Object.assign({}, router$1.query);
|
|
28
|
+
delete watchedQuery[RETURN_HREF_QUERY_PARAM];
|
|
29
|
+
/*
|
|
30
|
+
* After a page refresh there is no RETURN_HREF_QUERY_PARAM in router.query
|
|
31
|
+
* RETURN_HREF_QUERY_PARAM is only available in those history entries where
|
|
32
|
+
* contextual navigation is enabled (or WAS enabled in case history.back() is triggered)
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
var returnHref = returnHrefQueryParam != null ? returnHrefQueryParam : router$1.asPath; // @NOTE JSON.stringify might be replaced with any hashing solution
|
|
36
|
+
|
|
37
|
+
var queryHash = JSON.stringify(watchedQuery);
|
|
38
|
+
var makeContextualHref = react.useCallback(function (extraParams) {
|
|
39
|
+
var _Object$assign;
|
|
40
|
+
|
|
41
|
+
return router$1.pathname + '?' + stringify__default['default'](Object.assign({}, router$1.query, extraParams, (_Object$assign = {}, _Object$assign[RETURN_HREF_QUERY_PARAM] = returnHref, _Object$assign)));
|
|
42
|
+
}, [queryHash, returnHref]);
|
|
41
43
|
return {
|
|
42
44
|
returnHref: returnHref,
|
|
43
|
-
|
|
45
|
+
makeContextualHref: makeContextualHref
|
|
44
46
|
};
|
|
45
47
|
}
|
|
46
48
|
|
|
49
|
+
exports.RETURN_HREF_QUERY_PARAM = RETURN_HREF_QUERY_PARAM;
|
|
47
50
|
exports.useContextualRouting = useContextualRouting;
|
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
2
|
import { useRouter } from 'next/router';
|
|
3
|
-
import
|
|
3
|
+
import stringify from 'qs-stringify';
|
|
4
4
|
|
|
5
|
+
var RETURN_HREF_QUERY_PARAM = '_UCR_return_href';
|
|
5
6
|
/**
|
|
6
7
|
* During contextual routing browser URL will be controlled by Next Router's "as" prop
|
|
7
8
|
* while the page actually rendered is defined by Next Router's "href" prop.
|
|
@@ -10,34 +11,31 @@ import { stringify } from 'query-string';
|
|
|
10
11
|
* router.asPath: /item/3 reflects current URL and updates at each page change
|
|
11
12
|
* router.pathname: /search/[terms] stay the same as long as initial page doesn't change
|
|
12
13
|
* router.query: {"terms": "foo-bar"} same as above
|
|
13
|
-
*
|
|
14
|
-
* {Object} result
|
|
15
|
-
* {string} result.returnHref path where contextual routing started from
|
|
16
|
-
* {string} result.contextualHref href value to needed to start a contextual routing session (along with an "as" prop)
|
|
17
|
-
* @returns result
|
|
18
14
|
*/
|
|
19
15
|
|
|
20
16
|
function useContextualRouting() {
|
|
21
17
|
var router = useRouter();
|
|
18
|
+
var returnHrefQueryParam = router.query[RETURN_HREF_QUERY_PARAM];
|
|
19
|
+
var watchedQuery = Object.assign({}, router.query);
|
|
20
|
+
delete watchedQuery[RETURN_HREF_QUERY_PARAM];
|
|
21
|
+
/*
|
|
22
|
+
* After a page refresh there is no RETURN_HREF_QUERY_PARAM in router.query
|
|
23
|
+
* RETURN_HREF_QUERY_PARAM is only available in those history entries where
|
|
24
|
+
* contextual navigation is enabled (or WAS enabled in case history.back() is triggered)
|
|
25
|
+
*/
|
|
22
26
|
|
|
23
|
-
var
|
|
24
|
-
returnHref = _useState[0],
|
|
25
|
-
setReturnHref = _useState[1]; // @NOTE JSON.stringify might be replaced with any hashing solution
|
|
27
|
+
var returnHref = returnHrefQueryParam != null ? returnHrefQueryParam : router.asPath; // @NOTE JSON.stringify might be replaced with any hashing solution
|
|
26
28
|
|
|
29
|
+
var queryHash = JSON.stringify(watchedQuery);
|
|
30
|
+
var makeContextualHref = useCallback(function (extraParams) {
|
|
31
|
+
var _Object$assign;
|
|
27
32
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
var queryAsString = stringify(router.query);
|
|
31
|
-
return queryAsString ? "?".concat(queryAsString) : '';
|
|
32
|
-
}, [querySignature]);
|
|
33
|
-
var contextualHref = router.pathname + queryString;
|
|
34
|
-
useEffect(function () {
|
|
35
|
-
setReturnHref(router.asPath);
|
|
36
|
-
}, [querySignature]);
|
|
33
|
+
return router.pathname + '?' + stringify(Object.assign({}, router.query, extraParams, (_Object$assign = {}, _Object$assign[RETURN_HREF_QUERY_PARAM] = returnHref, _Object$assign)));
|
|
34
|
+
}, [queryHash, returnHref]);
|
|
37
35
|
return {
|
|
38
36
|
returnHref: returnHref,
|
|
39
|
-
|
|
37
|
+
makeContextualHref: makeContextualHref
|
|
40
38
|
};
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
export { useContextualRouting };
|
|
41
|
+
export { RETURN_HREF_QUERY_PARAM, useContextualRouting };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-use-contextual-routing",
|
|
3
3
|
"description": "Generate Next.js contextual routing paths",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.3.0",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
7
7
|
"types": "src/index.d.ts",
|
|
@@ -21,7 +21,10 @@
|
|
|
21
21
|
"test:source": "npm run test:source:ts && npm t -- --coverage",
|
|
22
22
|
"test:source:ts": "tsd",
|
|
23
23
|
"test:bundles": "es-check es5 --module './@(dist)/*.js'",
|
|
24
|
-
"format": "prettier --write \"**/*.{js,json,md}\""
|
|
24
|
+
"format": "prettier --write \"**/*.{js,json,md}\"",
|
|
25
|
+
"demo:start": "cd ./demo && next",
|
|
26
|
+
"demo:compile": "cd ./demo && next build && next export",
|
|
27
|
+
"demo:deploy": "npm run demo:compile && touch demo/out/.nojekyll && gh-pages -d demo/out --dotfiles"
|
|
25
28
|
},
|
|
26
29
|
"keywords": [
|
|
27
30
|
"next",
|
|
@@ -30,6 +33,7 @@
|
|
|
30
33
|
"routing",
|
|
31
34
|
"snowlift",
|
|
32
35
|
"route as modal",
|
|
36
|
+
"modal route",
|
|
33
37
|
"hook"
|
|
34
38
|
],
|
|
35
39
|
"repository": {
|
|
@@ -39,27 +43,31 @@
|
|
|
39
43
|
"author": "Andrea Carraro <me@andreacarraro.it>",
|
|
40
44
|
"license": "ISC",
|
|
41
45
|
"dependencies": {
|
|
42
|
-
"
|
|
46
|
+
"qs-stringify": "^1.2.1"
|
|
43
47
|
},
|
|
44
48
|
"devDependencies": {
|
|
45
|
-
"@babel/core": "^7.
|
|
49
|
+
"@babel/core": "^7.14.6",
|
|
50
|
+
"@babel/plugin-transform-react-jsx": "^7.14.5",
|
|
51
|
+
"@babel/preset-env": "^7.14.7",
|
|
46
52
|
"@rollup/plugin-babel": "^5.2.1",
|
|
47
|
-
"@testing-library/react-hooks": "^
|
|
48
|
-
"coveralls": "^3.1.0",
|
|
53
|
+
"@testing-library/react-hooks": "^7.0.1",
|
|
49
54
|
"es-check": "^5.1.1",
|
|
55
|
+
"gh-pages": "^3.1.0",
|
|
50
56
|
"husky": "^4.0.0",
|
|
51
57
|
"jest": "^26.4.2",
|
|
52
58
|
"lint-staged": "^10.0.0",
|
|
53
|
-
"next": "^
|
|
59
|
+
"next": "^12.1.0",
|
|
54
60
|
"prettier": "^2.0.0",
|
|
55
|
-
"react": "^
|
|
56
|
-
"react-
|
|
61
|
+
"react": "^17.0.2",
|
|
62
|
+
"react-dom": "^17.0.2",
|
|
63
|
+
"react-modal": "^3.14.3",
|
|
64
|
+
"react-test-renderer": "^17.0.2",
|
|
57
65
|
"rollup": "^2.28.2",
|
|
58
66
|
"tsd": "^0.13.1"
|
|
59
67
|
},
|
|
60
68
|
"peerDependencies": {
|
|
61
|
-
"next": "^9.0.0",
|
|
62
|
-
"react": "
|
|
69
|
+
"next": "^9.0.0 || ^10.0.0 || ^11.0.0 || ^12.0.0",
|
|
70
|
+
"react": ">=16.8.0"
|
|
63
71
|
},
|
|
64
72
|
"jest": {
|
|
65
73
|
"testPathIgnorePatterns": [
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { expectType, expectAssignable, expectError } from 'tsd';
|
|
2
|
+
import { useContextualRouting } from '../';
|
|
3
|
+
|
|
4
|
+
const { returnHref, makeContextualHref } = useContextualRouting();
|
|
5
|
+
|
|
6
|
+
expectType<string>(returnHref);
|
|
7
|
+
expectAssignable<Function>(makeContextualHref);
|
|
8
|
+
|
|
9
|
+
expectError(makeContextualHref('s'));
|
|
10
|
+
expectType<string>(makeContextualHref({ foo: 'bar' }));
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { RouterContext } from 'next/dist/shared/lib/router-context';
|
|
3
|
+
import { renderHook as TLRenderHook } from '@testing-library/react-hooks';
|
|
4
|
+
import stringify from 'qs-stringify';
|
|
5
|
+
import { useContextualRouting, RETURN_HREF_QUERY_PARAM } from '../index';
|
|
6
|
+
|
|
7
|
+
const mockRouter = {
|
|
8
|
+
basePath: '',
|
|
9
|
+
pathname: '/',
|
|
10
|
+
route: '/',
|
|
11
|
+
asPath: '/',
|
|
12
|
+
query: {},
|
|
13
|
+
push: jest.fn(),
|
|
14
|
+
replace: jest.fn(),
|
|
15
|
+
reload: jest.fn(),
|
|
16
|
+
back: jest.fn(),
|
|
17
|
+
prefetch: jest.fn(),
|
|
18
|
+
beforePopState: jest.fn(),
|
|
19
|
+
events: {
|
|
20
|
+
on: jest.fn(),
|
|
21
|
+
off: jest.fn(),
|
|
22
|
+
emit: jest.fn(),
|
|
23
|
+
},
|
|
24
|
+
isFallback: false,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function contextProvider({ router, children }) {
|
|
28
|
+
return (
|
|
29
|
+
<RouterContext.Provider value={{ ...mockRouter, ...router }}>
|
|
30
|
+
{children}
|
|
31
|
+
</RouterContext.Provider>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function renderHook({ router }) {
|
|
36
|
+
return TLRenderHook(() => useContextualRouting(), {
|
|
37
|
+
wrapper: ({ children }) => contextProvider({ router, children }),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('useContextualRouting', () => {
|
|
42
|
+
const initialRouter = {
|
|
43
|
+
asPath: '/startpage/55?page=2#anchor',
|
|
44
|
+
pathname: '/startpage/[id]',
|
|
45
|
+
query: {
|
|
46
|
+
id: 55,
|
|
47
|
+
page: 2,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
it('returns hrefs relative to the route where contextual routing starts from', () => {
|
|
52
|
+
const { result } = renderHook({ router: initialRouter });
|
|
53
|
+
expect(result.current.returnHref).toBe('/startpage/55?page=2#anchor');
|
|
54
|
+
expect(result.current.makeContextualHref({ extraParam: 'foo' })).toBe(
|
|
55
|
+
'/startpage/[id]?' +
|
|
56
|
+
stringify({
|
|
57
|
+
id: 55,
|
|
58
|
+
page: 2,
|
|
59
|
+
extraParam: 'foo',
|
|
60
|
+
[RETURN_HREF_QUERY_PARAM]: '/startpage/55?page=2#anchor',
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('returns expected hrefs when query object is empty', () => {
|
|
66
|
+
const router = {
|
|
67
|
+
...initialRouter,
|
|
68
|
+
asPath: '/startpage',
|
|
69
|
+
query: {},
|
|
70
|
+
};
|
|
71
|
+
const { result } = renderHook({ router });
|
|
72
|
+
expect(result.current.returnHref).toBe('/startpage');
|
|
73
|
+
expect(result.current.makeContextualHref()).toBe(
|
|
74
|
+
'/startpage/[id]?' +
|
|
75
|
+
stringify({
|
|
76
|
+
[RETURN_HREF_QUERY_PARAM]: '/startpage',
|
|
77
|
+
})
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('Contextual routing starts', () => {
|
|
82
|
+
it('preserves hrefs pointing to the page where contextual routing started from', () => {
|
|
83
|
+
const router = { ...initialRouter };
|
|
84
|
+
const { result, rerender } = renderHook({ router });
|
|
85
|
+
|
|
86
|
+
// Simulate route change happening when contextual routing starts
|
|
87
|
+
// @NOTE It seems that rerender accepts only newProp (new initial props) so here we mutate the original router object :(
|
|
88
|
+
// https://github.com/testing-library/react-hooks-testing-library/issues/228
|
|
89
|
+
router.asPath = '/post/5';
|
|
90
|
+
router.query[RETURN_HREF_QUERY_PARAM] = result.current.returnHref;
|
|
91
|
+
rerender();
|
|
92
|
+
|
|
93
|
+
expect(result.current.returnHref).toBe('/startpage/55?page=2#anchor');
|
|
94
|
+
expect(result.current.makeContextualHref()).toBe(
|
|
95
|
+
'/startpage/[id]?' +
|
|
96
|
+
stringify({
|
|
97
|
+
id: 55,
|
|
98
|
+
page: 2,
|
|
99
|
+
[RETURN_HREF_QUERY_PARAM]: '/startpage/55?page=2#anchor',
|
|
100
|
+
})
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('Route change before contextual routing has started', () => {
|
|
106
|
+
it('updates returned values to reflect query updates', () => {
|
|
107
|
+
const router = { ...initialRouter };
|
|
108
|
+
const { result, rerender } = renderHook({ router });
|
|
109
|
+
|
|
110
|
+
// Route change 2: back to startpage with different query
|
|
111
|
+
router.asPath = '/startpage/55?page=3#anchor';
|
|
112
|
+
router.pathname = '/startpage/[id]';
|
|
113
|
+
router.query = {
|
|
114
|
+
id: 55,
|
|
115
|
+
page: 3,
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
rerender();
|
|
119
|
+
|
|
120
|
+
expect(result.current.returnHref).toBe('/startpage/55?page=3#anchor');
|
|
121
|
+
expect(result.current.makeContextualHref()).toBe(
|
|
122
|
+
'/startpage/[id]?' +
|
|
123
|
+
stringify({
|
|
124
|
+
id: 55,
|
|
125
|
+
page: 3,
|
|
126
|
+
[RETURN_HREF_QUERY_PARAM]: '/startpage/55?page=3#anchor',
|
|
127
|
+
})
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
});
|
package/src/index.d.ts
CHANGED
package/src/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
2
|
import { useRouter } from 'next/router';
|
|
3
|
-
import
|
|
3
|
+
import stringify from 'qs-stringify';
|
|
4
|
+
export const RETURN_HREF_QUERY_PARAM = '_UCR_return_href';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* During contextual routing browser URL will be controlled by Next Router's "as" prop
|
|
@@ -10,26 +11,32 @@ import { stringify } from 'query-string';
|
|
|
10
11
|
* router.asPath: /item/3 reflects current URL and updates at each page change
|
|
11
12
|
* router.pathname: /search/[terms] stay the same as long as initial page doesn't change
|
|
12
13
|
* router.query: {"terms": "foo-bar"} same as above
|
|
13
|
-
*
|
|
14
|
-
* {Object} result
|
|
15
|
-
* {string} result.returnHref path where contextual routing started from
|
|
16
|
-
* {string} result.contextualHref href value to needed to start a contextual routing session (along with an "as" prop)
|
|
17
|
-
* @returns result
|
|
18
14
|
*/
|
|
19
15
|
export function useContextualRouting() {
|
|
20
16
|
const router = useRouter();
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const queryString = useMemo(() => {
|
|
25
|
-
const queryAsString = stringify(router.query);
|
|
26
|
-
return queryAsString ? `?${queryAsString}` : '';
|
|
27
|
-
}, [querySignature]);
|
|
28
|
-
const contextualHref = router.pathname + queryString;
|
|
17
|
+
const returnHrefQueryParam = router.query[RETURN_HREF_QUERY_PARAM];
|
|
18
|
+
const watchedQuery = Object.assign({}, router.query);
|
|
19
|
+
delete watchedQuery[RETURN_HREF_QUERY_PARAM];
|
|
29
20
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
21
|
+
/*
|
|
22
|
+
* After a page refresh there is no RETURN_HREF_QUERY_PARAM in router.query
|
|
23
|
+
* RETURN_HREF_QUERY_PARAM is only available in those history entries where
|
|
24
|
+
* contextual navigation is enabled (or WAS enabled in case history.back() is triggered)
|
|
25
|
+
*/
|
|
26
|
+
const returnHref = returnHrefQueryParam ?? router.asPath;
|
|
27
|
+
// @NOTE JSON.stringify might be replaced with any hashing solution
|
|
28
|
+
const queryHash = JSON.stringify(watchedQuery);
|
|
29
|
+
const makeContextualHref = useCallback(
|
|
30
|
+
(extraParams) =>
|
|
31
|
+
router.pathname +
|
|
32
|
+
'?' +
|
|
33
|
+
stringify(
|
|
34
|
+
Object.assign({}, router.query, extraParams, {
|
|
35
|
+
[RETURN_HREF_QUERY_PARAM]: returnHref,
|
|
36
|
+
})
|
|
37
|
+
),
|
|
38
|
+
[queryHash, returnHref]
|
|
39
|
+
);
|
|
33
40
|
|
|
34
|
-
return { returnHref,
|
|
41
|
+
return { returnHref, makeContextualHref };
|
|
35
42
|
}
|