Skip to content

Commit

Permalink
DIGG-483: Adding utillity function to handle redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaMunterud committed Dec 7, 2024
1 parent 78e1409 commit 4277fa6
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 502 deletions.
52 changes: 12 additions & 40 deletions pages/concepts/[concept]/[param].tsx
Original file line number Diff line number Diff line change
@@ -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",
});
};
58 changes: 11 additions & 47 deletions pages/concepts/[concept]/index.tsx
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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",
});
};
58 changes: 11 additions & 47 deletions pages/externalconcept/[...concept].tsx
Original file line number Diff line number Diff line change
@@ -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",
});
};
50 changes: 14 additions & 36 deletions pages/externalconcept/index.tsx
Original file line number Diff line number Diff line change
@@ -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,
);
};
58 changes: 11 additions & 47 deletions pages/externalspecification/[...specification].tsx
Original file line number Diff line number Diff line change
@@ -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",
});
};
50 changes: 14 additions & 36 deletions pages/externalspecification/index.tsx
Original file line number Diff line number Diff line change
@@ -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,
);
};
Loading

0 comments on commit 4277fa6

Please sign in to comment.