From 4d86c8c72714879e8e6c060479b02c553d35bb0f Mon Sep 17 00:00:00 2001 From: Sebastien Castiel Date: Fri, 19 Jan 2024 12:03:16 -0500 Subject: [PATCH] Remove varianle NEXT_PUBLIC_BASE_URL --- .env.example | 3 +-- container.env.example | 1 - src/app/groups/[groupId]/share-button.tsx | 24 +++++++++++++---------- src/app/layout.tsx | 2 -- src/app/robots.ts | 5 +++-- src/app/sitemap.ts | 19 +++++++++--------- src/lib/env.ts | 1 - src/lib/hooks.ts | 8 ++++++++ 8 files changed, 36 insertions(+), 27 deletions(-) diff --git a/.env.example b/.env.example index e61e4ef..bc38b5f 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,2 @@ POSTGRES_PRISMA_URL=postgresql://postgres:1234@localhost -POSTGRES_URL_NON_POOLING=postgresql://postgres:1234@localhost -NEXT_PUBLIC_BASE_URL=http://localhost:3000 \ No newline at end of file +POSTGRES_URL_NON_POOLING=postgresql://postgres:1234@localhost \ No newline at end of file diff --git a/container.env.example b/container.env.example index ecfc160..7451dcb 100644 --- a/container.env.example +++ b/container.env.example @@ -4,4 +4,3 @@ POSTGRES_PASSWORD=1234 # app POSTGRES_PRISMA_URL=postgresql://postgres:${POSTGRES_PASSWORD}@db POSTGRES_URL_NON_POOLING=postgresql://postgres:${POSTGRES_PASSWORD}@db -NEXT_PUBLIC_BASE_URL=http://localhost:3000 diff --git a/src/app/groups/[groupId]/share-button.tsx b/src/app/groups/[groupId]/share-button.tsx index 9e96a49..8676346 100644 --- a/src/app/groups/[groupId]/share-button.tsx +++ b/src/app/groups/[groupId]/share-button.tsx @@ -1,3 +1,4 @@ +'use client' import { CopyButton } from '@/components/copy-button' import { ShareUrlButton } from '@/components/share-url-button' import { Button } from '@/components/ui/button' @@ -7,7 +8,7 @@ import { PopoverContent, PopoverTrigger, } from '@/components/ui/popover' -import { env } from '@/lib/env' +import { useBaseUrl } from '@/lib/hooks' import { Group } from '@prisma/client' import { Share } from 'lucide-react' @@ -16,7 +17,8 @@ type Props = { } export function ShareButton({ group }: Props) { - const url = `${env.NEXT_PUBLIC_BASE_URL}/groups/${group.id}/expenses?ref=share` + const baseUrl = useBaseUrl() + const url = baseUrl && `${baseUrl}/groups/${group.id}/expenses?ref=share` return ( @@ -30,14 +32,16 @@ export function ShareButton({ group }: Props) { For other participants to see the group and add expenses, share its URL with them.

-
- - - -
+ {url && ( +
+ + + +
+ )}

Warning! Every person with the group URL will be able to see and edit expenses. Share with caution! diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 7d95d6e..9447a84 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -3,14 +3,12 @@ import { ThemeProvider } from '@/components/theme-provider' import { ThemeToggle } from '@/components/theme-toggle' import { Button } from '@/components/ui/button' import { Toaster } from '@/components/ui/toaster' -import { env } from '@/lib/env' import type { Metadata, Viewport } from 'next' import Image from 'next/image' import Link from 'next/link' import './globals.css' export const metadata: Metadata = { - metadataBase: new URL(env.NEXT_PUBLIC_BASE_URL), title: { default: 'Spliit · Share Expenses with Friends & Family', template: '%s · Spliit', diff --git a/src/app/robots.ts b/src/app/robots.ts index 5fb07a6..2492611 100644 --- a/src/app/robots.ts +++ b/src/app/robots.ts @@ -1,4 +1,3 @@ -import { env } from '@/lib/env' import { MetadataRoute } from 'next' export default function robots(): MetadataRoute.Robots { @@ -8,6 +7,8 @@ export default function robots(): MetadataRoute.Robots { allow: '/', disallow: '/groups/', }, - sitemap: `${env.NEXT_PUBLIC_BASE_URL}/sitemap.xml`, + sitemap: process.env.VERCEL_URL + ? `${process.env.VERCEL_URL}/sitemap.xml` + : undefined, } } diff --git a/src/app/sitemap.ts b/src/app/sitemap.ts index a3b92a1..7dfbba7 100644 --- a/src/app/sitemap.ts +++ b/src/app/sitemap.ts @@ -1,13 +1,14 @@ -import { env } from '@/lib/env' import { MetadataRoute } from 'next' export default function sitemap(): MetadataRoute.Sitemap { - return [ - { - url: env.NEXT_PUBLIC_BASE_URL, - lastModified: new Date(), - changeFrequency: 'yearly', - priority: 1, - }, - ] + return process.env.VERCEL_URL + ? [ + { + url: process.env.VERCEL_URL, + lastModified: new Date(), + changeFrequency: 'yearly', + priority: 1, + }, + ] + : [] } diff --git a/src/lib/env.ts b/src/lib/env.ts index af5616c..31b91e7 100644 --- a/src/lib/env.ts +++ b/src/lib/env.ts @@ -1,7 +1,6 @@ import { z } from 'zod' const envSchema = z.object({ - NEXT_PUBLIC_BASE_URL: z.string().url(), POSTGRES_URL_NON_POOLING: z.string().url(), POSTGRES_PRISMA_URL: z.string().url(), }) diff --git a/src/lib/hooks.ts b/src/lib/hooks.ts index 02e2543..4d3ad08 100644 --- a/src/lib/hooks.ts +++ b/src/lib/hooks.ts @@ -40,3 +40,11 @@ export function useMediaQuery(query: string): boolean { return matches } + +export function useBaseUrl() { + const [baseUrl, setBaseUrl] = useState(null) + useEffect(() => { + setBaseUrl(window.location.origin) + }, []) + return baseUrl +}