feat: detect vercel env (#425)

This commit is contained in:
Innei
2023-04-26 10:44:27 +01:00
committed by GitHub
parent 16ba0c5abf
commit e7d1b1d7d8
4 changed files with 33 additions and 3 deletions
+3 -1
View File
@@ -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">
+5
View File
@@ -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
View File
@@ -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(
+6 -1
View File
@@ -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)
}