feat: detect vercel env (#425)
This commit is contained in:
@@ -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 (
|
||||
<Link
|
||||
key={post.id}
|
||||
href={`/${post.slug || post.id}`}
|
||||
href={getSlugUrl(`/${post.slug || post.id}`)}
|
||||
className="xlog-post sm:hover:bg-hover bg-white transition-all px-5 py-7 -mx-5 first:-mt-5 sm:rounded-xl flex flex-col sm:flex-row items-center"
|
||||
suppressHydrationWarning
|
||||
>
|
||||
<div className="flex-1 flex justify-center flex-col w-full min-w-0">
|
||||
<h3 className="xlog-post-title text-2xl font-bold text-zinc-700">
|
||||
|
||||
@@ -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"
|
||||
|
||||
+19
-1
@@ -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(
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user