@thoughtbot/superglue 0.30.0 → 0.40.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.
@@ -121,7 +121,9 @@ function visit(path, _temp2) {
121
121
  _ref4$beforeSave = _ref4.beforeSave,
122
122
  beforeSave = _ref4$beforeSave === void 0 ? function (prevPage, receivedPage) {
123
123
  return receivedPage;
124
- } : _ref4$beforeSave;
124
+ } : _ref4$beforeSave,
125
+ _ref4$revisit = _ref4.revisit,
126
+ revisit = _ref4$revisit === void 0 ? false : _ref4$revisit;
125
127
 
126
128
  path = (0, _utils.withoutBusters)(path);
127
129
  var pageKey = (0, _utils.urlToPageKey)(path);
@@ -183,6 +185,14 @@ function visit(path, _temp2) {
183
185
  meta.suggestedAction = 'replace';
184
186
  }
185
187
 
188
+ if (revisit && fetchArgs[1].method == 'GET') {
189
+ if (rsp.redirected) {
190
+ meta.suggestedAction = 'replace';
191
+ } else {
192
+ meta.suggestedAction = 'none';
193
+ }
194
+ }
195
+
186
196
  if (method !== 'GET') {
187
197
  var contentLocation = rsp.headers.get('content-location');
188
198
 
package/components/Nav.js CHANGED
@@ -33,11 +33,13 @@ var Nav = /*#__PURE__*/function (_React$Component) {
33
33
  initialPageKey = _this$props.initialPageKey;
34
34
  _this.history = history;
35
35
  _this.navigateTo = _this.navigateTo.bind(_assertThisInitialized(_this));
36
+ _this.scrollTo = _this.scrollTo.bind(_assertThisInitialized(_this));
36
37
  _this.onHistoryChange = _this.onHistoryChange.bind(_assertThisInitialized(_this));
37
38
  _this.state = {
38
39
  pageKey: initialPageKey,
39
40
  ownProps: {}
40
41
  };
42
+ _this.hasWindow = typeof window !== 'undefined';
41
43
  return _this;
42
44
  }
43
45
 
@@ -59,21 +61,39 @@ var Nav = /*#__PURE__*/function (_React$Component) {
59
61
  action = _ref.action,
60
62
  ownProps = _ref.ownProps;
61
63
 
64
+ if (action === 'none') {
65
+ return false;
66
+ }
67
+
62
68
  path = (0, _utils.pathWithoutBZParams)(path);
63
69
  var nextPageKey = (0, _utils.urlToPageKey)(path);
64
70
  var store = this.props.store;
65
71
  var hasPage = !!store.getState().pages[nextPageKey];
66
72
 
67
73
  if (hasPage) {
68
- var prevPageKey = this.history.location.state.pageKey;
74
+ var location = this.history.location;
75
+ var prevPageKey = location.state.pageKey;
69
76
  var historyArgs = [path, {
70
77
  pageKey: nextPageKey,
71
- superglue: true
78
+ superglue: true,
79
+ posY: 0,
80
+ posX: 0
72
81
  }];
73
82
 
74
83
  if (action === 'push') {
75
84
  var _this$history;
76
85
 
86
+ if (this.hasWindow) {
87
+ this.history.replace({
88
+ pathname: location.pathname,
89
+ search: location.search,
90
+ hash: location.hash
91
+ }, Object.assign({}, location.state, {
92
+ posY: window.pageYOffset,
93
+ posX: window.pageXOffset
94
+ }));
95
+ }
96
+
77
97
  (_this$history = this.history).push.apply(_this$history, historyArgs);
78
98
  }
79
99
 
@@ -87,6 +107,7 @@ var Nav = /*#__PURE__*/function (_React$Component) {
87
107
  pageKey: nextPageKey,
88
108
  ownProps: ownProps
89
109
  });
110
+ this.scrollTo(0, 0);
90
111
 
91
112
  if (action === 'replace' && prevPageKey && prevPageKey !== nextPageKey) {
92
113
  store.dispatch({
@@ -104,8 +125,18 @@ var Nav = /*#__PURE__*/function (_React$Component) {
104
125
  }
105
126
  };
106
127
 
107
- _proto.onHistoryChange = function onHistoryChange(location) {
108
- var store = this.props.store;
128
+ _proto.scrollTo = function scrollTo(posX, posY) {
129
+ this.hasWindow && window.scrollTo(posX, posY);
130
+ };
131
+
132
+ _proto.onHistoryChange = function onHistoryChange(_ref2) {
133
+ var _this2 = this;
134
+
135
+ var location = _ref2.location,
136
+ action = _ref2.action;
137
+ var _this$props2 = this.props,
138
+ store = _this$props2.store,
139
+ visit = _this$props2.visit;
109
140
  var pathname = location.pathname,
110
141
  search = location.search,
111
142
  hash = location.hash,
@@ -120,23 +151,75 @@ var Nav = /*#__PURE__*/function (_React$Component) {
120
151
  hash: hash
121
152
  }
122
153
  });
123
- var pageKey = state.pageKey;
154
+
155
+ if (action !== 'POP') {
156
+ return;
157
+ }
158
+
159
+ var pageKey = state.pageKey,
160
+ posX = state.posX,
161
+ posY = state.posY;
124
162
  var containsKey = !!store.getState().pages[pageKey];
125
163
 
126
164
  if (containsKey) {
127
- this.setState({
128
- pageKey: pageKey
129
- });
165
+ var restoreStrategy = store.getState().pages[pageKey].restoreStrategy;
166
+
167
+ switch (restoreStrategy) {
168
+ case 'fromCacheOnly':
169
+ this.setState({
170
+ pageKey: pageKey
171
+ });
172
+ this.scrollTo(posX, posY);
173
+ break;
174
+
175
+ case 'fromCacheAndRevisitInBackground':
176
+ this.setState({
177
+ pageKey: pageKey
178
+ });
179
+ this.scrollTo(posX, posY);
180
+ visit(pageKey, {
181
+ revisit: true
182
+ });
183
+ break;
184
+
185
+ case 'revisitOnly':
186
+ default:
187
+ visit(pageKey, {
188
+ revisit: true
189
+ }).then(function (meta) {
190
+ if (meta === undefined) {
191
+ console.warn("scoll restoration was skipped. Your visit's then funtion\n should return the meta object it recieved if you want your\n application to restore the page's previous scroll.");
192
+ }
193
+
194
+ if (!!meta && meta.suggestedAction === 'none') {
195
+ _this2.setState({
196
+ pageKey: pageKey
197
+ });
198
+
199
+ _this2.scrollTo(posX, posY);
200
+ }
201
+ });
202
+ }
130
203
  } else {
131
- this.reloadPage();
204
+ visit(pageKey, {
205
+ revisit: true
206
+ }).then(function (meta) {
207
+ if (meta === undefined) {
208
+ console.warn("scoll restoration was skipped. Your visit's then funtion\n should return the meta object it recieved if you want your\n application to restore the page's previous scroll.");
209
+ }
210
+
211
+ if (!!meta && meta.suggestedAction === 'none') {
212
+ _this2.setState({
213
+ pageKey: pageKey
214
+ });
215
+
216
+ _this2.scrollTo(posX, posY);
217
+ }
218
+ });
132
219
  }
133
220
  }
134
221
  };
135
222
 
136
- _proto.reloadPage = function reloadPage() {
137
- window.location.reload();
138
- };
139
-
140
223
  _proto.notFound = function notFound(identifier) {
141
224
  var reminder = '';
142
225
 
@@ -149,10 +232,10 @@ var Nav = /*#__PURE__*/function (_React$Component) {
149
232
  };
150
233
 
151
234
  _proto.render = function render() {
152
- var _this$props2 = this.props,
153
- store = _this$props2.store,
154
- visit = _this$props2.visit,
155
- remote = _this$props2.remote;
235
+ var _this$props3 = this.props,
236
+ store = _this$props3.store,
237
+ visit = _this$props3.visit,
238
+ remote = _this$props3.remote;
156
239
  var _this$state = this.state,
157
240
  pageKey = _this$state.pageKey,
158
241
  ownProps = _this$state.ownProps;
package/index.js CHANGED
@@ -198,7 +198,7 @@ var ApplicationBase = /*#__PURE__*/function (_React$Component) {
198
198
  visit: this.visit,
199
199
  remote: this.remote,
200
200
  store: this.store,
201
- ujsAttributePrefix: 'data-bz'
201
+ ujsAttributePrefix: 'data-sg'
202
202
  });
203
203
  var _this$ujsHandlers = this.ujsHandlers,
204
204
  onClick = _this$ujsHandlers.onClick,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thoughtbot/superglue",
3
- "version": "0.30.0",
3
+ "version": "0.40.0",
4
4
  "description": "Use a vanilla Rails with React and Redux",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,7 +26,7 @@
26
26
  "eslint-plugin-react": "^7.24.0",
27
27
  "fetch-headers": "^2.0.0",
28
28
  "fetch-mock": "^9.11.0",
29
- "history": "^4.7.1",
29
+ "history": "^5.3.0",
30
30
  "html-react-parser": "^1.2.6",
31
31
  "jest": "^27.0.4",
32
32
  "node-fetch": "^2.6.1",
@@ -43,7 +43,7 @@
43
43
  "html-react-parser": ">=1.2.6",
44
44
  "react": ">=16",
45
45
  "redux": ">=4.1",
46
- "history": "^4.7.1",
46
+ "history": "^5.3.0",
47
47
  "react-redux": ">=7.2",
48
48
  "redux-thunk": ">=2.3"
49
49
  },
package/utils/helpers.js CHANGED
@@ -31,6 +31,8 @@ function argsForHistory(path) {
31
31
  var pageKey = (0, _url.urlToPageKey)(path);
32
32
  return [path, {
33
33
  superglue: true,
34
- pageKey: pageKey
34
+ pageKey: pageKey,
35
+ posX: 0,
36
+ posY: 0
35
37
  }];
36
38
  }