Enhance Open Graph support by adding meta data retrieval and updating robots.txt for better SEO

This commit is contained in:
Marco Pedone 2025-08-08 13:30:21 +02:00
parent a6fa1b960b
commit 7bfef22f80
4 changed files with 72 additions and 18 deletions

View file

@ -1,4 +1,7 @@
User-agent: * User-agent: *
Allow: /api/og/* Allow: /
Disallow: Disallow: /api/
Disallow: /area-riservata/
# Crawl delay (optional - adjust based on your server capacity)
Crawl-delay: 1

View file

@ -61,10 +61,17 @@ import type { Annunci } from "~/schemas/public/Annunci";
type AnnuncioProps = { type AnnuncioProps = {
cod: string; cod: string;
flag: string; flag: string;
meta: {
ogImage: string;
title: string;
description: string;
ogUrl: string;
};
}; };
const AnnuncioDettaglio: NextPage<AnnuncioProps> = ({ const AnnuncioDettaglio: NextPage<AnnuncioProps> = ({
cod, cod,
flag, flag,
meta,
}: AnnuncioProps) => { }: AnnuncioProps) => {
const session = useSession(); const session = useSession();
const { locale } = useTranslation(); const { locale } = useTranslation();
@ -99,21 +106,15 @@ const AnnuncioDettaglio: NextPage<AnnuncioProps> = ({
<TouchProvider> <TouchProvider>
<main> <main>
<Head> <Head>
<title>{`${cod} | ${titolo}`}</title> <title>{`${cod} | ${meta.title}`}</title>
<meta property="description" content={description} /> <meta property="description" content={meta.description} />
<meta property="og:url" content={window.location.href} /> <meta property="og:url" content={meta.ogUrl} />
<meta property="og:type" content="website" /> <meta property="og:type" content="website" />
<meta property="og:title" content={`${cod} | ${titolo}`} /> <meta property="og:title" content={`${cod} | ${meta.title}`} />
<meta property="og:description" content={description} /> <meta property="og:description" content={meta.description} />
{/* <meta <meta property="og:image" content={meta.ogImage} />
property="og:image" <meta property="og:image:width" content="1200" />
content={ <meta property="og:image:height" content="630" />
data.url_immagini
? `${env.NEXT_PUBLIC_BASE_URL}${data.url_immagini[0]}`
: ""
}
/> */}
<meta property="og:image" content={data.og_url || ""} />
<meta <meta
property="og:logo" property="og:logo"
content={`${env.NEXT_PUBLIC_BASE_URL}/Infoalloggi.png`} content={`${env.NEXT_PUBLIC_BASE_URL}/Infoalloggi.png`}
@ -192,8 +193,15 @@ export async function getStaticProps(
) { ) {
const ssg = generateSSGHelper(); const ssg = generateSSGHelper();
const cod = context.params?.cod; const cod = context.params?.cod;
if (typeof cod !== "string") throw new Error("no cod"); if (typeof cod !== "string") {
return {
notFound: true,
};
}
await ssg.annunci.getAnnuncio.prefetch({ cod: cod }); await ssg.annunci.getAnnuncio.prefetch({ cod: cod });
const meta = await ssg.annunci.getAnnuncioMeta.fetch({ cod: cod });
const flag = await ssg.settings.GetFlagValue.fetch({ const flag = await ssg.settings.GetFlagValue.fetch({
id: "ANNUNCIO_INTERACTIONS_DISABLED", id: "ANNUNCIO_INTERACTIONS_DISABLED",
}); });
@ -202,6 +210,7 @@ export async function getStaticProps(
trpcState: ssg.dehydrate(), trpcState: ssg.dehydrate(),
cod, cod,
flag, flag,
meta,
}, },
revalidate: 1, revalidate: 1,
}; };

View file

@ -16,6 +16,7 @@ import {
getCursor_AnnunciHandler, getCursor_AnnunciHandler,
get_AnnunciPositionsHandler, get_AnnunciPositionsHandler,
getAnnunciById, getAnnunciById,
getAnnunciMetaByCod,
} from "~/server/controllers/annunci.controller"; } from "~/server/controllers/annunci.controller";
import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types"; import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types";
@ -35,6 +36,15 @@ export const annunciRouter = createTRPCRouter({
.query(async ({ input }) => { .query(async ({ input }) => {
return await getAnnunciByCod({ ...input }); return await getAnnunciByCod({ ...input });
}), }),
getAnnuncioMeta: publicProcedure
.input(
z.object({
cod: z.string(),
}),
)
.query(async ({ input }) => {
return await getAnnunciMetaByCod({ ...input });
}),
getAnnuncioById: publicProcedure getAnnuncioById: publicProcedure
.input( .input(
z.object({ z.object({

View file

@ -1,11 +1,12 @@
import type { Annunci, AnnunciId } from "~/schemas/public/Annunci"; import type { Annunci, AnnunciId } from "~/schemas/public/Annunci";
import { TRPCError } from "@trpc/server"; 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 { AnnuncioObjectWithImages } from "~/server/services/annunci.service";
import { zAnnuncioId } from "~/server/utils/zod_types"; import { zAnnuncioId } from "~/server/utils/zod_types";
import { z } from "zod/v4"; import { z } from "zod/v4";
import { db } from "~/server/db"; import { db } from "~/server/db";
import type { ServizioServizioId } from "~/schemas/public/Servizio"; import type { ServizioServizioId } from "~/schemas/public/Servizio";
import { env } from "~/env.mjs";
// const ratelimit = new RateLimiterHandler({ // const ratelimit = new RateLimiterHandler({
// windowSize: 10, // 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 ({ export const getAnnunciById = async ({
id, id,