diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..72e9aa4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +Dockerfile +.dockerignore +node_modules +npm-debug.log +README.md +.next +.git \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..84ffe76 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,74 @@ +FROM node:18-alpine AS base + +# Install dependencies only when needed +FROM base AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +WORKDIR /app + +# Install dependencies based on the preferred package manager +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ +RUN \ + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi + + +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +## WORKAROUND: +RUN rm -rf src/app/~/dash +RUN rm -rf src/app/actions + +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +# Uncomment the following line in case you want to disable telemetry during the build. +# ENV NEXT_TELEMETRY_DISABLED=1 + +RUN npx prisma generate + +RUN \ + if [ -f yarn.lock ]; then yarn run build; \ + elif [ -f package-lock.json ]; then npm run build; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ + else echo "Lockfile not found." && exit 1; \ + fi + +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV=production +# Uncomment the following line in case you want to disable telemetry during runtime. +# ENV NEXT_TELEMETRY_DISABLED=1 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +# Set the correct permission for prerender cache +RUN mkdir .next +RUN chown nextjs:nodejs .next + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs + +EXPOSE 3000 + +ENV PORT=3000 + +# server.js is created by next build from the standalone output +# https://nextjs.org/docs/pages/api-reference/next-config-js/output +ENV HOSTNAME="0.0.0.0" +CMD ["node", "server.js"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 0bbe83b..3e238b2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,4 +9,8 @@ services: - "5432:5432" volumes: - ./.vol_db:/var/lib/postgresql/data - \ No newline at end of file + + web: + build: . + ports: + - 3000:3000 \ No newline at end of file diff --git a/next.config.mjs b/next.config.mjs index 0f3965a..681efdc 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -11,7 +11,8 @@ const nextConfig = { hostname: 'logo.clearbit.com', } ] - } + }, + output: "standalone", }; diff --git a/prisma/prisma.ts b/prisma/prisma.ts index 20e4d98..54434e0 100644 --- a/prisma/prisma.ts +++ b/prisma/prisma.ts @@ -5,10 +5,10 @@ let prisma: PrismaClient; if (process.env.NODE_ENV === 'production') { prisma = new PrismaClient(); } else { - if (!global.prisma) { - global.prisma = new PrismaClient(); + if (!(global as any).prisma) { + (global as any).prisma = new PrismaClient(); } - prisma = global.prisma; + prisma = (global as any).prisma; } export default prisma; \ No newline at end of file diff --git a/src/actions/block_actions.ts b/src/actions/block_actions.ts index 757d962..318fa27 100644 --- a/src/actions/block_actions.ts +++ b/src/actions/block_actions.ts @@ -68,7 +68,7 @@ export async function createLinkBlock(prevState: any, formData: FormData) { }; } - revalidatePath("/dash"); + revalidatePath("/~/dash"); revalidatePath(`/~/dash/${data.data.customPageUuid}/page`); revalidatePath(`/${customPage.page.shortcode}`); @@ -99,7 +99,7 @@ export async function deleteLinkBlock(prevState: any, formData: FormData) { } }); - revalidatePath("/dash"); + revalidatePath("/~/dash"); return { message: "Link block deleted" diff --git a/src/actions/page_actions.ts b/src/actions/page_actions.ts index 7206d3c..50843d4 100644 --- a/src/actions/page_actions.ts +++ b/src/actions/page_actions.ts @@ -3,7 +3,6 @@ import { z } from "zod"; import prisma from "../../prisma/prisma"; import { revalidatePath } from "next/cache"; -import { error } from "console"; export async function createCustomPage(prevState: any, fromData: FormData) { const schema = z.object({ @@ -49,12 +48,13 @@ export async function createCustomPage(prevState: any, fromData: FormData) { return { message: "Shortcode already exists", error: { - shortcode: "Shortcode already exists" + shortcode: ["Shortcode already exists"] + } }; } - revalidatePath("/dash"); + revalidatePath("/~/dash"); return { message: "Page created", }; @@ -93,7 +93,7 @@ export async function createRedirectPage(prevState: any, fromData: FormData) { } }); - revalidatePath("/dash"); + revalidatePath("/~/dash"); return { message: "URL created", @@ -141,7 +141,7 @@ export async function deletePage(prevState: any, fromData: FormData) { } }); - revalidatePath("/dash"); + revalidatePath("/~/dash"); return { message: "Page deleted", @@ -178,7 +178,7 @@ export async function pinPageToRoot(prevState: any, fromData: FormData) { } }); - revalidatePath("/dash"); + revalidatePath("/~/dash"); revalidatePath("/"); return { diff --git a/src/app/[shortcode]/page.tsx b/src/app/[shortcode]/page.tsx index f93efd6..8784470 100644 --- a/src/app/[shortcode]/page.tsx +++ b/src/app/[shortcode]/page.tsx @@ -3,8 +3,9 @@ import { headers } from "next/headers"; import { notFound, redirect, RedirectType } from "next/navigation"; import { sha256 } from "js-sha256"; import CustomPageComponent from "@/components/CustomPageComponent"; +import { PageProps } from "../../../.next/types/app/layout"; -export default async function RedirectToUrl({ params }: { params: { shortcode: string } }) { +export default async function RedirectToUrl({ params }: PageProps) { const { shortcode } = await params; @@ -21,7 +22,7 @@ export default async function RedirectToUrl({ params }: { params: { shortcode: s return notFound() } - // Logg the visit + // Log the visit const requestHeaders = await headers(); const userAgent = requestHeaders.get('user-agent') || 'unknown'; const ip = requestHeaders.get('x-forwarded-for') || 'unknown'; diff --git a/src/app/~/dash/[uuid]/page.tsx b/src/app/~/dash/[uuid]/page.tsx index 3ddb87a..09aa8ba 100644 --- a/src/app/~/dash/[uuid]/page.tsx +++ b/src/app/~/dash/[uuid]/page.tsx @@ -8,7 +8,13 @@ import LinkFavicon from "@/components/LinkFavicon"; import { hashToColor } from "@/utils"; import PinPageToRootForm from "@/components/forms/pinPageToRootForm"; -export default async function DetailedStats({ params }: { params: { uuid: string } }) { +interface PageProps { + params: Promise<{ + uuid: string + }> +} + +export default async function DetailedStats({ params } : PageProps) { const { uuid } = await params; diff --git a/src/components/forms/createCustomPageForm.tsx b/src/components/forms/createCustomPageForm.tsx index 9ac7eac..012753c 100644 --- a/src/components/forms/createCustomPageForm.tsx +++ b/src/components/forms/createCustomPageForm.tsx @@ -3,6 +3,7 @@ import { createCustomPage } from "@/actions/page_actions"; import { useActionState } from "react"; import SubmitButton from "../SubmitButton"; + export default function CreateCustomPageForm() { const initialState = {