Skip to content

Commit 134de67

Browse files
authored
(fix/map) Map failed to filter by path if indexed (#1333)
* Nick: * Update map.ts * Update map.ts
1 parent f87e117 commit 134de67

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

apps/api/src/controllers/v1/map.ts

+26
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export async function getMapResults({
5656
allowExternalLinks,
5757
abort = new AbortController().signal, // noop
5858
mock,
59+
filterByPath = true,
5960
}: {
6061
url: string;
6162
search?: string;
@@ -70,6 +71,7 @@ export async function getMapResults({
7071
allowExternalLinks?: boolean;
7172
abort?: AbortSignal;
7273
mock?: string;
74+
filterByPath?: boolean;
7375
}): Promise<MapResult> {
7476
const id = uuidv4();
7577
let links: string[] = [url];
@@ -247,6 +249,29 @@ export async function getMapResults({
247249
links = links.filter((x) => isSameSubdomain(x, url));
248250
}
249251

252+
// Filter by path if enabled
253+
if (filterByPath && !allowExternalLinks) {
254+
try {
255+
const urlObj = new URL(url);
256+
const urlPath = urlObj.pathname;
257+
// Only apply path filtering if the URL has a significant path (not just '/' or empty)
258+
// This means we only filter by path if the user has not selected a root domain
259+
if (urlPath && urlPath !== '/' && urlPath.length > 1) {
260+
links = links.filter(link => {
261+
try {
262+
const linkObj = new URL(link);
263+
return linkObj.pathname.startsWith(urlPath);
264+
} catch (e) {
265+
return false;
266+
}
267+
});
268+
}
269+
} catch (e) {
270+
// If URL parsing fails, continue without path filtering
271+
logger.warn(`Failed to parse URL for path filtering: ${url}`, { error: e });
272+
}
273+
}
274+
250275
// remove duplicates that could be due to http/https or www
251276
links = removeDuplicateUrls(links);
252277
}
@@ -300,6 +325,7 @@ export async function mapController(
300325
plan: req.auth.plan,
301326
abort: abort.signal,
302327
mock: req.body.useMock,
328+
filterByPath: req.body.filterByPath !== false,
303329
}),
304330
...(req.body.timeout !== undefined ? [
305331
new Promise((resolve, reject) => setTimeout(() => {

apps/api/src/controllers/v1/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ export const mapRequestSchema = crawlerOptions
506506
limit: z.number().min(1).max(30000).default(5000),
507507
timeout: z.number().positive().finite().optional(),
508508
useMock: z.string().optional(),
509+
filterByPath: z.boolean().default(true),
509510
})
510511
.strict(strictMessage);
511512

0 commit comments

Comments
 (0)