@coog/gatsby-plugin-react-i18next
Version:
Easily translate your Gatsby website into multiple languages
87 lines (67 loc) • 2.48 kB
JavaScript
;
exports.__esModule = true;
exports.navigate = void 0;
/**
* HACK because gatsby-link navigate doesn't check if window exists so it fails at build
*/
var _utils = require('@gatsbyjs/reach-router/lib/utils');
var getGlobalPathPrefix = function getGlobalPathPrefix() {
return undefined; // return process.env.NODE_ENV !== 'production'
// ? typeof __PATH_PREFIX__ !== 'undefined'
// ? __PATH_PREFIX__
// : undefined
// : __PATH_PREFIX__;
};
var getGlobalBasePrefix = function getGlobalBasePrefix() {
return undefined; // return process.env.NODE_ENV !== 'production'
// ? typeof __BASE_PATH__ !== 'undefined'
// ? __BASE_PATH__
// : undefined
// : __BASE_PATH__;
};
var withPrefix = function withPrefix(path, prefix) {
var _ref, _prefix;
if (prefix === void 0) {
prefix = getGlobalBasePrefix();
}
if (!isLocalLink(path)) {
return path;
}
if (path.startsWith('./') || path.startsWith('../')) {
return path;
}
var base = (_ref = (_prefix = prefix) !== null && _prefix !== void 0 ? _prefix : getGlobalPathPrefix()) !== null && _ref !== void 0 ? _ref : '/';
return '' + (base !== null && base !== void 0 && base.endsWith('/') ? base.slice(0, -1) : base) + (path.startsWith('/') ? path : '/' + path);
};
var isAbsolutePath = function isAbsolutePath(path) {
return path === null || path === void 0 ? void 0 : path.startsWith('/');
};
var absolutify = function absolutify(path, current) {
// If it's already absolute, return as-is
if (isAbsolutePath(path)) {
return path;
}
return _utils.resolve(path, current);
};
var isLocalLink = function isLocalLink(path) {
return path && !path.startsWith('http://') && !path.startsWith('https://') && !path.startsWith('//');
};
var rewriteLinkPath = function rewriteLinkPath(path, relativeTo) {
if (typeof path === 'number') {
return path;
}
if (!isLocalLink(path)) {
return path;
}
return isAbsolutePath(path) ? withPrefix(path) : absolutify(path, relativeTo);
}; // WORKAROUND to avoid typescript error TS2695: Left side of comma operator is unused and has no side effects.
// see: https://stackoverflow.com/questions/56457935/typescript-error-property-x-does-not-exist-on-type-window
var navigate = function navigate(to, options) {
if (typeof window !== 'undefined') {
window.___navigate(rewriteLinkPath(to, window.location.pathname), options);
}
};
/**
* END HACK
*/
exports.navigate = navigate;