gatsby-link 4.12.0-next.3 → 4.12.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gatsby-link",
3
3
  "description": "An enhanced Link component for Gatsby sites with support for resource prefetching",
4
- "version": "4.12.0-next.3",
4
+ "version": "4.12.0",
5
5
  "author": "Kyle Mathews <mathews.kyle@gmail.com>",
6
6
  "bugs": {
7
7
  "url": "https://github.com/gatsbyjs/gatsby/issues"
@@ -9,14 +9,14 @@
9
9
  "dependencies": {
10
10
  "@babel/runtime": "^7.15.4",
11
11
  "@types/reach__router": "^1.3.10",
12
- "gatsby-page-utils": "^2.12.0-next.3",
12
+ "gatsby-page-utils": "^2.12.0",
13
13
  "prop-types": "^15.7.2"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@babel/cli": "^7.15.4",
17
17
  "@babel/core": "^7.15.5",
18
18
  "@testing-library/react": "^11.2.7",
19
- "babel-preset-gatsby-package": "^2.12.0-next.0",
19
+ "babel-preset-gatsby-package": "^2.12.0",
20
20
  "cross-env": "^7.0.3"
21
21
  },
22
22
  "peerDependencies": {
@@ -44,5 +44,5 @@
44
44
  "engines": {
45
45
  "node": ">=14.15.0"
46
46
  },
47
- "gitHead": "b474dbb25518b51ca8cc1ce75fffb044e7d8609a"
47
+ "gitHead": "12e90fce284eb0a64b7d1f15e293027483826372"
48
48
  }
package/index.js DELETED
@@ -1,295 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- exports.__esModule = true;
6
- exports.withPrefix = withPrefix;
7
- exports.withAssetPrefix = withAssetPrefix;
8
- exports.navigate = exports.default = void 0;
9
-
10
- var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
11
-
12
- var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
13
-
14
- var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
15
-
16
- var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
17
-
18
- var _propTypes = _interopRequireDefault(require("prop-types"));
19
-
20
- var _react = _interopRequireDefault(require("react"));
21
-
22
- var _reachRouter = require("@gatsbyjs/reach-router");
23
-
24
- var _parsePath = require("./parse-path");
25
-
26
- exports.parsePath = _parsePath.parsePath;
27
-
28
- var _isLocalLink = require("./is-local-link");
29
-
30
- var _rewriteLinkPath = require("./rewrite-link-path");
31
-
32
- var _excluded = ["to", "getProps", "onClick", "onMouseEnter", "activeClassName", "activeStyle", "innerRef", "partiallyActive", "state", "replace", "_location"];
33
-
34
- function withPrefix(path, prefix) {
35
- var _ref, _prefix;
36
-
37
- if (prefix === void 0) {
38
- prefix = getGlobalBasePrefix();
39
- }
40
-
41
- if (!(0, _isLocalLink.isLocalLink)(path)) {
42
- return path;
43
- }
44
-
45
- if (path.startsWith("./") || path.startsWith("../")) {
46
- return path;
47
- }
48
-
49
- var base = (_ref = (_prefix = prefix) !== null && _prefix !== void 0 ? _prefix : getGlobalPathPrefix()) !== null && _ref !== void 0 ? _ref : "/";
50
- return "" + (base !== null && base !== void 0 && base.endsWith("/") ? base.slice(0, -1) : base) + (path.startsWith("/") ? path : "/" + path);
51
- } // These global values are wrapped in typeof clauses to ensure the values exist.
52
- // This is especially problematic in unit testing of this component.
53
-
54
-
55
- var getGlobalPathPrefix = function getGlobalPathPrefix() {
56
- return process.env.NODE_ENV !== "production" ? typeof __PATH_PREFIX__ !== "undefined" ? __PATH_PREFIX__ : undefined : __PATH_PREFIX__;
57
- };
58
-
59
- var getGlobalBasePrefix = function getGlobalBasePrefix() {
60
- return process.env.NODE_ENV !== "production" ? typeof __BASE_PATH__ !== "undefined" ? __BASE_PATH__ : undefined : __BASE_PATH__;
61
- };
62
-
63
- function withAssetPrefix(path) {
64
- return withPrefix(path, getGlobalPathPrefix());
65
- }
66
-
67
- var NavLinkPropTypes = {
68
- activeClassName: _propTypes.default.string,
69
- activeStyle: _propTypes.default.object,
70
- partiallyActive: _propTypes.default.bool
71
- }; // Set up IntersectionObserver
72
-
73
- var createIntersectionObserver = function createIntersectionObserver(el, cb) {
74
- var io = new window.IntersectionObserver(function (entries) {
75
- entries.forEach(function (entry) {
76
- if (el === entry.target) {
77
- // Check if element is within viewport, remove listener, destroy observer, and run link callback.
78
- // MSEdge doesn't currently support isIntersecting, so also test for an intersectionRatio > 0
79
- cb(entry.isIntersecting || entry.intersectionRatio > 0);
80
- }
81
- });
82
- }); // Add element to the observer
83
-
84
- io.observe(el);
85
- return {
86
- instance: io,
87
- el: el
88
- };
89
- };
90
-
91
- function GatsbyLinkLocationWrapper(props) {
92
- return /*#__PURE__*/_react.default.createElement(_reachRouter.Location, null, function (_ref2) {
93
- var location = _ref2.location;
94
- return /*#__PURE__*/_react.default.createElement(GatsbyLink, (0, _extends2.default)({}, props, {
95
- _location: location
96
- }));
97
- });
98
- }
99
-
100
- var GatsbyLink = /*#__PURE__*/function (_React$Component) {
101
- (0, _inheritsLoose2.default)(GatsbyLink, _React$Component);
102
-
103
- function GatsbyLink(props) {
104
- var _this;
105
-
106
- _this = _React$Component.call(this, props) || this; // Default to no support for IntersectionObserver
107
-
108
- _this.defaultGetProps = function (_ref3) {
109
- var isPartiallyCurrent = _ref3.isPartiallyCurrent,
110
- isCurrent = _ref3.isCurrent;
111
-
112
- if (_this.props.partiallyActive ? isPartiallyCurrent : isCurrent) {
113
- return {
114
- className: [_this.props.className, _this.props.activeClassName].filter(Boolean).join(" "),
115
- style: (0, _extends2.default)({}, _this.props.style, _this.props.activeStyle)
116
- };
117
- }
118
-
119
- return null;
120
- };
121
-
122
- var IOSupported = false;
123
-
124
- if (typeof window !== "undefined" && window.IntersectionObserver) {
125
- IOSupported = true;
126
- }
127
-
128
- _this.state = {
129
- IOSupported: IOSupported
130
- };
131
- _this.abortPrefetch = null;
132
- _this.handleRef = _this.handleRef.bind((0, _assertThisInitialized2.default)(_this));
133
- return _this;
134
- }
135
-
136
- var _proto = GatsbyLink.prototype;
137
-
138
- _proto._prefetch = function _prefetch() {
139
- var currentPath = window.location.pathname + window.location.search; // reach router should have the correct state
140
-
141
- if (this.props._location && this.props._location.pathname) {
142
- currentPath = this.props._location.pathname + this.props._location.search;
143
- }
144
-
145
- var rewrittenPath = (0, _rewriteLinkPath.rewriteLinkPath)(this.props.to, currentPath);
146
- var parsed = (0, _parsePath.parsePath)(rewrittenPath);
147
- var newPathName = parsed.pathname + parsed.search; // Prefetch is used to speed up next navigations. When you use it on the current navigation,
148
- // there could be a race-condition where Chrome uses the stale data instead of waiting for the network to complete
149
-
150
- if (currentPath !== newPathName) {
151
- return ___loader.enqueue(newPathName);
152
- }
153
-
154
- return undefined;
155
- };
156
-
157
- _proto.componentWillUnmount = function componentWillUnmount() {
158
- if (!this.io) {
159
- return;
160
- }
161
-
162
- var _this$io = this.io,
163
- instance = _this$io.instance,
164
- el = _this$io.el;
165
-
166
- if (this.abortPrefetch) {
167
- this.abortPrefetch.abort();
168
- }
169
-
170
- instance.unobserve(el);
171
- instance.disconnect();
172
- };
173
-
174
- _proto.handleRef = function handleRef(ref) {
175
- var _this2 = this;
176
-
177
- if (this.props.innerRef && Object.prototype.hasOwnProperty.call(this.props.innerRef, "current")) {
178
- this.props.innerRef.current = ref;
179
- } else if (this.props.innerRef) {
180
- this.props.innerRef(ref);
181
- }
182
-
183
- if (this.state.IOSupported && ref) {
184
- // If IO supported and element reference found, setup Observer functionality
185
- this.io = createIntersectionObserver(ref, function (inViewPort) {
186
- if (inViewPort) {
187
- _this2.abortPrefetch = _this2._prefetch();
188
- } else {
189
- if (_this2.abortPrefetch) {
190
- _this2.abortPrefetch.abort();
191
- }
192
- }
193
- });
194
- }
195
- };
196
-
197
- _proto.render = function render() {
198
- var _this3 = this;
199
-
200
- var _this$props = this.props,
201
- to = _this$props.to,
202
- _this$props$getProps = _this$props.getProps,
203
- getProps = _this$props$getProps === void 0 ? this.defaultGetProps : _this$props$getProps,
204
- _onClick = _this$props.onClick,
205
- _onMouseEnter = _this$props.onMouseEnter,
206
- $activeClassName = _this$props.activeClassName,
207
- $activeStyle = _this$props.activeStyle,
208
- $innerRef = _this$props.innerRef,
209
- partiallyActive = _this$props.partiallyActive,
210
- state = _this$props.state,
211
- replace = _this$props.replace,
212
- _location = _this$props._location,
213
- rest = (0, _objectWithoutPropertiesLoose2.default)(_this$props, _excluded);
214
-
215
- if (process.env.NODE_ENV !== "production" && !(0, _isLocalLink.isLocalLink)(to)) {
216
- console.warn("External link " + to + " was detected in a Link component. Use the Link component only for internal links. See: https://gatsby.dev/internal-links");
217
- }
218
-
219
- var prefixedTo = (0, _rewriteLinkPath.rewriteLinkPath)(to, _location.pathname);
220
-
221
- if (!(0, _isLocalLink.isLocalLink)(prefixedTo)) {
222
- return /*#__PURE__*/_react.default.createElement("a", (0, _extends2.default)({
223
- href: prefixedTo
224
- }, rest));
225
- }
226
-
227
- return /*#__PURE__*/_react.default.createElement(_reachRouter.Link, (0, _extends2.default)({
228
- to: prefixedTo,
229
- state: state,
230
- getProps: getProps,
231
- innerRef: this.handleRef,
232
- onMouseEnter: function onMouseEnter(e) {
233
- if (_onMouseEnter) {
234
- _onMouseEnter(e);
235
- }
236
-
237
- var parsed = (0, _parsePath.parsePath)(prefixedTo);
238
-
239
- ___loader.hovering(parsed.pathname + parsed.search);
240
- },
241
- onClick: function onClick(e) {
242
- if (_onClick) {
243
- _onClick(e);
244
- }
245
-
246
- if (e.button === 0 && // ignore right clicks
247
- !_this3.props.target && // let browser handle "target=_blank"
248
- !e.defaultPrevented && // onClick prevented default
249
- !e.metaKey && // ignore clicks with modifier keys...
250
- !e.altKey && !e.ctrlKey && !e.shiftKey) {
251
- e.preventDefault();
252
- var shouldReplace = replace;
253
-
254
- var isCurrent = encodeURI(prefixedTo) === _location.pathname;
255
-
256
- if (typeof replace !== "boolean" && isCurrent) {
257
- shouldReplace = true;
258
- } // Make sure the necessary scripts and data are
259
- // loaded before continuing.
260
-
261
-
262
- window.___navigate(prefixedTo, {
263
- state: state,
264
- replace: shouldReplace
265
- });
266
- }
267
-
268
- return true;
269
- }
270
- }, rest));
271
- };
272
-
273
- return GatsbyLink;
274
- }(_react.default.Component);
275
-
276
- GatsbyLink.propTypes = (0, _extends2.default)({}, NavLinkPropTypes, {
277
- onClick: _propTypes.default.func,
278
- to: _propTypes.default.string.isRequired,
279
- replace: _propTypes.default.bool,
280
- state: _propTypes.default.object
281
- });
282
-
283
- var _default = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
284
- return /*#__PURE__*/_react.default.createElement(GatsbyLinkLocationWrapper, (0, _extends2.default)({
285
- innerRef: ref
286
- }, props));
287
- });
288
-
289
- exports.default = _default;
290
-
291
- var navigate = function navigate(to, options) {
292
- window.___navigate((0, _rewriteLinkPath.rewriteLinkPath)(to, window.location.pathname), options);
293
- };
294
-
295
- exports.navigate = navigate;
package/is-local-link.js DELETED
@@ -1,21 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.isLocalLink = void 0;
5
- // Copied from https://github.com/sindresorhus/is-absolute-url/blob/3ab19cc2e599a03ea691bcb8a4c09fa3ebb5da4f/index.js
6
- var ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/;
7
-
8
- var isAbsolute = function isAbsolute(path) {
9
- return ABSOLUTE_URL_REGEX.test(path);
10
- };
11
-
12
- var isLocalLink = function isLocalLink(path) {
13
- if (typeof path !== "string") {
14
- return undefined; // TODO(v5): Re-Add TypeError
15
- // throw new TypeError(`Expected a \`string\`, got \`${typeof path}\``)
16
- }
17
-
18
- return !isAbsolute(path);
19
- };
20
-
21
- exports.isLocalLink = isLocalLink;
package/parse-path.js DELETED
@@ -1,29 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.parsePath = parsePath;
5
-
6
- function parsePath(path) {
7
- var pathname = path || "/";
8
- var search = "";
9
- var hash = "";
10
- var hashIndex = pathname.indexOf("#");
11
-
12
- if (hashIndex !== -1) {
13
- hash = pathname.slice(hashIndex);
14
- pathname = pathname.slice(0, hashIndex);
15
- }
16
-
17
- var searchIndex = pathname.indexOf("?");
18
-
19
- if (searchIndex !== -1) {
20
- search = pathname.slice(searchIndex);
21
- pathname = pathname.slice(0, searchIndex);
22
- }
23
-
24
- return {
25
- pathname: pathname,
26
- search: search === "?" ? "" : search,
27
- hash: hash === "#" ? "" : hash
28
- };
29
- }
@@ -1,59 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.rewriteLinkPath = void 0;
5
-
6
- var _utils = require("@gatsbyjs/reach-router/lib/utils");
7
-
8
- var _applyTrailingSlashOption = require("gatsby-page-utils/apply-trailing-slash-option");
9
-
10
- var _parsePath2 = require("./parse-path");
11
-
12
- var _isLocalLink = require("./is-local-link");
13
-
14
- var _ = require(".");
15
-
16
- // Specific import to treeshake Node.js stuff
17
- var isAbsolutePath = function isAbsolutePath(path) {
18
- return path === null || path === void 0 ? void 0 : path.startsWith("/");
19
- };
20
-
21
- var getGlobalTrailingSlash = function getGlobalTrailingSlash() {
22
- return typeof __TRAILING_SLASH__ !== "undefined" ? __TRAILING_SLASH__ : undefined;
23
- };
24
-
25
- function absolutify(path, current) {
26
- // If it's already absolute, return as-is
27
- if (isAbsolutePath(path)) {
28
- return path;
29
- }
30
-
31
- return (0, _utils.resolve)(path, current);
32
- }
33
-
34
- var rewriteLinkPath = function rewriteLinkPath(path, relativeTo) {
35
- if (typeof path === "number") {
36
- return path;
37
- }
38
-
39
- if (!(0, _isLocalLink.isLocalLink)(path)) {
40
- return path;
41
- }
42
-
43
- var _parsePath = (0, _parsePath2.parsePath)(path),
44
- pathname = _parsePath.pathname,
45
- search = _parsePath.search,
46
- hash = _parsePath.hash;
47
-
48
- var option = getGlobalTrailingSlash();
49
- var adjustedPath = path;
50
-
51
- if (option === "always" || option === "never") {
52
- var output = (0, _applyTrailingSlashOption.applyTrailingSlashOption)(pathname, option);
53
- adjustedPath = "" + output + search + hash;
54
- }
55
-
56
- return isAbsolutePath(adjustedPath) ? (0, _.withPrefix)(adjustedPath) : absolutify(adjustedPath, relativeTo);
57
- };
58
-
59
- exports.rewriteLinkPath = rewriteLinkPath;