next-use-contextual-routing 2.3.0 → 3.0.1

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 CHANGED
@@ -96,7 +96,9 @@ const { makeContextualHref, returnHref } = useContextualRouting();
96
96
  #### makeContextualHref
97
97
 
98
98
  ```ts
99
- const makeContextualHref: (extraQueryParams: { [key: string]: any }) => string;
99
+ const makeContextualHref: (
100
+ extraParams?: Record<string, string | number>
101
+ ) => string;
100
102
  ```
101
103
 
102
104
  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.
@@ -111,6 +113,13 @@ const returnHref: string;
111
113
 
112
114
  The path to return to to close contextual navigation.
113
115
 
116
+ ## Next.js versions support
117
+
118
+ | Next use contextual routing | Next.js |
119
+ | --------------------------- | ------------- |
120
+ | v2.2.0 | v9.X -> v12.X |
121
+ | v3.0.0 + | v13.X |
122
+
114
123
  ## Notes
115
124
 
116
125
  ### Naming
@@ -130,6 +139,10 @@ When using **Contextual routing**, default browsers scroll behaviour on page ref
130
139
  history.scrollRestoration = 'manual';
131
140
  ```
132
141
 
142
+ ### Devnote: deploy Next.js on GitHub pages
143
+
144
+ https://www.viget.com/articles/host-build-and-deploy-next-js-projects-on-github-pages
145
+
133
146
  [ci-badge]: https://github.com/toomuchdesign/next-use-contextual-routing/actions/workflows/ci.yml/badge.svg
134
147
  [ci]: https://github.com/toomuchdesign/next-use-contextual-routing/actions/workflows/ci.yml
135
148
  [npm]: https://www.npmjs.com/package/next-use-contextual-routing
@@ -0,0 +1,14 @@
1
+ export declare const RETURN_HREF_QUERY_PARAM = "_UCR_return_href";
2
+ /**
3
+ * During contextual routing browser URL will be controlled by Next Router's "as" prop
4
+ * while the page actually rendered is defined by Next Router's "href" prop.
5
+ *
6
+ * During contextual navigation Next Router's behaves as follows:
7
+ * router.asPath: /item/3 reflects current URL and updates at each page change
8
+ * router.pathname: /search/[terms] stay the same as long as initial page doesn't change
9
+ * router.query: {"terms": "foo-bar"} same as above
10
+ */
11
+ export declare function useContextualRouting(): {
12
+ returnHref: string;
13
+ makeContextualHref: (extraParams?: Record<string, string | number>) => string;
14
+ };
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ import { useCallback } from 'react';
2
+ import { useRouter } from 'next/router.js';
3
+ import stringify from 'qs-stringify';
4
+ export var RETURN_HREF_QUERY_PARAM = '_UCR_return_href';
5
+ /**
6
+ * During contextual routing browser URL will be controlled by Next Router's "as" prop
7
+ * while the page actually rendered is defined by Next Router's "href" prop.
8
+ *
9
+ * During contextual navigation Next Router's behaves as follows:
10
+ * router.asPath: /item/3 reflects current URL and updates at each page change
11
+ * router.pathname: /search/[terms] stay the same as long as initial page doesn't change
12
+ * router.query: {"terms": "foo-bar"} same as above
13
+ */
14
+ export function useContextualRouting() {
15
+ var router = useRouter();
16
+ var returnHrefQueryParam = typeof router.query[RETURN_HREF_QUERY_PARAM] === 'string'
17
+ ? router.query[RETURN_HREF_QUERY_PARAM]
18
+ : undefined;
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
+ */
26
+ var returnHref = returnHrefQueryParam !== null && returnHrefQueryParam !== void 0 ? returnHrefQueryParam : router.asPath;
27
+ // @NOTE JSON.stringify might be replaced with any hashing solution
28
+ var queryHash = JSON.stringify(watchedQuery);
29
+ var makeContextualHref = useCallback(function (extraParams) {
30
+ var _a;
31
+ return router.pathname +
32
+ '?' +
33
+ stringify(Object.assign({}, router.query, extraParams, (_a = {},
34
+ _a[RETURN_HREF_QUERY_PARAM] = returnHref,
35
+ _a)));
36
+ }, [queryHash, returnHref]);
37
+ return { returnHref: returnHref, makeContextualHref: makeContextualHref };
38
+ }
package/package.json CHANGED
@@ -1,30 +1,29 @@
1
1
  {
2
2
  "name": "next-use-contextual-routing",
3
3
  "description": "Generate Next.js contextual routing paths",
4
- "version": "2.3.0",
5
- "main": "dist/index.cjs.js",
6
- "module": "dist/index.esm.js",
7
- "types": "src/index.d.ts",
4
+ "version": "3.0.1",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "type": "module",
8
9
  "files": [
9
- "dist",
10
- "src",
11
- "!__tests__"
10
+ "dist"
12
11
  ],
13
12
  "scripts": {
14
13
  "clean": "rm -rf dist",
15
- "compile": "npm run clean && rollup -c",
14
+ "build": "npm run clean && tsc -p tsconfig.build.json",
16
15
  "preversion": "npm run prepare",
17
16
  "version": "git add package.json",
18
17
  "postversion": "git push && git push --tags",
19
- "prepare": "npm run test:source && npm run compile && npm run test:bundles",
18
+ "prepare": "npm run test:source && npm run build && npm run test:bundles",
20
19
  "test": "jest",
21
- "test:source": "npm run test:source:ts && npm t -- --coverage",
22
- "test:source:ts": "tsd",
20
+ "test:source": "npm t -- --coverage",
23
21
  "test:bundles": "es-check es5 --module './@(dist)/*.js'",
24
22
  "format": "prettier --write \"**/*.{js,json,md}\"",
23
+ "start": "npm run demo:start",
25
24
  "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
+ "demo:build": "npm run build && cd ./demo && BUILD_TARGET=github-pages next build && BUILD_TARGET=github-pages next export",
26
+ "demo:deploy": "npm run demo:build && touch demo/out/.nojekyll && gh-pages -d demo/out --dotfiles"
28
27
  },
29
28
  "keywords": [
30
29
  "next",
@@ -46,30 +45,30 @@
46
45
  "qs-stringify": "^1.2.1"
47
46
  },
48
47
  "devDependencies": {
49
- "@babel/core": "^7.14.6",
50
- "@babel/plugin-transform-react-jsx": "^7.14.5",
51
- "@babel/preset-env": "^7.14.7",
52
- "@rollup/plugin-babel": "^5.2.1",
53
- "@testing-library/react-hooks": "^7.0.1",
48
+ "@testing-library/react": "^13.4.0",
49
+ "@types/jest": "^29.2.3",
50
+ "@types/react": "^18.0.25",
54
51
  "es-check": "^5.1.1",
55
52
  "gh-pages": "^3.1.0",
56
53
  "husky": "^4.0.0",
57
- "jest": "^26.4.2",
54
+ "jest": "^29.3.1",
55
+ "jest-environment-jsdom": "^29.3.1",
58
56
  "lint-staged": "^10.0.0",
59
- "next": "^12.1.0",
57
+ "next": "^13.0.5",
60
58
  "prettier": "^2.0.0",
61
- "react": "^17.0.2",
62
- "react-dom": "^17.0.2",
63
- "react-modal": "^3.14.3",
64
- "react-test-renderer": "^17.0.2",
65
- "rollup": "^2.28.2",
66
- "tsd": "^0.13.1"
59
+ "react": "^18.2.0",
60
+ "react-dom": "^18.2.0",
61
+ "react-modal": "^3.16.1",
62
+ "react-test-renderer": "^18.2.0",
63
+ "ts-jest": "^29.0.3",
64
+ "typescript": "^4.9.3"
67
65
  },
68
66
  "peerDependencies": {
69
- "next": "^9.0.0 || ^10.0.0 || ^11.0.0 || ^12.0.0",
70
- "react": ">=16.8.0"
67
+ "next": "^13.0.0",
68
+ "react": ">=18.0.0"
71
69
  },
72
70
  "jest": {
71
+ "preset": "ts-jest/presets/js-with-ts",
73
72
  "testPathIgnorePatterns": [
74
73
  ".test-d\\.ts"
75
74
  ],
@@ -79,10 +78,8 @@
79
78
  "coverageReporters": [
80
79
  "text",
81
80
  "lcov"
82
- ]
83
- },
84
- "tsd": {
85
- "directory": "src/__tests__"
81
+ ],
82
+ "testEnvironment": "jsdom"
86
83
  },
87
84
  "husky": {
88
85
  "hooks": {
package/dist/index.cjs.js DELETED
@@ -1,50 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var react = require('react');
6
- var router = require('next/router');
7
- var stringify = require('qs-stringify');
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';
14
- /**
15
- * During contextual routing browser URL will be controlled by Next Router's "as" prop
16
- * while the page actually rendered is defined by Next Router's "href" prop.
17
- *
18
- * During contextual navigation Next Router's behaves as follows:
19
- * router.asPath: /item/3 reflects current URL and updates at each page change
20
- * router.pathname: /search/[terms] stay the same as long as initial page doesn't change
21
- * router.query: {"terms": "foo-bar"} same as above
22
- */
23
-
24
- function useContextualRouting() {
25
- var router$1 = router.useRouter();
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]);
43
- return {
44
- returnHref: returnHref,
45
- makeContextualHref: makeContextualHref
46
- };
47
- }
48
-
49
- exports.RETURN_HREF_QUERY_PARAM = RETURN_HREF_QUERY_PARAM;
50
- exports.useContextualRouting = useContextualRouting;
package/dist/index.esm.js DELETED
@@ -1,41 +0,0 @@
1
- import { useCallback } from 'react';
2
- import { useRouter } from 'next/router';
3
- import stringify from 'qs-stringify';
4
-
5
- var RETURN_HREF_QUERY_PARAM = '_UCR_return_href';
6
- /**
7
- * During contextual routing browser URL will be controlled by Next Router's "as" prop
8
- * while the page actually rendered is defined by Next Router's "href" prop.
9
- *
10
- * During contextual navigation Next Router's behaves as follows:
11
- * router.asPath: /item/3 reflects current URL and updates at each page change
12
- * router.pathname: /search/[terms] stay the same as long as initial page doesn't change
13
- * router.query: {"terms": "foo-bar"} same as above
14
- */
15
-
16
- function useContextualRouting() {
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
- */
26
-
27
- var returnHref = returnHrefQueryParam != null ? returnHrefQueryParam : router.asPath; // @NOTE JSON.stringify might be replaced with any hashing solution
28
-
29
- var queryHash = JSON.stringify(watchedQuery);
30
- var makeContextualHref = useCallback(function (extraParams) {
31
- var _Object$assign;
32
-
33
- return router.pathname + '?' + stringify(Object.assign({}, router.query, extraParams, (_Object$assign = {}, _Object$assign[RETURN_HREF_QUERY_PARAM] = returnHref, _Object$assign)));
34
- }, [queryHash, returnHref]);
35
- return {
36
- returnHref: returnHref,
37
- makeContextualHref: makeContextualHref
38
- };
39
- }
40
-
41
- export { RETURN_HREF_QUERY_PARAM, useContextualRouting };
@@ -1,10 +0,0 @@
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' }));
@@ -1,131 +0,0 @@
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 DELETED
@@ -1,6 +0,0 @@
1
- export function useContextualRouting(): {
2
- returnHref: string;
3
- makeContextualHref: (extraQueryParams: { [key: string]: any }) => string;
4
- };
5
-
6
-
package/src/index.js DELETED
@@ -1,42 +0,0 @@
1
- import { useCallback } from 'react';
2
- import { useRouter } from 'next/router';
3
- import stringify from 'qs-stringify';
4
- export const RETURN_HREF_QUERY_PARAM = '_UCR_return_href';
5
-
6
- /**
7
- * During contextual routing browser URL will be controlled by Next Router's "as" prop
8
- * while the page actually rendered is defined by Next Router's "href" prop.
9
- *
10
- * During contextual navigation Next Router's behaves as follows:
11
- * router.asPath: /item/3 reflects current URL and updates at each page change
12
- * router.pathname: /search/[terms] stay the same as long as initial page doesn't change
13
- * router.query: {"terms": "foo-bar"} same as above
14
- */
15
- export function useContextualRouting() {
16
- const router = useRouter();
17
- const returnHrefQueryParam = router.query[RETURN_HREF_QUERY_PARAM];
18
- const watchedQuery = Object.assign({}, router.query);
19
- delete watchedQuery[RETURN_HREF_QUERY_PARAM];
20
-
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
- );
40
-
41
- return { returnHref, makeContextualHref };
42
- }