♻️ Refactor code for generating absolute url (#904)

This commit is contained in:
Luke Vella 2023-10-19 18:01:00 +01:00 committed by GitHub
parent eaa8f5813d
commit 703d551aac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 108 additions and 61 deletions

View file

@ -2,7 +2,6 @@ import "tailwindcss/tailwind.css";
import "../style.css";
import { trpc, UserSession } from "@rallly/backend/next/trpc/client";
import { absoluteUrl } from "@rallly/utils";
import { inject } from "@vercel/analytics";
import dayjs from "dayjs";
import localizedFormat from "dayjs/plugin/localizedFormat";
@ -16,6 +15,8 @@ import { appWithTranslation } from "next-i18next";
import { DefaultSeo, SoftwareAppJsonLd } from "next-seo";
import React from "react";
import { absoluteUrl } from "@/utils/absolute-url";
import * as nextI18nNextConfig from "../../next-i18next.config.js";
import { NextPageWithLayout } from "../types";

View file

@ -1,5 +1,4 @@
import { ArrowLeftIcon } from "@rallly/icons";
import { absoluteUrl } from "@rallly/utils";
import { GetStaticPropsContext } from "next";
import ErrorPage from "next/error";
import Head from "next/head";
@ -14,6 +13,7 @@ import { getBlogLayout } from "@/components/layouts/blog-layout";
import { getAllPosts, getPostBySlug } from "@/lib/api";
import markdownToHtml from "@/lib/markdownToHtml";
import { NextPageWithLayout, Post } from "@/types";
import { absoluteUrl } from "@/utils/absolute-url";
import { getStaticTranslations } from "@/utils/page-translations";
type Props = {

View file

@ -0,0 +1,30 @@
const port = process.env.PORT || 3000;
const getVercelUrl = () => {
return process.env.NEXT_PUBLIC_VERCEL_URL
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
: null;
};
function joinPath(baseUrl: string, subpath = "") {
if (subpath) {
const url = new URL(subpath, baseUrl);
return url.href;
}
return baseUrl;
}
export function absoluteUrl(subpath = "", query?: Record<string, string>) {
const queryString = query
? `?${Object.entries(query)
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
.join("&")}`
: "";
const baseUrl =
process.env.NEXT_PUBLIC_BASE_URL ??
getVercelUrl() ??
`http://localhost:${port}`;
return joinPath(baseUrl, subpath) + queryString;
}

View file

@ -9,10 +9,6 @@ declare global {
* "development" or "production"
*/
NODE_ENV: "development" | "production";
/**
* Set to "true" to take users straight to app instead of landing page
*/
DISABLE_LANDING_PAGE?: string;
/**
* Must be 32 characters long
*/
@ -84,10 +80,6 @@ declare global {
* The app version just for reference
*/
NEXT_PUBLIC_APP_VERSION?: string;
/**
* "true" to enable finalization of polls
*/
NEXT_PUBLIC_ENABLE_FINALIZATION?: string;
}
}
}

View file

@ -8,7 +8,6 @@ import {
DialogTitle,
DialogTrigger,
} from "@rallly/ui/dialog";
import { shortUrl } from "@rallly/utils";
import Link from "next/link";
import React from "react";
import { useCopyToClipboard } from "react-use";
@ -16,6 +15,7 @@ import { useCopyToClipboard } from "react-use";
import { useParticipants } from "@/components/participants-provider";
import { Trans } from "@/components/trans";
import { usePoll } from "@/contexts/poll";
import { shortUrl } from "@/utils/absolute-url";
import { isSelfHosted } from "@/utils/constants";
export const InviteDialog = () => {

View file

@ -1,10 +1,11 @@
import { getSession } from "@rallly/backend/next/session";
import { stripe } from "@rallly/backend/stripe";
import { prisma } from "@rallly/database";
import { absoluteUrl } from "@rallly/utils";
import { NextApiRequest, NextApiResponse } from "next";
import { z } from "zod";
import { absoluteUrl } from "@/utils/absolute-url";
export const config = {
edge: true,
};

View file

@ -1,10 +1,11 @@
import { getSession } from "@rallly/backend/next/session";
import { stripe } from "@rallly/backend/stripe";
import { prisma } from "@rallly/database";
import { absoluteUrl } from "@rallly/utils";
import { NextApiRequest, NextApiResponse } from "next";
import { z } from "zod";
import { absoluteUrl } from "@/utils/absolute-url";
const inputSchema = z.object({
session_id: z.string().optional(),
return_path: z.string().optional(),

View file

@ -1,6 +1,7 @@
import { trpcNextApiHandler } from "@rallly/backend/next/trpc/server";
import { NextApiRequest, NextApiResponse } from "next";
import { absoluteUrl, shortUrl } from "@/utils/absolute-url";
import { getServerSession, isEmailBlocked } from "@/utils/auth";
import { isSelfHosted } from "@/utils/constants";
import { emailClient } from "@/utils/emails";
@ -31,5 +32,7 @@ export default async function handler(
emailClient,
isSelfHosted,
isEmailBlocked,
absoluteUrl,
shortUrl,
})(req, res);
}

View file

@ -2,7 +2,6 @@ import { trpc } from "@rallly/backend";
import { prisma } from "@rallly/database";
import { ArrowUpLeftIcon } from "@rallly/icons";
import { Button } from "@rallly/ui/button";
import { absoluteUrl } from "@rallly/utils";
import { GetStaticProps } from "next";
import Head from "next/head";
import Link from "next/link";
@ -19,6 +18,7 @@ import { UserProvider, useUser } from "@/components/user-provider";
import { VisibilityProvider } from "@/components/visibility";
import { PermissionsContext } from "@/contexts/permissions";
import { usePoll } from "@/contexts/poll";
import { absoluteUrl } from "@/utils/absolute-url";
import { ConnectedDayjsProvider } from "@/utils/dayjs";
import { getStaticTranslations } from "@/utils/with-page-translations";

View file

@ -6,10 +6,30 @@ const getVercelUrl = () => {
: null;
};
export function absoluteUrl(path = "") {
function joinPath(baseUrl: string, subpath = "") {
if (subpath) {
const url = new URL(subpath, baseUrl);
return url.href;
}
return baseUrl;
}
export function absoluteUrl(subpath = "", query?: Record<string, string>) {
const queryString = query
? `?${Object.entries(query)
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
.join("&")}`
: "";
const baseUrl =
process.env.NEXT_PUBLIC_BASE_URL ??
getVercelUrl() ??
`http://localhost:${port}`;
return `${baseUrl}${path}`;
return joinPath(baseUrl, subpath) + queryString;
}
export function shortUrl(subpath = "") {
const baseUrl = process.env.NEXT_PUBLIC_SHORT_BASE_URL ?? absoluteUrl();
return joinPath(baseUrl, subpath);
}

View file

@ -1,5 +1,7 @@
import { EmailClient, SupportedEmailProviders } from "@rallly/emails";
import { absoluteUrl } from "@/utils/absolute-url";
const env = process.env["NODE" + "_ENV"];
export const emailClient = new EmailClient({
@ -16,4 +18,8 @@ export const emailClient = new EmailClient({
(process.env.SUPPORT_EMAIL as string),
},
},
context: {
logoUrl: absoluteUrl("/logo.png"),
baseUrl: absoluteUrl(""),
},
});