UNPKG

preload-next

Version:

A Preload Link Component for Next.js to handle seamless page transitions

77 lines (76 loc) 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDomainLocale = exports.createPropError = exports.removePathTrailingSlash = exports.removeLocale = exports.addBasePath = exports.removeBasePath = exports.hasBasePath = void 0; var path_has_prefix_1 = require("next/dist/shared/lib/router/utils/path-has-prefix"); var parse_path_1 = require("next/dist/shared/lib/router/utils/parse-path"); function hasBasePath(path) { return (0, path_has_prefix_1.pathHasPrefix)(path, basePath); } exports.hasBasePath = hasBasePath; var removeBasePath = function (path) { if (process.env.__NEXT_MANUAL_CLIENT_BASE_PATH) { if (!hasBasePath(path)) { return path; } } path = path.slice(basePath.length); if (!path.startsWith("/")) path = "/".concat(path); return path; }; exports.removeBasePath = removeBasePath; var addBasePath = function (path, required) { if (process.env.__NEXT_MANUAL_CLIENT_BASE_PATH) { if (!required) { return path; } } }; exports.addBasePath = addBasePath; function removeLocale(path, locale) { if (process.env.__NEXT_I18N_SUPPORT) { var pathname = (0, parse_path_1.parsePath)(path).pathname; var pathLower = pathname.toLowerCase(); var localeLower = locale === null || locale === void 0 ? void 0 : locale.toLowerCase(); return locale && (pathLower.startsWith("/".concat(localeLower, "/")) || pathLower === "/".concat(localeLower)) ? "".concat(pathname.length === locale.length + 1 ? "/" : "").concat(path.slice(locale.length + 1)) : path; } return path; } exports.removeLocale = removeLocale; var removePathTrailingSlash = function (path) { if (path !== "/") { return path.replace(/\/$/, ""); } return path; }; exports.removePathTrailingSlash = removePathTrailingSlash; var createPropError = function (args) { return new Error("Failed prop type: The prop `".concat(args.key, "` expects a ").concat(args.expected, " in `<Link>`, but got `").concat(args.actual, "` instead.") + (typeof window !== "undefined" ? "\nOpen your browser's console to view the Component stack trace." : "")); }; exports.createPropError = createPropError; var basePath = process.env.__NEXT_ROUTER_BASEPATH || ""; function getDomainLocale(path, locale, locales, domainLocales) { if (process.env.__NEXT_I18N_SUPPORT) { var normalizeLocalePath = require("./normalize-locale-path").normalizeLocalePath; var detectDomainLocale_1 = require("./detect-domain-locale").detectDomainLocale; var target = locale || normalizeLocalePath(path, locales).detectedLocale; var domain = detectDomainLocale_1(domainLocales, undefined, target); if (domain) { var proto = "http".concat(domain.http ? "" : "s", "://"); var finalLocale = target === domain.defaultLocale ? "" : "/".concat(target); return "".concat(proto).concat(domain.domain).concat(basePath).concat(finalLocale).concat(path); } return false; } else { return false; } } exports.getDomainLocale = getDomainLocale;