diff --git a/pages/concepts/[concept]/[param].tsx b/pages/concepts/[concept]/[param].tsx index 8aa547a8..c0f55c54 100644 --- a/pages/concepts/[concept]/[param].tsx +++ b/pages/concepts/[concept]/[param].tsx @@ -1,47 +1,19 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next"; +import { GetServerSidePropsContext } from "next/types"; -import { SettingsUtil } from "@/env"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Concept() { return null; } -export const getServerSideProps: GetServerSideProps = async ({ - params, - locale, -}) => { - const env = SettingsUtil.create(); - const { concept, param } = params || {}; - const curi = `${concept}/${param}`; - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_TERMS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - const entryUri = env.ENTRYSCAPE_TERMS_PATH.includes("sandbox") - ? `https://www-sandbox.dataportal.se/concepts/${curi}` - : `https://dataportal.se/concepts/${curi}`; - - const entry = await esu.getEntryByResourceURI(entryUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/concepts/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect(context, { + pathPrefix: "/concepts", + redirectPath: "/concepts", + entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", + paramName: "concept", + secondParamName: "param", + }); }; diff --git a/pages/concepts/[concept]/index.tsx b/pages/concepts/[concept]/index.tsx index 107b492e..9b17d878 100644 --- a/pages/concepts/[concept]/index.tsx +++ b/pages/concepts/[concept]/index.tsx @@ -1,12 +1,11 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next"; import { useRouter } from "next/router"; +import { GetServerSidePropsContext } from "next/types"; import { useContext } from "react"; -import { SettingsUtil } from "@/env"; import { ConceptPage } from "@/features/entryscape/concept-page"; import { EntrystoreProvider } from "@/providers/entrystore-provider"; import { SettingsContext } from "@/providers/settings-provider"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Concept() { const { env } = useContext(SettingsContext); @@ -29,48 +28,13 @@ export default function Concept() { ); } -export const getServerSideProps: GetServerSideProps = async ({ - params, - locale, -}) => { - const env = SettingsUtil.create(); - const { concept } = params || {}; - - if ( - typeof concept === "string" && - /\d/.test(concept) && - concept.includes("_") - ) { - return { props: {} }; - } - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_TERMS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - const entryUri = env.ENTRYSCAPE_TERMS_PATH.includes("sandbox") - ? `https://www-sandbox.dataportal.se/concepts/${concept}` - : `https://dataportal.se/concepts/${concept}`; - - const entry = await esu.getEntryByResourceURI(entryUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/concepts/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect(context, { + pathPrefix: "/concepts", + redirectPath: "/concepts", + entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", + paramName: "concept", + }); }; diff --git a/pages/externalconcept/[...concept].tsx b/pages/externalconcept/[...concept].tsx index 672a91b2..6e92c865 100644 --- a/pages/externalconcept/[...concept].tsx +++ b/pages/externalconcept/[...concept].tsx @@ -1,54 +1,18 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next/types"; +import { GetServerSidePropsContext } from "next/types"; -import { SettingsUtil } from "@/env"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Concept() { return null; } -export const getServerSideProps: GetServerSideProps = async ({ - params, - locale, -}) => { - const env = SettingsUtil.create(); - const concept = (params?.concept as string[]) || []; - const scheme = concept[0]; - - if (scheme != "http" && scheme != "https") { - return { - notFound: true, - }; - } - - // Reconstruct the original URI and redirect to the new format - const curi = concept.slice(1).join("/"); - const uri = `${scheme}://${curi}`; - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_TERMS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - - const entry = await esu.getEntryByResourceURI(uri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/concepts/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect(context, { + pathPrefix: "/concepts", + redirectPath: "/concepts", + entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", + paramName: "concept", + }); }; diff --git a/pages/externalconcept/index.tsx b/pages/externalconcept/index.tsx index eda02673..164a1492 100644 --- a/pages/externalconcept/index.tsx +++ b/pages/externalconcept/index.tsx @@ -1,43 +1,21 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next/types"; +import { GetServerSidePropsContext } from "next/types"; -import { SettingsUtil } from "@/env"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Concept() { return null; } -export const getServerSideProps: GetServerSideProps = async ({ - query: { resource }, - locale, -}) => { - const env = SettingsUtil.create(); - const resourceUri = decodeURIComponent(resource as string); - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_TERMS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - - const entry = await esu.getEntryByResourceURI(resourceUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/concepts/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect( + context, + { + pathPrefix: "/concepts", + redirectPath: "/concepts", + entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", + }, + context.query.resource as string, + ); }; diff --git a/pages/externalspecification/[...specification].tsx b/pages/externalspecification/[...specification].tsx index c264c7c6..44ea3802 100644 --- a/pages/externalspecification/[...specification].tsx +++ b/pages/externalspecification/[...specification].tsx @@ -1,54 +1,18 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next/types"; +import { GetServerSidePropsContext } from "next/types"; -import { SettingsUtil } from "@/env"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Specification() { return null; } -export const getServerSideProps: GetServerSideProps = async ({ - params, - locale, -}) => { - const env = SettingsUtil.create(); - const specification = (params?.specification as string[]) || []; - const scheme = specification[0]; - - if (scheme != "http" && scheme != "https") { - return { - notFound: true, - }; - } - - // Reconstruct the original URI and redirect to the new format - const curi = specification.slice(1).join("/"); - const entryUri = `${scheme}://${curi}`; - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_SPECS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - - const entry = await esu.getEntryByResourceURI(entryUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/specifications/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect(context, { + pathPrefix: "/specifications", + redirectPath: "/specifications", + entrystorePathKey: "ENTRYSCAPE_SPECS_PATH", + paramName: "specification", + }); }; diff --git a/pages/externalspecification/index.tsx b/pages/externalspecification/index.tsx index ea926f94..7ad7db53 100644 --- a/pages/externalspecification/index.tsx +++ b/pages/externalspecification/index.tsx @@ -1,43 +1,21 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next/types"; +import { GetServerSidePropsContext } from "next/types"; -import { SettingsUtil } from "@/env"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Specification() { return null; } -export const getServerSideProps: GetServerSideProps = async ({ - query: { resource }, - locale, -}) => { - const env = SettingsUtil.create(); - const resourceUri = decodeURIComponent(resource as string); - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_SPECS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - - const entry = await esu.getEntryByResourceURI(resourceUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/specifications/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect( + context, + { + pathPrefix: "/specifications", + redirectPath: "/specifications", + entrystorePathKey: "ENTRYSCAPE_SPECS_PATH", + }, + context.query.resource as string, + ); }; diff --git a/pages/externalterminology/[...terminology].tsx b/pages/externalterminology/[...terminology].tsx index 1dd8cb95..bfae1354 100644 --- a/pages/externalterminology/[...terminology].tsx +++ b/pages/externalterminology/[...terminology].tsx @@ -1,54 +1,18 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next"; +import { GetServerSidePropsContext } from "next/types"; -import { SettingsUtil } from "@/env"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Terminology() { return null; } -export const getServerSideProps: GetServerSideProps = async ({ - params, - locale, -}) => { - const env = SettingsUtil.create(); - const terminology = (params?.terminology as string[]) || []; - const scheme = terminology[0]; - - if (scheme != "http" && scheme != "https") { - return { - notFound: true, - }; - } - - // Reconstruct the original URI and redirect to the new format - const curi = terminology.slice(1).join("/"); - const entryUri = `${scheme}://${curi}`; - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_TERMS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - - const entry = await esu.getEntryByResourceURI(entryUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/terminology/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect(context, { + pathPrefix: "/concepts", + redirectPath: "/terminology", + entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", + paramName: "terminology", + }); }; diff --git a/pages/externalterminology/index.tsx b/pages/externalterminology/index.tsx index b7a867f3..bf0a1fd8 100644 --- a/pages/externalterminology/index.tsx +++ b/pages/externalterminology/index.tsx @@ -1,43 +1,21 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next"; +import { GetServerSidePropsContext } from "next/types"; -import { SettingsUtil } from "@/env"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Terminology() { return null; } -export const getServerSideProps: GetServerSideProps = async ({ - query: { resource }, - locale, -}) => { - const env = SettingsUtil.create(); - const resourceUri = decodeURIComponent(resource as string); - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_TERMS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - - const entry = await esu.getEntryByResourceURI(resourceUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/terminology/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect( + context, + { + pathPrefix: "/concepts", + redirectPath: "/terminology", + entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", + }, + context.query.resource as string, + ); }; diff --git a/pages/specifications/[spec]/[param].tsx b/pages/specifications/[spec]/[param].tsx index 2f8762fd..d322f107 100644 --- a/pages/specifications/[spec]/[param].tsx +++ b/pages/specifications/[spec]/[param].tsx @@ -1,47 +1,19 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next"; +import { GetServerSidePropsContext } from "next/types"; -import { SettingsUtil } from "@/env"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Specification() { return null; } -export const getServerSideProps: GetServerSideProps = async ({ - params, - locale, -}) => { - const env = SettingsUtil.create(); - const { spec, param } = params || {}; - const curi = `${spec}/${param}`; - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_SPECS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - const entryUri = env.ENTRYSCAPE_SPECS_PATH.includes("sandbox") - ? `https://www-sandbox.dataportal.se/specifications/${curi}` - : `https://dataportal.se/specifications/${curi}`; - - const entry = await esu.getEntryByResourceURI(entryUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/specifications/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect(context, { + pathPrefix: "/specifications", + redirectPath: "/specifications", + entrystorePathKey: "ENTRYSCAPE_SPECS_PATH", + paramName: "spec", + secondParamName: "param", + }); }; diff --git a/pages/specifications/[spec]/index.tsx b/pages/specifications/[spec]/index.tsx index f5842bdf..ab23a50d 100644 --- a/pages/specifications/[spec]/index.tsx +++ b/pages/specifications/[spec]/index.tsx @@ -1,12 +1,11 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next"; import { useRouter } from "next/router"; +import { GetServerSidePropsContext } from "next/types"; import { useContext } from "react"; -import { SettingsUtil } from "@/env"; import { SpecificationPage } from "@/features/entryscape/specification-page"; import { EntrystoreProvider } from "@/providers/entrystore-provider"; import { SettingsContext } from "@/providers/settings-provider"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Specification() { const { env } = useContext(SettingsContext); @@ -31,44 +30,13 @@ export default function Specification() { } } -export const getServerSideProps: GetServerSideProps = async ({ - params, - locale, -}) => { - const env = SettingsUtil.create(); - const { spec } = params || {}; - - if (typeof spec === "string" && /\d/.test(spec) && spec.includes("_")) { - return { props: {} }; - } - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_SPECS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - const entryUri = env.ENTRYSCAPE_SPECS_PATH.includes("sandbox") - ? `https://www-sandbox.dataportal.se/specifications/${spec}` - : `https://dataportal.se/specifications/${spec}`; - - const entry = await esu.getEntryByResourceURI(entryUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/specifications/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect(context, { + pathPrefix: "/specifications", + redirectPath: "/specifications", + entrystorePathKey: "ENTRYSCAPE_SPECS_PATH", + paramName: "spec", + }); }; diff --git a/pages/terminology/[term]/[param].tsx b/pages/terminology/[term]/[param].tsx index 02863bff..ead52a15 100644 --- a/pages/terminology/[term]/[param].tsx +++ b/pages/terminology/[term]/[param].tsx @@ -1,47 +1,19 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next/types"; +import { GetServerSidePropsContext } from "next/types"; -import { SettingsUtil } from "@/env"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Terminology() { return null; } -export const getServerSideProps: GetServerSideProps = async ({ - params, - locale, -}) => { - const env = SettingsUtil.create(); - const { terminology, param } = params || {}; - const curi = `${terminology}/${param}`; - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_SPECS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - const entryUri = env.ENTRYSCAPE_TERMS_PATH.includes("sandbox") - ? `https://www-sandbox.dataportal.se/concepts/${curi}` - : `https://dataportal.se/concepts/${curi}`; - - const entry = await esu.getEntryByResourceURI(entryUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/terminology/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect(context, { + pathPrefix: "/concepts", + redirectPath: "/terminology", + entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", + paramName: "term", + secondParamName: "param", + }); }; diff --git a/pages/terminology/[term]/index.tsx b/pages/terminology/[term]/index.tsx index b284414b..4400a921 100644 --- a/pages/terminology/[term]/index.tsx +++ b/pages/terminology/[term]/index.tsx @@ -1,12 +1,11 @@ -import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; -import { GetServerSideProps } from "next"; +import { GetServerSidePropsContext } from "next"; import { useRouter } from "next/router"; import { useContext } from "react"; -import { SettingsUtil } from "@/env"; import { ConceptPage } from "@/features/entryscape/concept-page"; import { EntrystoreProvider } from "@/providers/entrystore-provider"; import { SettingsContext } from "@/providers/settings-provider"; +import { handleEntryStoreRedirect } from "@/utilities/entryscape/entrystore-redirect"; export default function Terminology() { const { env } = useContext(SettingsContext); @@ -29,44 +28,13 @@ export default function Terminology() { ); } -export const getServerSideProps: GetServerSideProps = async ({ - params, - locale, -}) => { - const env = SettingsUtil.create(); - const { term } = params || {}; - - if (typeof term === "string" && /\d/.test(term) && term.includes("_")) { - return { props: {} }; - } - - try { - const es = new EntryStore( - `https://${env.ENTRYSCAPE_TERMS_PATH}/store` || - "https://admin.dataportal.se/store", - ); - const esu = new EntryStoreUtil(es); - const entryUri = env.ENTRYSCAPE_TERMS_PATH.includes("sandbox") - ? `https://www-sandbox.dataportal.se/concepts/${term}` - : `https://dataportal.se/concepts/${term}`; - - const entry = await esu.getEntryByResourceURI(entryUri); - - if (entry) { - return { - redirect: { - destination: `/${locale}/terminology/${entry - .getContext() - .getId()}_${entry.getId()}`, - permanent: true, // This creates a 301 redirect - }, - }; - } - } catch (error) { - console.error("Error fetching entry:", error); - } - - return { - notFound: true, - }; +export const getServerSideProps = async ( + context: GetServerSidePropsContext, +) => { + return handleEntryStoreRedirect(context, { + pathPrefix: "/concepts", + redirectPath: "/terminology", + entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", + paramName: "term", + }); }; diff --git a/utilities/entryscape/entrystore-redirect.ts b/utilities/entryscape/entrystore-redirect.ts new file mode 100644 index 00000000..a2767fc9 --- /dev/null +++ b/utilities/entryscape/entrystore-redirect.ts @@ -0,0 +1,76 @@ +import { EntryStore, EntryStoreUtil } from "@entryscape/entrystore-js"; +import { GetServerSidePropsContext } from "next"; + +import { SettingsUtil } from "@/env"; + +type RedirectConfig = { + pathPrefix: string; + redirectPath: "/concepts" | "/specifications" | "/terminology"; + entrystorePathKey: "ENTRYSCAPE_TERMS_PATH" | "ENTRYSCAPE_SPECS_PATH"; + paramName?: string; + secondParamName?: string; +}; + +export async function handleEntryStoreRedirect( + context: GetServerSidePropsContext, + config: RedirectConfig, + resourceUri?: string, +) { + const { locale, params } = context; + const env = SettingsUtil.create(); + + // Handle catch-all routes ([...param]) + const param = params?.[config.paramName || ""]; + if (Array.isArray(param)) { + const scheme = param[0]; + if (scheme !== "http" && scheme !== "https") { + return { notFound: true }; + } + + const curi = param.slice(1).join("/"); + resourceUri = `${scheme}://${curi}`; + } + // Handle regular routes + else if (param) { + if (/\d/.test(param) && param.includes("_")) { + return { props: {} }; + } + + // Construct resourceUri based on number of parameters + const baseUrl = env[config.entrystorePathKey].includes("sandbox") + ? "https://www-sandbox.dataportal.se" + : "https://dataportal.se"; + + const pathSuffix = + config.secondParamName && params?.[config.secondParamName] + ? `${param}/${params[config.secondParamName]}` // Two params + : param; // Single param + + resourceUri = `${baseUrl}${config.pathPrefix}/${pathSuffix}`; + } + + try { + const es = new EntryStore( + `https://${env[config.entrystorePathKey]}/store` || + "https://admin.dataportal.se/store", + ); + const esu = new EntryStoreUtil(es); + + const entry = await esu.getEntryByResourceURI(resourceUri || ""); + + if (entry) { + return { + redirect: { + destination: `/${locale}${config.redirectPath}/${entry + .getContext() + .getId()}_${entry.getId()}`, + permanent: true, + }, + }; + } + } catch (error) { + console.error("Error fetching entry:", error); + } + + return { notFound: true }; +}