feat: add compatibility patch for query parameters in getServerSideProps
This commit is contained in:
parent
5829a45718
commit
e1f7c888f3
2 changed files with 43 additions and 1 deletions
|
|
@ -39,6 +39,7 @@ import { cn, debounce } from "~/lib/utils";
|
||||||
import { useTranslation } from "~/providers/I18nProvider";
|
import { useTranslation } from "~/providers/I18nProvider";
|
||||||
import {
|
import {
|
||||||
type AnnunciOptions,
|
type AnnunciOptions,
|
||||||
|
compatibilityPatch,
|
||||||
RicercaProvider,
|
RicercaProvider,
|
||||||
type SortTypes,
|
type SortTypes,
|
||||||
useRicerca,
|
useRicerca,
|
||||||
|
|
@ -93,9 +94,21 @@ Annunci.getLayout = (page: React.ReactNode) => {
|
||||||
return <Layout>{page}</Layout>;
|
return <Layout>{page}</Layout>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getServerSideProps = (async () => {
|
|
||||||
|
|
||||||
|
export const getServerSideProps = (async (ctx) => {
|
||||||
const helper = generateSSGHelper();
|
const helper = generateSSGHelper();
|
||||||
const options = await helper.annunci.getAnnunciOptions.fetch();
|
const options = await helper.annunci.getAnnunciOptions.fetch();
|
||||||
|
const {patched, query} = compatibilityPatch(ctx.query);
|
||||||
|
if (patched){
|
||||||
|
const queryString = new URLSearchParams(query as Record<string, string>).toString();
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: `/annunci?${queryString}`,
|
||||||
|
permanent: false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
options,
|
options,
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import {
|
||||||
parseAsStringLiteral,
|
parseAsStringLiteral,
|
||||||
useQueryState,
|
useQueryState,
|
||||||
} from "nuqs";
|
} from "nuqs";
|
||||||
|
import type { ParsedUrlQuery } from "querystring";
|
||||||
import {
|
import {
|
||||||
createContext,
|
createContext,
|
||||||
type FC,
|
type FC,
|
||||||
|
|
@ -228,3 +229,31 @@ export const RicercaProvider: FC<{
|
||||||
</RicercaContext.Provider>
|
</RicercaContext.Provider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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 };
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue