Skip to content

Commit

Permalink
Add query string support
Browse files Browse the repository at this point in the history
  • Loading branch information
mariodian committed Oct 4, 2022
1 parent 430b874 commit ef0b457
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions server/handlers/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,32 @@ export const redirect = (app: ReturnType<typeof next>): Handler => async (
return res.redirect("/banned");
}

// 5. If wants to see link info, then redirect
// 5. Append query string when provided
if (req.query) {
const linkTargetParams = new URLSearchParams(link.target);

Object.entries(req.query).forEach(([key, value]) => {
if (typeof value === "string") {
linkTargetParams.append(key, value);
}
});

// toString() encodes original url, decode it back
link.target = decodeURIComponent(linkTargetParams.toString());
}

// 6. If wants to see link info, then redirect
const doesRequestInfo = /.*\+$/gi.test(req.params.id);
if (doesRequestInfo && !link.password) {
return app.render(req, res, "/url-info", { target: link.target });
}

// 6. If link is protected, redirect to password page
// 7. If link is protected, redirect to password page
if (link.password) {
return res.redirect(`/protected/${link.uuid}`);
}

// 7. Create link visit
// 8. Create link visit
if (link.user_id && !isBot) {
queue.visit.add({
headers: req.headers,
Expand All @@ -310,7 +324,7 @@ export const redirect = (app: ReturnType<typeof next>): Handler => async (
});
}

// 8. Create Google Analytics visit
// 9. Create Google Analytics visit
if (env.GOOGLE_ANALYTICS_UNIVERSAL && !isBot) {
ua(env.GOOGLE_ANALYTICS_UNIVERSAL)
.pageview({
Expand Down

0 comments on commit ef0b457

Please sign in to comment.