From e1f7c888f3558b07a5d04132bf70027f55f7e86d Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Tue, 30 Dec 2025 09:59:39 +0100 Subject: [PATCH] feat: add compatibility patch for query parameters in getServerSideProps --- apps/infoalloggi/src/pages/annunci.tsx | 15 +++++++++- .../src/providers/RicercaProvider.tsx | 29 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/apps/infoalloggi/src/pages/annunci.tsx b/apps/infoalloggi/src/pages/annunci.tsx index f5458c6..b1baea7 100644 --- a/apps/infoalloggi/src/pages/annunci.tsx +++ b/apps/infoalloggi/src/pages/annunci.tsx @@ -39,6 +39,7 @@ import { cn, debounce } from "~/lib/utils"; import { useTranslation } from "~/providers/I18nProvider"; import { type AnnunciOptions, + compatibilityPatch, RicercaProvider, type SortTypes, useRicerca, @@ -93,9 +94,21 @@ Annunci.getLayout = (page: React.ReactNode) => { return {page}; }; -export const getServerSideProps = (async () => { + + +export const getServerSideProps = (async (ctx) => { const helper = generateSSGHelper(); const options = await helper.annunci.getAnnunciOptions.fetch(); + const {patched, query} = compatibilityPatch(ctx.query); + if (patched){ + const queryString = new URLSearchParams(query as Record).toString(); + return { + redirect: { + destination: `/annunci?${queryString}`, + permanent: false, + }, + }; + } return { props: { options, diff --git a/apps/infoalloggi/src/providers/RicercaProvider.tsx b/apps/infoalloggi/src/providers/RicercaProvider.tsx index 952c3b2..6355c63 100644 --- a/apps/infoalloggi/src/providers/RicercaProvider.tsx +++ b/apps/infoalloggi/src/providers/RicercaProvider.tsx @@ -7,6 +7,7 @@ import { parseAsStringLiteral, useQueryState, } from "nuqs"; +import type { ParsedUrlQuery } from "querystring"; import { createContext, type FC, @@ -228,3 +229,31 @@ export const RicercaProvider: FC<{ ); }; + +const acceptedParams = ["m", "t", "c", "f", "bmin", "bmax", "s"]; +const patchedParams = { + map: "m", + tipo: "t", + comune: "c", + consegna: "f", + minBudget: "bmin", + maxBudget: "bmax", + sort: "s", +}; +export const compatibilityPatch = (query: ParsedUrlQuery) => { + let patched = false; + for (const key of Object.keys(query)) { + if (!acceptedParams.includes(key)) { + if (Object.keys(patchedParams).includes(key)) { + const mappedKey = patchedParams[key as keyof typeof patchedParams]; + if (mappedKey) { + const value = query[key]; + delete query[key]; + query[mappedKey] = value; + patched = true; + } + } + } + } + return { patched, query }; +};