Skip to content

Commit

Permalink
[Frontend] fix: env usages (#537) (#540)
Browse files Browse the repository at this point in the history
Co-authored-by: Sasha <sasha@codemod.com>
  • Loading branch information
mohab-sameh and r4zendev authored May 10, 2024
1 parent edb3d80 commit 3057d7c
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 46 deletions.
6 changes: 3 additions & 3 deletions apps/frontend/app/(website)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { draftMode } from "next/headers";

import GlobalLayout from "@/components/global/GlobalLayout";
import GlobalLayoutPreview from "@/components/global/GlobalLayoutPreview";
import config from "@/config";
import publicConfig from "@/config";
import { loadGlobalData } from "@/data/sanity";
import { GLOBAL_QUERY } from "@/data/sanity/queries";
import { getOgImages } from "@/data/sanity/resolveSanityRouteMetadata";
Expand All @@ -17,9 +17,9 @@ export async function generateMetadata(): Promise<Metadata> {
const { data } = await loadGlobalData(GLOBAL_QUERY);

return {
title: config.siteName,
title: publicConfig.siteName,
openGraph: {
title: config.siteName,
title: publicConfig.siteName,
images: !data?.fallbackOGImage
? undefined
: getOgImages(data.fallbackOGImage),
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/app/api/draft/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { validatePreviewUrl } from "@sanity/preview-url-secret";
import { draftMode } from "next/headers";
import { redirect } from "next/navigation";

import config from "@/config";
import { client } from "@/data/sanity/client";
import { env } from "@/env";

const clientWithToken = client.withConfig({ token: config.sanity.token });
const clientWithToken = client.withConfig({ token: env.SANITY_API_TOKEN });

export async function GET(request: Request) {
const { isValid, redirectTo = "/" } = await validatePreviewUrl(
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/app/manage/[[...index]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import config from "@/config";
import publicConfig from "@/config";
import type { Metadata } from "next";
import Studio from "./Studio";

export const dynamic = "force-static";

export const metadata: Metadata = {
title: `${config.siteName} - CMS`,
title: `${publicConfig.siteName} - CMS`,
};

export default function StudioPage() {
Expand Down
3 changes: 1 addition & 2 deletions apps/frontend/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import config from "@/config";
import type { MetadataRoute } from "next";
import { groq } from "next-sanity";

Expand All @@ -10,7 +9,7 @@ import type { AutomationAPIListResponse } from "@/types/object.types";
import { pathToAbsUrl } from "@/utils/urls";

const sanityClient = client.withConfig({
token: config.sanity.token,
token: env.SANITY_API_TOKEN,
perspective: "published",
useCdn: false,
stega: false,
Expand Down
16 changes: 7 additions & 9 deletions apps/frontend/config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { env } from "./env";

const config = {
const publicConfig = {
sanity: {
projectId: env.NEXT_PUBLIC_SANITY_PROJECT_ID || "",
dataset: env.NEXT_PUBLIC_SANITY_DATASET || "",
projectId: env.NEXT_PUBLIC_SANITY_PROJECT_ID ?? "",
dataset: env.NEXT_PUBLIC_SANITY_DATASET ?? "",
// Not exposed to the front-end, used solely by the server
token: env.SANITY_API_TOKEN || "",
apiVersion: env.NEXT_PUBLIC_SANITY_API_VERSION || "2023-06-21",
revalidateSecret: env.SANITY_REVALIDATE_SECRET || "",
apiVersion: env.NEXT_PUBLIC_SANITY_API_VERSION ?? "2023-06-21",
studioUrl: "/manage",
},
siteName: "Codemod",
siteDomain: env.NEXT_PUBLIC_SITE_DOMAIN || "",
baseUrl: env.NEXT_PUBLIC_BASE_URL || "",
siteDomain: env.NEXT_PUBLIC_SITE_DOMAIN ?? "",
baseUrl: env.NEXT_PUBLIC_BASE_URL ?? "",
};

export default config;
export default publicConfig;
10 changes: 5 additions & 5 deletions apps/frontend/data/sanity/client.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import config from "@/config";
import publicConfig from "@/config";
import { type ClientPerspective, createClient } from "@sanity/client/stega";
import createImageUrlBuilder from "@sanity/image-url";

const clientConfig = {
projectId: config.sanity.projectId,
dataset: config.sanity.dataset,
apiVersion: config.sanity.apiVersion,
projectId: publicConfig.sanity.projectId,
dataset: publicConfig.sanity.dataset,
apiVersion: publicConfig.sanity.apiVersion,
useCdn: process.env.NODE_ENV === "production",
perspective: "published" as ClientPerspective,
};

export const client = createClient({
...clientConfig,
stega: {
studioUrl: config.sanity.studioUrl,
studioUrl: publicConfig.sanity.studioUrl,
// logger: console,
},
});
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/data/sanity/generateStaticSlugs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import "server-only";

import { groq } from "next-sanity";

import config from "@/config";
import { client } from "@/data/sanity/client";
import { env } from "@/env";

// Used in `generateStaticParams`
export function generateStaticPaths(types: string[]) {
return client
.withConfig({
token: config.sanity.token,
token: env.SANITY_API_TOKEN,
perspective: "published",
useCdn: false,
stega: false,
Expand Down
5 changes: 2 additions & 3 deletions apps/frontend/data/sanity/loadQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as queryStore from "@sanity/react-loader";
import { draftMode } from "next/headers";
import type { QueryParams } from "sanity";

import config from "@/config";
import { env } from "@/env";
import type {
BlogIndexPayload,
NotFoundPayload,
Expand All @@ -17,7 +17,6 @@ import type {
} from "@/types/object.types";
import {
AUTOMATION_PAGE_QUERY,
AUTOMATION_STORIES,
NOT_FOUND_DOC_QUERY,
ROUTE_QUERY,
buildBlogIndexQuery,
Expand All @@ -28,7 +27,7 @@ let serverClientSet = false;

function initClient() {
const serverClient = client.withConfig({
token: config.sanity.token,
token: env.SANITY_API_TOKEN,
stega: {
enabled: draftMode().isEnabled,
},
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/data/sanity/redirects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import config from "@/config";
import { client } from "@/data/sanity/client";
import { env } from "@/env";
import { groq } from "next-sanity";

export type RedirectSetting = {
Expand All @@ -9,7 +9,7 @@ export type RedirectSetting = {
};

const sanityClient = client.withConfig({
token: config.sanity.token,
token: env.SANITY_API_TOKEN,
perspective: "published",
useCdn: true,
stega: false,
Expand Down
6 changes: 3 additions & 3 deletions apps/frontend/data/sanity/resolveSanityRouteMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getFilterIcon,
getFilterSection,
} from "@/components/templates/Registry/helpers";
import config from "@/config";
import publicConfig from "@/config";
import { REGISTRY_FILTER_TYPES } from "@/constants";
import { env } from "@/env";
import {
Expand Down Expand Up @@ -90,7 +90,7 @@ export async function resolveSanityRouteMetadata(
const seo = data?.seo;

if (!seo) {
return { title: config.siteName };
return { title: publicConfig.siteName };
}

const parent = await parentPromise;
Expand All @@ -99,7 +99,7 @@ export async function resolveSanityRouteMetadata(
seo?.title ||
capitalize((data as CodemodPagePayload)?.automationName) ||
data.title ||
config.siteName;
publicConfig.siteName;

const canonicalUrl = seo?.canonicalUrl || pathToAbsUrl(data.pathname);

Expand Down
12 changes: 6 additions & 6 deletions apps/frontend/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { muxInput } from "sanity-plugin-mux-input";
import { structureTool } from "sanity/structure";

import StudioLogo from "@/components/shared/StudioLogo";
import config from "@/config";
import publicConfig from "@/config";

export default defineConfig({
basePath: config.sanity.studioUrl,
projectId: config.sanity.projectId,
dataset: config.sanity.dataset,
title: config.siteName,
basePath: publicConfig.sanity.studioUrl,
projectId: publicConfig.sanity.projectId,
dataset: publicConfig.sanity.dataset,
title: publicConfig.siteName,
icon: StudioLogo,
schema: {
types: schemas,
Expand Down Expand Up @@ -111,7 +111,7 @@ export default defineConfig({
}),
media(),
codeInput(),
visionTool({ defaultApiVersion: config.sanity.apiVersion }),
visionTool({ defaultApiVersion: publicConfig.sanity.apiVersion }),
muxInput({ mp4_support: "standard" }),
],
tools: (tools) => {
Expand Down
8 changes: 3 additions & 5 deletions apps/frontend/sanity/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import config from "@/config";
import publicConfig from "@/config";
import createImageUrlBuilder from "@sanity/image-url";
import type { Image } from "sanity";

const { dataset, projectId } = config.sanity;

export const imageBuilder = createImageUrlBuilder({
projectId: projectId || "",
dataset: dataset || "",
projectId: publicConfig.sanity.projectId ?? "",
dataset: publicConfig.sanity.dataset ?? "",
});

export const urlForImage = (source: Image | undefined) => {
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/utils/urls/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import config from "@/config";
import publicConfig from "@/config";
import { formatPath } from "@tinloof/sanity-web";

export function isExternalUrl(url: string) {
Expand All @@ -11,7 +11,7 @@ export function pathToAbsUrl(path?: string): string | undefined {
if (typeof path !== "string") return;

return (
config.baseUrl +
publicConfig.baseUrl +
// When creating absolute URLs, ensure the homepage doesn't have a trailing slash
(path === "/" ? "" : formatPath(path))
);
Expand Down

0 comments on commit 3057d7c

Please sign in to comment.