diff --git a/pages/api/entrystore-redirect.ts b/pages/api/entrystore-redirect.ts deleted file mode 100644 index f7f36329d..000000000 --- a/pages/api/entrystore-redirect.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { NextApiRequest, NextApiResponse } from "next"; - -import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - const { config, resourceUri, locale, isSandbox } = req.body; - - try { - const result = await handleEntryStoreRedirect( - config, - locale, - isSandbox, - resourceUri, - ); - - if (result.notFound) { - return res.status(404).json(result); - } else { - return res.status(200).json(result); - } - } catch (error) { - console.error(error); - return res.status(500).json({ error: "Internal Server Error" }); - } -} diff --git a/pages/concepts/[concept]/[param].tsx b/pages/concepts/[concept]/[param].tsx index 75322a9a2..c2e1016cc 100644 --- a/pages/concepts/[concept]/[param].tsx +++ b/pages/concepts/[concept]/[param].tsx @@ -4,7 +4,7 @@ import { useContext, useEffect, useState } from "react"; import { ConceptPage } from "@/features/entryscape/concept-page"; import { EntrystoreProvider } from "@/providers/entrystore-provider"; import { SettingsContext } from "@/providers/settings-provider"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Concept() { const { env } = useContext(SettingsContext); @@ -17,19 +17,18 @@ export default function Concept() { if (!concept || !param) return; const isSandbox = window.location.host.includes("sandbox"); - const data = await getEntryStoreProps({ - config: { + const data = await handleEntryStoreRedirect( + { pathPrefix: "/concepts", redirectPath: "/concepts", entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", param: concept, secondParam: param as string, }, - locale: router.locale || "sv", - isSandbox, router, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + ); if (data?.resourceUri) { setResourceUri(data.resourceUri); diff --git a/pages/concepts/[concept]/index.tsx b/pages/concepts/[concept]/index.tsx index 1ed78e806..ce0370295 100644 --- a/pages/concepts/[concept]/index.tsx +++ b/pages/concepts/[concept]/index.tsx @@ -4,7 +4,7 @@ import { useContext, useEffect, useState } from "react"; import { ConceptPage } from "@/features/entryscape/concept-page"; import { EntrystoreProvider } from "@/providers/entrystore-provider"; import { SettingsContext } from "@/providers/settings-provider"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Concept() { const { env } = useContext(SettingsContext); @@ -18,18 +18,17 @@ export default function Concept() { if (!concept) return; const isSandbox = window.location.host.includes("sandbox"); - const data = await getEntryStoreProps({ - config: { + const data = await handleEntryStoreRedirect( + { pathPrefix: "/concepts", redirectPath: "/concepts", entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", param: concept, }, - locale: router.locale || "sv", - isSandbox, router, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + ); if (data?.resourceUri) { setResourceUri(data.resourceUri); diff --git a/pages/externalconcept/[...concept].tsx b/pages/externalconcept/[...concept].tsx index 6bd11a008..b002e2d45 100644 --- a/pages/externalconcept/[...concept].tsx +++ b/pages/externalconcept/[...concept].tsx @@ -1,7 +1,7 @@ import { useRouter } from "next/router"; import { useEffect } from "react"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Concept() { const router = useRouter(); @@ -12,18 +12,17 @@ export default function Concept() { if (!concept) return; const isSandbox = window.location.host.includes("sandbox"); - await getEntryStoreProps({ - config: { + await handleEntryStoreRedirect( + { pathPrefix: "/concepts", redirectPath: "/concepts", entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", param: concept, }, - locale: router.locale || "sv", - isSandbox, router, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + ); }; fetchEntryStoreProps(); diff --git a/pages/externalconcept/index.tsx b/pages/externalconcept/index.tsx index 2e5a36e0d..65ae49936 100644 --- a/pages/externalconcept/index.tsx +++ b/pages/externalconcept/index.tsx @@ -1,7 +1,7 @@ import { useRouter } from "next/router"; import { useEffect } from "react"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Concept() { const router = useRouter(); @@ -12,18 +12,17 @@ export default function Concept() { if (!resource) return; const isSandbox = window.location.host.includes("sandbox"); - await getEntryStoreProps({ - config: { + await handleEntryStoreRedirect( + { pathPrefix: "/concepts", redirectPath: "/concepts", entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", }, - locale: router.locale || "sv", - isSandbox, router, - resourceUri: resource as string, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + resource as string, + ); }; fetchEntryStoreProps(); diff --git a/pages/externalspecification/[...specification].tsx b/pages/externalspecification/[...specification].tsx index 5ff031479..024e663b8 100644 --- a/pages/externalspecification/[...specification].tsx +++ b/pages/externalspecification/[...specification].tsx @@ -1,7 +1,7 @@ import { useRouter } from "next/router"; import { useEffect } from "react"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Specification() { const router = useRouter(); @@ -12,18 +12,17 @@ export default function Specification() { if (!specification) return; const isSandbox = window.location.host.includes("sandbox"); - await getEntryStoreProps({ - config: { + await handleEntryStoreRedirect( + { pathPrefix: "/concepts", redirectPath: "/concepts", entrystorePathKey: "ENTRYSCAPE_SPECS_PATH", param: specification, }, - locale: router.locale || "sv", - isSandbox, router, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + ); }; fetchEntryStoreProps(); diff --git a/pages/externalspecification/index.tsx b/pages/externalspecification/index.tsx index a46884265..875271594 100644 --- a/pages/externalspecification/index.tsx +++ b/pages/externalspecification/index.tsx @@ -1,7 +1,7 @@ import { useRouter } from "next/router"; import { useEffect } from "react"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Specification() { const router = useRouter(); @@ -12,18 +12,17 @@ export default function Specification() { if (!resource) return; const isSandbox = window.location.host.includes("sandbox"); - await getEntryStoreProps({ - config: { + await handleEntryStoreRedirect( + { pathPrefix: "/specifications", redirectPath: "/specifications", entrystorePathKey: "ENTRYSCAPE_SPECS_PATH", }, - locale: router.locale || "sv", - isSandbox, router, - resourceUri: resource as string, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + resource as string, + ); }; fetchEntryStoreProps(); diff --git a/pages/externalterminology/[...terminology].tsx b/pages/externalterminology/[...terminology].tsx index 6256f8518..9155bed7f 100644 --- a/pages/externalterminology/[...terminology].tsx +++ b/pages/externalterminology/[...terminology].tsx @@ -1,7 +1,7 @@ import { useRouter } from "next/router"; import { useEffect } from "react"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Terminology() { const router = useRouter(); @@ -12,18 +12,17 @@ export default function Terminology() { if (!terminology) return; const isSandbox = window.location.host.includes("sandbox"); - await getEntryStoreProps({ - config: { + await handleEntryStoreRedirect( + { pathPrefix: "/concepts", redirectPath: "/terminology", entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", param: terminology, }, - locale: router.locale || "sv", - isSandbox, router, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + ); }; fetchEntryStoreProps(); diff --git a/pages/externalterminology/index.tsx b/pages/externalterminology/index.tsx index 02f9b4b91..3c6bb9da1 100644 --- a/pages/externalterminology/index.tsx +++ b/pages/externalterminology/index.tsx @@ -1,7 +1,7 @@ import { useRouter } from "next/router"; import { useEffect } from "react"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Terminology() { const router = useRouter(); @@ -12,18 +12,17 @@ export default function Terminology() { if (!resource) return; const isSandbox = window.location.host.includes("sandbox"); - await getEntryStoreProps({ - config: { + await handleEntryStoreRedirect( + { pathPrefix: "/concepts", redirectPath: "/terminology", entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", }, - locale: router.locale || "sv", - isSandbox, router, - resourceUri: resource as string, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + resource as string, + ); }; fetchEntryStoreProps(); diff --git a/pages/specifications/[spec]/[param].tsx b/pages/specifications/[spec]/[param].tsx index f2ea42f32..a7bd9f70d 100644 --- a/pages/specifications/[spec]/[param].tsx +++ b/pages/specifications/[spec]/[param].tsx @@ -4,7 +4,7 @@ import { useContext, useEffect, useState } from "react"; import { SpecificationPage } from "@/features/entryscape/specification-page"; import { EntrystoreProvider } from "@/providers/entrystore-provider"; import { SettingsContext } from "@/providers/settings-provider"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Specification() { const { env } = useContext(SettingsContext); @@ -17,19 +17,18 @@ export default function Specification() { if (!spec || !param) return; const isSandbox = window.location.host.includes("sandbox"); - const data = await getEntryStoreProps({ - config: { + const data = await handleEntryStoreRedirect( + { pathPrefix: "/specifications", redirectPath: "/specifications", entrystorePathKey: "ENTRYSCAPE_SPECS_PATH", param: spec, secondParam: param as string, }, - locale: router.locale || "sv", - isSandbox, router, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + ); if (data?.resourceUri) { setResourceUri(data.resourceUri); diff --git a/pages/specifications/[spec]/index.tsx b/pages/specifications/[spec]/index.tsx index f544fe412..2a0340c9d 100644 --- a/pages/specifications/[spec]/index.tsx +++ b/pages/specifications/[spec]/index.tsx @@ -4,7 +4,7 @@ import { useContext, useEffect, useState } from "react"; import { SpecificationPage } from "@/features/entryscape/specification-page"; import { EntrystoreProvider } from "@/providers/entrystore-provider"; import { SettingsContext } from "@/providers/settings-provider"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Specification() { const { env } = useContext(SettingsContext); @@ -18,18 +18,17 @@ export default function Specification() { if (!spec) return; const isSandbox = window.location.host.includes("sandbox"); - const data = await getEntryStoreProps({ - config: { + const data = await handleEntryStoreRedirect( + { pathPrefix: "/specifications", redirectPath: "/specifications", entrystorePathKey: "ENTRYSCAPE_SPECS_PATH", param: spec, }, - locale: router.locale || "sv", - isSandbox, router, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + ); if (data?.resourceUri) { setResourceUri(data.resourceUri); diff --git a/pages/terminology/[term]/[param].tsx b/pages/terminology/[term]/[param].tsx index faf7ae960..78ef21c44 100644 --- a/pages/terminology/[term]/[param].tsx +++ b/pages/terminology/[term]/[param].tsx @@ -4,7 +4,7 @@ import { useContext, useEffect, useState } from "react"; import { ConceptPage } from "@/features/entryscape/concept-page"; import { EntrystoreProvider } from "@/providers/entrystore-provider"; import { SettingsContext } from "@/providers/settings-provider"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Terminology() { const { env } = useContext(SettingsContext); @@ -17,19 +17,18 @@ export default function Terminology() { if (!term || !param) return; const isSandbox = window.location.host.includes("sandbox"); - const data = await getEntryStoreProps({ - config: { + const data = await handleEntryStoreRedirect( + { pathPrefix: "/concepts", redirectPath: "/terminology", entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", param: term, secondParam: param as string, }, - locale: router.locale || "sv", - isSandbox, router, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + ); if (data?.resourceUri) { setResourceUri(data.resourceUri); diff --git a/pages/terminology/[term]/index.tsx b/pages/terminology/[term]/index.tsx index 7a7a76511..84e9a44a9 100644 --- a/pages/terminology/[term]/index.tsx +++ b/pages/terminology/[term]/index.tsx @@ -4,7 +4,7 @@ import { useContext, useEffect, useState } from "react"; import { ConceptPage } from "@/features/entryscape/concept-page"; import { EntrystoreProvider } from "@/providers/entrystore-provider"; import { SettingsContext } from "@/providers/settings-provider"; -import { getEntryStoreProps } from "@/utilities/entrystore/get-entrystore-props"; +import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect"; export default function Terminology() { const { env } = useContext(SettingsContext); @@ -18,18 +18,17 @@ export default function Terminology() { if (!term) return; const isSandbox = window.location.host.includes("sandbox"); - const data = await getEntryStoreProps({ - config: { + const data = await handleEntryStoreRedirect( + { pathPrefix: "/concepts", redirectPath: "/terminology", entrystorePathKey: "ENTRYSCAPE_TERMS_PATH", param: term, }, - locale: router.locale || "sv", - isSandbox, router, - includeBasePath: false, - }); + router.locale || "sv", + isSandbox, + ); if (data?.resourceUri) { setResourceUri(data.resourceUri); diff --git a/utilities/entrystore/entrystore-redirect.ts b/utilities/entrystore/entrystore-redirect.ts index ba70f68de..fb9ded7e2 100644 --- a/utilities/entrystore/entrystore-redirect.ts +++ b/utilities/entrystore/entrystore-redirect.ts @@ -1,4 +1,5 @@ import { Entry } from "@entryscape/entrystore-js"; +import { NextRouter } from "next/router"; import getT from "next-translate/getT"; import { SettingsUtil } from "@/env"; @@ -10,6 +11,7 @@ import { includeLangInPath } from "../check-lang"; export async function handleEntryStoreRedirect( config: RedirectConfig, + router: NextRouter, locale: string = "sv", isSandbox: boolean = false, resourceUri?: string, @@ -49,11 +51,12 @@ export async function handleEntryStoreRedirect( const resourceUri = entry.getResourceURI(); if (resourceUri.startsWith(baseUrl)) { - return { - redirect: `${includeLangInPath(locale)}${ + router.replace( + `${includeLangInPath(locale)}${ config.redirectPath }${resourceUri.replace(`${baseUrl}${config.pathPrefix}`, "")}`, - }; + ); + return; } else { return { props: {} }; } @@ -65,9 +68,7 @@ export async function handleEntryStoreRedirect( resourceUri = `${baseUrl}${config.pathPrefix}/${pathSuffix}`; return { - props: { - resourceUri, - }, + resourceUri, }; } } @@ -78,11 +79,12 @@ export async function handleEntryStoreRedirect( ); if (entry) { - return { - redirect: `${includeLangInPath(locale)}${config.redirectPath}/${entry + router.replace( + `${includeLangInPath(locale)}${config.redirectPath}/${entry .getContext() .getId()}_${entry.getId()}`, - }; + ); + return; } } catch (error) { console.error("Error fetching entry:", error); diff --git a/utilities/entrystore/get-entrystore-props.ts b/utilities/entrystore/get-entrystore-props.ts deleted file mode 100644 index eeb1c41c4..000000000 --- a/utilities/entrystore/get-entrystore-props.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { NextRouter } from "next/router"; - -import { RedirectConfig } from "@/types/global"; - -interface EntryStoreResponse { - resourceUri: string | null; -} -export async function getEntryStoreProps({ - config, - locale, - isSandbox = false, - router, - resourceUri, - includeBasePath = true, -}: { - config: RedirectConfig; - locale: string; - isSandbox: boolean; - router: NextRouter; - resourceUri?: string; - includeBasePath?: boolean; -}): Promise { - try { - const response = await fetch( - `${includeBasePath ? process.env.HOST : ""}/api/entrystore-redirect`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - config, - locale, - resourceUri, - isSandbox, - }), - }, - ); - - const data = await response.json(); - - if (data.redirect) { - router.replace(data.redirect, undefined, { - shallow: true, - }); - return; - } - - if (data.notFound) { - router.replace("/404", undefined, { - shallow: true, - }); - return; - } - - return { - resourceUri: data.props.resourceUri || null, - }; - } catch (error) { - console.error("Error during static prop generation:", error); - router.replace("/404", undefined, { - shallow: true, - }); - return; - } -}