From 7bfef22f803b7ac7bce09c767b1db562f8e0b708 Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Fri, 8 Aug 2025 13:30:21 +0200 Subject: [PATCH] Enhance Open Graph support by adding meta data retrieval and updating robots.txt for better SEO --- apps/infoalloggi/public/robots.txt | 7 +++- apps/infoalloggi/src/pages/annuncio/[cod].tsx | 39 ++++++++++++------- .../src/server/api/routers/annunci.ts | 10 +++++ .../server/controllers/annunci.controller.ts | 34 +++++++++++++++- 4 files changed, 72 insertions(+), 18 deletions(-) diff --git a/apps/infoalloggi/public/robots.txt b/apps/infoalloggi/public/robots.txt index 9aa1e51..80ddc38 100644 --- a/apps/infoalloggi/public/robots.txt +++ b/apps/infoalloggi/public/robots.txt @@ -1,4 +1,7 @@ User-agent: * -Allow: /api/og/* -Disallow: +Allow: / +Disallow: /api/ +Disallow: /area-riservata/ +# Crawl delay (optional - adjust based on your server capacity) +Crawl-delay: 1 \ No newline at end of file diff --git a/apps/infoalloggi/src/pages/annuncio/[cod].tsx b/apps/infoalloggi/src/pages/annuncio/[cod].tsx index 59a8680..ca99c97 100644 --- a/apps/infoalloggi/src/pages/annuncio/[cod].tsx +++ b/apps/infoalloggi/src/pages/annuncio/[cod].tsx @@ -61,10 +61,17 @@ import type { Annunci } from "~/schemas/public/Annunci"; type AnnuncioProps = { cod: string; flag: string; + meta: { + ogImage: string; + title: string; + description: string; + ogUrl: string; + }; }; const AnnuncioDettaglio: NextPage = ({ cod, flag, + meta, }: AnnuncioProps) => { const session = useSession(); const { locale } = useTranslation(); @@ -99,21 +106,15 @@ const AnnuncioDettaglio: NextPage = ({
- {`${cod} | ${titolo}`} - - + {`${cod} | ${meta.title}`} + + - - - {/* */} - + + + + + { return await getAnnunciByCod({ ...input }); }), + getAnnuncioMeta: publicProcedure + .input( + z.object({ + cod: z.string(), + }), + ) + .query(async ({ input }) => { + return await getAnnunciMetaByCod({ ...input }); + }), getAnnuncioById: publicProcedure .input( z.object({ diff --git a/apps/infoalloggi/src/server/controllers/annunci.controller.ts b/apps/infoalloggi/src/server/controllers/annunci.controller.ts index f92efd4..d734eef 100644 --- a/apps/infoalloggi/src/server/controllers/annunci.controller.ts +++ b/apps/infoalloggi/src/server/controllers/annunci.controller.ts @@ -1,11 +1,12 @@ import type { Annunci, AnnunciId } from "~/schemas/public/Annunci"; import { TRPCError } from "@trpc/server"; -import { createSrcset } from "~/server/services/imageServer"; +import { createSrc, createSrcset } from "~/server/services/imageServer"; import { AnnuncioObjectWithImages } from "~/server/services/annunci.service"; import { zAnnuncioId } from "~/server/utils/zod_types"; import { z } from "zod/v4"; import { db } from "~/server/db"; import type { ServizioServizioId } from "~/schemas/public/Servizio"; +import { env } from "~/env.mjs"; // const ratelimit = new RateLimiterHandler({ // windowSize: 10, @@ -88,6 +89,37 @@ export const getAnnunciByCod = async ({ }); } }; +export const getAnnunciMetaByCod = async ({ cod }: { cod: string }) => { + try { + const annuncio = await db + .selectFrom("annunci") + .select(["titolo_it", "desc_it", "url_immagini", "og_url"]) + .where("annunci.web", "=", true) + .where("codice", "=", cod) + .executeTakeFirst(); + if (!annuncio) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "Annuncio non trovato", + }); + } + + return { + ogImage: + annuncio.url_immagini && annuncio.url_immagini[0] + ? createSrc(annuncio.url_immagini[0]) + : annuncio.og_url || "", + title: annuncio.titolo_it || "", + description: annuncio.desc_it || "", + ogUrl: env.NEXT_PUBLIC_BASE_URL + "/annuncio/" + cod, + }; + } catch { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: "Errore query getAnnuncio", + }); + } +}; export const getAnnunciById = async ({ id,