@searchspring/snap-url-manager 0.71.0 → 0.72.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 +17 -33
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,38 +1,21 @@
|
|
|
1
1
|
# Snap URL Manager
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
Snap URL Manager is available on each controller via `controller.urlManager`. It is a core service that provides customizable frontend URL management and a means to subscribe to the URL for changes. Its primary purpose is to provide a standard API to manage URL across Snap products.
|
|
5
5
|
|
|
6
|
-
## Installation
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
npm install --save-dev @searchspring/url-manager
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
## Import
|
|
13
|
-
|
|
14
|
-
```js
|
|
15
|
-
import { UrlManager, UrlTranslator } from '@searchspring/url-manager';
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
## Instantiation
|
|
19
|
-
|
|
20
|
-
The UrlManager class takes one parameter. This is the translator that manages your URL scheme, as well as rules for updating the URL. For now, we'll use the included translator that uses query strings and hash fragments and utilizes pushState to modify the URL in the browser.
|
|
7
|
+
A `urlManager` is constructed with a `Translator` instance. This is the translator that manages your URL scheme, as well as rules for updating the URL. For now, we'll use the included translator that uses query strings and hash fragments and utilizes pushState to modify the URL in the browser.
|
|
21
8
|
|
|
22
9
|
See [Translators](https://github.com/searchspring/snap/tree/main/packages/snap-url-manager/src/Translators) for more info.
|
|
23
10
|
|
|
24
|
-
|
|
25
|
-
const urlManager = new UrlManager(new UrlTranslator());
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
Depending on the translator, the instantiated url manager will automatically start tracking from the browser URL.
|
|
11
|
+
Depending on the translator, the instantiated `urlManager` will automatically start tracking from the browser URL.
|
|
29
12
|
|
|
30
13
|
## Navigate to another location using transforms.
|
|
31
14
|
|
|
32
15
|
Let's add a new parameter, `page=2`, to the browser's address bar.
|
|
33
16
|
|
|
34
17
|
```js
|
|
35
|
-
urlManager.merge({ page: 2 }).go();
|
|
18
|
+
controller.urlManager.merge({ page: 2 }).go();
|
|
36
19
|
```
|
|
37
20
|
|
|
38
21
|
In this example, we first created a new url manager with internal state that reflects `page=2` . Url managers are never directly modified, but when merging or setting parameters, a new url manager is created with a new state.
|
|
@@ -50,19 +33,19 @@ Paths can be expressed in either of these formats:
|
|
|
50
33
|
Therefore, you can also accomplish this same transformation with the following:
|
|
51
34
|
|
|
52
35
|
```js
|
|
53
|
-
const newManager = urlManager.set('page', 2);
|
|
36
|
+
const newManager = controller.urlManager.set('page', 2);
|
|
54
37
|
```
|
|
55
38
|
|
|
56
39
|
The page parameter can be removed with the following:
|
|
57
40
|
|
|
58
41
|
```js
|
|
59
|
-
const newManager = urlManager.remove('page');
|
|
42
|
+
const newManager = controller.urlManager.remove('page');
|
|
60
43
|
```
|
|
61
44
|
|
|
62
45
|
Setting a value in a longer, nested path would look like this:
|
|
63
46
|
|
|
64
47
|
```js
|
|
65
|
-
const newManager = urlManager.set('filter.color', 'blue');
|
|
48
|
+
const newManager = controller.urlManager.set('filter.color', 'blue');
|
|
66
49
|
```
|
|
67
50
|
|
|
68
51
|
This will produce 1 value for `filter.color` and remove any others.
|
|
@@ -80,7 +63,7 @@ const newManager = urlManager
|
|
|
80
63
|
You can access a url manager's href property at any time with
|
|
81
64
|
|
|
82
65
|
```js
|
|
83
|
-
urlManager.href;
|
|
66
|
+
controller.urlManager.href;
|
|
84
67
|
```
|
|
85
68
|
|
|
86
69
|
## Many copies
|
|
@@ -93,7 +76,7 @@ const facetValuesWithUrls = (facet, facetValues) => {
|
|
|
93
76
|
return facetValues.map((facetValue) => {
|
|
94
77
|
return {
|
|
95
78
|
...facetValue,
|
|
96
|
-
url: urlManager.merge(['filter', facet.field], facetValue.value),
|
|
79
|
+
url: controller.urlManager.merge(['filter', facet.field], facetValue.value),
|
|
97
80
|
};
|
|
98
81
|
});
|
|
99
82
|
};
|
|
@@ -148,7 +131,7 @@ See [Linkers](https://github.com/searchspring/snap/tree/main/packages/snap-url-m
|
|
|
148
131
|
You can subscribe to URL changes like so:
|
|
149
132
|
|
|
150
133
|
```js
|
|
151
|
-
urlManager.subscribe((prev, next) => {
|
|
134
|
+
controller.urlManager.subscribe((prev, next) => {
|
|
152
135
|
console.log(prev, next);
|
|
153
136
|
});
|
|
154
137
|
```
|
|
@@ -171,11 +154,11 @@ Take the following example:
|
|
|
171
154
|
```js
|
|
172
155
|
const urlManager = new UrlManager(new UrlTranslator());
|
|
173
156
|
|
|
174
|
-
const urlManagerWithColor = urlManager.merge('filter.color_family', 'blue');
|
|
157
|
+
const urlManagerWithColor = controller.urlManager.merge('filter.color_family', 'blue');
|
|
175
158
|
|
|
176
159
|
console.log(window.location.href); // https://try.searchspring.com?q=dress
|
|
177
160
|
|
|
178
|
-
console.log(urlManager.href); // ?q=dress
|
|
161
|
+
console.log(controller.urlManager.href); // ?q=dress
|
|
179
162
|
console.log(urlManagerWithColor.href); // ?q=dress#/filter:color_family:Blue
|
|
180
163
|
```
|
|
181
164
|
|
|
@@ -184,11 +167,11 @@ You can see that the href of `urlManager` matches the parameters of the page URL
|
|
|
184
167
|
Now let's navigate to a new URL using the `go` function and check the href's again:
|
|
185
168
|
|
|
186
169
|
```js
|
|
187
|
-
const urlManagerWithNewParam = urlManager.merge({ newParam: 'newValue' });
|
|
170
|
+
const urlManagerWithNewParam = controller.urlManager.merge({ newParam: 'newValue' });
|
|
188
171
|
|
|
189
172
|
urlManagerWithNewParam.go();
|
|
190
173
|
console.log(window.location.href); // https://try.searchspring.com?q=dress#/newParam:newValue
|
|
191
|
-
console.log(urlManager.href); // ?q=dress#/newParam:newValue
|
|
174
|
+
console.log(controller.urlManager.href); // ?q=dress#/newParam:newValue
|
|
192
175
|
console.log(urlManagerWithNewParam.href); // ?q=dress#/newParam:newValue
|
|
193
176
|
|
|
194
177
|
console.log(urlManagerWithColor.href); // ?q=dress#/filter:color_family:Blue/newParam:newValue
|
|
@@ -200,14 +183,15 @@ state of `urlManagerWithColor` remains unmodified. This holds true for removals
|
|
|
200
183
|
```js
|
|
201
184
|
const urlManagerWithColorButWithoutNewParam = urlManagerWithColor.remove('newParam');
|
|
202
185
|
|
|
203
|
-
console.log(urlManager.href); // ?q=dress#/newParam:newValue
|
|
186
|
+
console.log(controller.urlManager.href); // ?q=dress#/newParam:newValue
|
|
204
187
|
console.log(urlManagerWithNewParam.href); // ?q=dress#/newParam:newValue
|
|
205
188
|
|
|
206
189
|
console.log(urlManagerWithColor.href); // ?q=dress#/filter:color_family:Blue/newParam:newValuecolor=blue
|
|
207
190
|
console.log(urlManagerWithColorButWithoutNewParam.href); // ?q=dress#/filter:color_family:Blue
|
|
208
191
|
```
|
|
209
192
|
|
|
210
|
-
|
|
193
|
+
> [!NOTE]
|
|
194
|
+
> UrlManagers with different instantiations will not notify each other about URL updates. For example:
|
|
211
195
|
|
|
212
196
|
```js
|
|
213
197
|
const originalUrlManager = new UrlManager(new QueryStringTranslator());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@searchspring/snap-url-manager",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.72.0",
|
|
4
4
|
"description": "Snap URL Manager",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"files": [
|
|
31
31
|
"dist/**/*"
|
|
32
32
|
],
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "737d4bd308ebbb852fe3f09b2b90f4af87bbe264"
|
|
34
34
|
}
|