Skip to content

Commit 5438298

Browse files
authored
Merge pull request dubinc#2272 from dubinc/links-redirect
Use middleware for /links redirect
2 parents 94ad759 + 0106f0f commit 5438298

15 files changed

Lines changed: 183 additions & 186 deletions

File tree

apps/web/app/api/workspaces/[idOrSlug]/exists/route.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { withSession } from "@/lib/auth";
2-
import { isReservedKey } from "@/lib/edge-config";
32
import { prisma } from "@dub/prisma";
4-
import { DEFAULT_REDIRECTS } from "@dub/utils";
3+
import { DEFAULT_REDIRECTS, RESERVED_SLUGS } from "@dub/utils";
54
import { NextResponse } from "next/server";
65

76
// GET /api/workspaces/[idOrSlug]/exists – check if a project exists
87
export const GET = withSession(async ({ params }) => {
98
const { idOrSlug: slug } = params;
10-
if ((await isReservedKey(slug)) || DEFAULT_REDIRECTS[slug]) {
9+
if (RESERVED_SLUGS.includes(slug) || DEFAULT_REDIRECTS[slug]) {
1110
return NextResponse.json(1);
1211
}
1312
const project = await prisma.project.findUnique({

apps/web/app/app.dub.co/(onboarding)/onboarding/page.tsx

Lines changed: 0 additions & 5 deletions
This file was deleted.

apps/web/app/app.dub.co/(redirects)/[slug]/page.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

apps/web/lib/api/links/utils/key-checks.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { DubApiError } from "@/lib/api/errors";
2-
import {
3-
isBlacklistedKey,
4-
isReservedKey,
5-
isReservedUsername,
6-
} from "@/lib/edge-config";
2+
import { isBlacklistedKey, isReservedUsername } from "@/lib/edge-config";
73
import { checkIfKeyExists } from "@/lib/planetscale";
84
import { WorkspaceProps } from "@/lib/types";
95
import {
106
DEFAULT_REDIRECTS,
117
isDubDomain,
128
isReservedKeyGlobal,
9+
RESERVED_SLUGS,
1310
} from "@dub/utils";
1411

1512
export async function keyChecks({
@@ -46,7 +43,7 @@ export async function keyChecks({
4643

4744
if (isDubDomain(domain) && process.env.NEXT_PUBLIC_IS_DUB) {
4845
if (domain === "dub.sh" || domain === "dub.link") {
49-
if (DEFAULT_REDIRECTS[key] || (await isReservedKey(key))) {
46+
if (DEFAULT_REDIRECTS[key] || RESERVED_SLUGS.includes(key)) {
5047
return {
5148
error: "Duplicate key: This short link already exists.",
5249
code: "conflict",

apps/web/lib/edge-config/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ export * from "./is-blacklisted-domain";
33
export * from "./is-blacklisted-email";
44
export * from "./is-blacklisted-key";
55
export * from "./is-blacklisted-referrer";
6-
export * from "./is-reserved-key";
76
export * from "./is-reserved-username";
87
export * from "./update";

apps/web/lib/edge-config/is-reserved-key.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

apps/web/lib/middleware/app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ export default async function AppMiddleware(req: NextRequest) {
9696
) {
9797
return WorkspacesMiddleware(req, user);
9898
} else if (appRedirect(path)) {
99-
return NextResponse.redirect(new URL(appRedirect(path), req.url));
99+
return NextResponse.redirect(
100+
new URL(`${appRedirect(path)}${searchParamsString}`, req.url),
101+
);
100102
}
101103
}
102104

apps/web/lib/middleware/new-link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default async function NewLinkMiddleware(
1717
if (defaultWorkspace) {
1818
return NextResponse.redirect(
1919
new URL(
20-
`/${defaultWorkspace}?newLink=${searchParams.get("link") || true}${searchParams.has("domain") ? `&newLinkDomain=${searchParams.get("domain")}` : ""}`,
20+
`/${defaultWorkspace}/links?newLink=${searchParams.get("link") || true}${searchParams.has("domain") ? `&newLinkDomain=${searchParams.get("domain")}` : ""}`,
2121
req.url,
2222
),
2323
);

apps/web/lib/middleware/utils/app-redirect.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1+
import { RESERVED_SLUGS } from "@dub/utils";
2+
13
const APP_REDIRECTS = {
24
"/account": "/account/settings",
35
"/referrals": "/account/settings/referrals",
4-
"/welcome": "/onboarding",
6+
"/onboarding": "/onboarding/welcome",
7+
"/welcome": "/onboarding/welcome",
58
};
69

710
export const appRedirect = (path: string) => {
811
if (APP_REDIRECTS[path]) {
912
return APP_REDIRECTS[path];
1013
}
14+
// Redirect "/[slug]" to "/[slug]/links"
15+
const rootRegex = /^\/([^\/]+)$/;
16+
if (rootRegex.test(path) && !RESERVED_SLUGS.includes(path.split("/")[1]))
17+
return path.replace(rootRegex, "/$1/links");
1118

1219
// Redirect "programs/[programId]/settings" to "programs/[programId]/settings/rewards" (first tab)
1320
const programSettingsRegex = /\/programs\/([^\/]+)\/settings$/;

apps/web/lib/middleware/workspaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default async function WorkspacesMiddleware(
2121
}
2222
return NextResponse.redirect(
2323
new URL(
24-
`/${defaultWorkspace}${redirectPath}${searchParamsString}`,
24+
`/${defaultWorkspace}${redirectPath || "/links"}${searchParamsString}`,
2525
req.url,
2626
),
2727
);

0 commit comments

Comments
 (0)