From e7d1b1d7d8353b4922fd03b60e417809c02e3c92 Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 26 Apr 2023 17:44:27 +0800 Subject: [PATCH] feat: detect vercel env (#425) --- src/components/site/SiteHome.tsx | 4 +++- src/lib/env.ts | 5 +++++ src/lib/helpers.ts | 20 +++++++++++++++++++- src/pages/api/redirection.tsx | 7 ++++++- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/components/site/SiteHome.tsx b/src/components/site/SiteHome.tsx index 901e0bfc..68f6cb4f 100644 --- a/src/components/site/SiteHome.tsx +++ b/src/components/site/SiteHome.tsx @@ -6,6 +6,7 @@ import { useEffect, useState } from "react" import { Button } from "~/components/ui/Button" import { Image } from "~/components/ui/Image" import { useDate } from "~/hooks/useDate" +import { getSlugUrl } from "~/lib/helpers" import { Notes } from "~/lib/types" import { EmptyState } from "../ui/EmptyState" @@ -41,8 +42,9 @@ export const SiteHome: React.FC<{ return (

diff --git a/src/lib/env.ts b/src/lib/env.ts index c976f180..1ea21746 100644 --- a/src/lib/env.ts +++ b/src/lib/env.ts @@ -20,3 +20,8 @@ export const IPFS_GATEWAY = process.env.NEXT_PUBLIC_IPFS_GATEWAY || "https://ipfs.4everland.xyz/ipfs/" export const MIRA_LINK = process.env.NEXT_PUBLIC_MIRA_LINK || "https://mira.crossbell.io" +export const IS_VERCEL_PREVIEW = + process.env.NEXT_PUBLIC_VERCEL_ENV === "preview" || + process.env.VERCEL_ENV === "preview" + +export const IS_DEV = process.env.NODE_ENV === "development" diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index 44fe043a..7d236411 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -3,7 +3,8 @@ import { NoteEntity } from "crossbell.js" import { Note, Profile } from "~/lib/types" import { IS_PROD } from "./constants" -import { OUR_DOMAIN } from "./env" +import { IS_VERCEL_PREVIEW, OUR_DOMAIN } from "./env" +import { isServerSide } from "./utils" export const getSiteLink = ({ domain, @@ -14,15 +15,32 @@ export const getSiteLink = ({ subdomain: string noProtocol?: boolean }) => { + if (IS_VERCEL_PREVIEW) return `/_site/${subdomain}` + if (domain) { return `https://${domain}` } if (noProtocol) { return `${subdomain}.${OUR_DOMAIN}` } + return `${IS_PROD ? "https" : "http"}://${subdomain}.${OUR_DOMAIN}` } +export const getSlugUrl = (slug: string) => { + if (!isServerSide() && IS_VERCEL_PREVIEW) { + const pathArr = new URL(location.href).pathname.split("/").filter(($) => $) + const indicatorIndex = pathArr.findIndex(($) => $ === "_site") + if (-~indicatorIndex) { + const handle = pathArr[indicatorIndex + 1] + + return `/_site/${handle}${slug}` + } + } + + return `/${slug}` +} + export const getNoteSlug = (note: NoteEntity) => { return ( note.metadata?.content?.attributes?.find( diff --git a/src/pages/api/redirection.tsx b/src/pages/api/redirection.tsx index 9c0003a3..67158935 100644 --- a/src/pages/api/redirection.tsx +++ b/src/pages/api/redirection.tsx @@ -1,6 +1,7 @@ import { NextApiRequest, NextApiResponse } from "next" import { getDefaultSlug } from "~/lib/default-slug" +import { IS_VERCEL_PREVIEW } from "~/lib/env" import { getSiteLink } from "~/lib/helpers" import { checkDomainServer } from "~/models/site.model" @@ -45,5 +46,9 @@ export default async function handler( link += `/${encodeURIComponent(slug)}` } - res.redirect(link) + if (IS_VERCEL_PREVIEW) { + const path = new URL(link).pathname + + res.redirect(`/_site/${character.handle}${path}`) + } else res.redirect(link) }