Skip to content

Commit 90c4ec2

Browse files
committed
Merge remote-tracking branch 'query/with-query-string' into kutt_dev
thedevs-network#630
2 parents 0dc2289 + 57923f5 commit 90c4ec2

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

server/handlers/links.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,37 @@ export const redirect = (app: ReturnType<typeof next>): Handler => async (
289289
return res.redirect("/banned");
290290
}
291291

292-
// 5. If wants to see link info, then redirect
292+
// 5. Append query string when provided
293+
if (req.query) {
294+
const url = URL.parse(link.target, true);
295+
const linkTargetParams = new URLSearchParams(url.search);
296+
297+
let added = 0;
298+
Object.entries(req.query).forEach(([key, value]) => {
299+
if (typeof value === "string") {
300+
added++;
301+
linkTargetParams.append(key, value);
302+
}
303+
});
304+
305+
if (added) {
306+
url.search = linkTargetParams.toString();
307+
link.target = URL.format(url);
308+
}
309+
}
310+
311+
// 6. If wants to see link info, then redirect
293312
const doesRequestInfo = /.*\+$/gi.test(req.params.id);
294313
if (doesRequestInfo && !link.password) {
295314
return app.render(req, res, "/url-info", { target: link.target });
296315
}
297316

298-
// 6. If link is protected, redirect to password page
317+
// 7. If link is protected, redirect to password page
299318
if (link.password) {
300319
return res.redirect(`/protected/${link.uuid}`);
301320
}
302321

303-
// 7. Create link visit
322+
// 8. Create link visit
304323
if (link.user_id && !isBot) {
305324
queue.visit.add({
306325
headers: req.headers,
@@ -310,7 +329,19 @@ export const redirect = (app: ReturnType<typeof next>): Handler => async (
310329
});
311330
}
312331

313-
// 8. Redirect to target
332+
// 9. Create Google Analytics visit
333+
if (env.GOOGLE_ANALYTICS_UNIVERSAL && !isBot) {
334+
ua(env.GOOGLE_ANALYTICS_UNIVERSAL)
335+
.pageview({
336+
dp: `/${address}`,
337+
ua: req.headers["user-agent"],
338+
uip: req.realIP,
339+
aip: 1
340+
})
341+
.send();
342+
}
343+
344+
// 10. Redirect to target
314345
return res.redirect(link.target);
315346
};
316347

0 commit comments

Comments
 (0)