Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

internal_runWithWaitUntil

Steven Tey879261c79196
diff --git a/apps/web/app/api/edge/metatags/route-wip.ts b/apps/web/app/api/edge/metatags/route.ts
similarity index 88%
rename from apps/web/app/api/edge/metatags/route-wip.ts
rename to apps/web/app/api/edge/metatags/route.ts
index 1a00e96a66..4ae43470f1 100644
--- a/apps/web/app/api/edge/metatags/route-wip.ts
+++ b/apps/web/app/api/edge/metatags/route.ts
@@ -2,13 +2,13 @@ import { ratelimit } from "@/lib/upstash";
import { LOCALHOST_IP, isValidUrl } from "@dub/utils";
import { ipAddress } from "@vercel/edge";
import { getToken } from "next-auth/jwt";
-import { NextFetchEvent, NextRequest } from "next/server";
+import { NextRequest } from "next/server";
import { getMetaTags } from "./utils";
export const runtime = "edge";
// TODO: waitUntil() is not supported in App Router yet: https://vercel.com/docs/functions/edge-functions/edge-functions-api#waituntil
-export async function GET(req: NextRequest, ev: NextFetchEvent) {
+export async function GET(req: NextRequest) {
const url = req.nextUrl.searchParams.get("url");
if (!url || !isValidUrl(url)) {
return new Response("Invalid URL", { status: 400 });
@@ -27,7 +27,7 @@ export async function GET(req: NextRequest, ev: NextFetchEvent) {
}
}
- const metatags = await getMetaTags(url, ev);
+ const metatags = await getMetaTags(url);
return new Response(JSON.stringify(metatags), {
status: 200,
headers: {
diff --git a/apps/web/app/api/edge/metatags/utils.ts b/apps/web/app/api/edge/metatags/utils.ts
index a32b866978..283667c535 100644
--- a/apps/web/app/api/edge/metatags/utils.ts
+++ b/apps/web/app/api/edge/metatags/utils.ts
@@ -2,6 +2,7 @@ import { parse } from "node-html-parser";
import { recordMetatags } from "@/lib/upstash";
import { isValidUrl } from "@dub/utils";
import { NextFetchEvent } from "next/server";
+import { internal_runWithWaitUntil as waitUntil } from "next/dist/server/web/internal-edge-wait-until";
export const getHtml = async (url: string) => {
try {
@@ -60,7 +61,7 @@ export const getRelativeUrl = (url: string, imageUrl: string) => {
return new URL(imageUrl, baseURL).toString();
};
-export const getMetaTags = async (url: string, ev?: NextFetchEvent) => {
+export const getMetaTags = async (url: string) => {
const html = await getHtml(url);
if (!html) {
return {
@@ -99,9 +100,9 @@ export const getMetaTags = async (url: string, ev?: NextFetchEvent) => {
object["icon"] ||
object["shortcut icon"];
- ev?.waitUntil(
- recordMetatags(url, title && description && image ? false : true),
- );
+ waitUntil(async () => {
+ await recordMetatags(url, title && description && image ? false : true);
+ });
return {
title: title || url,
diff --git a/apps/web/pages/api/edge/metatags.ts b/apps/web/pages/api/edge/metatags.ts
deleted file mode 100644
index 63a7e76b3e..0000000000
--- a/apps/web/pages/api/edge/metatags.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-import { ratelimit } from "@/lib/upstash";
-import { LOCALHOST_IP, isValidUrl } from "@dub/utils";
-import { ipAddress } from "@vercel/edge";
-import { getMetaTags } from "app/api/edge/metatags/utils";
-import { getToken } from "next-auth/jwt";
-import { NextFetchEvent, NextRequest } from "next/server";
-
-export const config = {
- runtime: "edge",
-};
-
-export default async function handler(req: NextRequest, ev: NextFetchEvent) {
- if (req.method === "GET") {
- const url = req.nextUrl.searchParams.get("url");
- if (!url || !isValidUrl(url)) {
- return new Response("Invalid URL", { status: 400 });
- }
-
- // Rate limit if user is not logged in
- const session = await getToken({
- req,
- secret: process.env.NEXTAUTH_SECRET,
- });
- if (!session?.email) {
- const ip = ipAddress(req) || LOCALHOST_IP;
- const { success } = await ratelimit().limit(ip);
- if (!success) {
- return new Response("Don't DDoS me pls 🥺", { status: 429 });
- }
- }
-
- const metatags = await getMetaTags(url, ev);
- return new Response(JSON.stringify(metatags), {
- status: 200,
- headers: {
- "Content-Type": "application/json",
- "Access-Control-Allow-Origin": "*",
- },
- });
- } else if (req.method === "OPTIONS") {
- return new Response(null, {
- status: 204,
- headers: {
- "Access-Control-Allow-Origin": "*",
- "Access-Control-Allow-Methods": "GET, OPTIONS",
- },
- });
- } else {
- return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
- }
-}