@nimpl/getters
Version:
Implementation of server getters in React Server Components without switching to SSR in next.js
28 lines (27 loc) • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPathname = getPathname;
const work_async_storage_external_1 = require("next/dist/server/app-render/work-async-storage.external");
const work_unit_async_storage_external_1 = require("next/dist/server/app-render/work-unit-async-storage.external");
const has_base_path_1 = require("next/dist/client/has-base-path");
const remove_base_path_1 = require("next/dist/client/remove-base-path");
const server_getter_in_client_component_error_1 = require("./server-getter-in-client-component-error");
const utils_1 = require("./utils");
function getPathname() {
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("getPathname");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pageStore = work_unit_async_storage_external_1.workUnitAsyncStorage.getStore();
let originalPathname = pageStore?.url?.pathname;
if (!originalPathname) {
const pageConfigurationStore = work_async_storage_external_1.workAsyncStorage.getStore();
originalPathname =
pageConfigurationStore?.route &&
(0, utils_1.normalizeInterceptingRoutes)(pageConfigurationStore.route.split("/")).join("/");
}
if (!originalPathname) {
return null;
}
const url = new URL(originalPathname, "http://n");
const pathname = (0, has_base_path_1.hasBasePath)(url.pathname) ? (0, remove_base_path_1.removeBasePath)(url.pathname) : url.pathname;
return pathname;
}