-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DIGG-483: Adding utillity function to handle redirects
- Loading branch information
1 parent
78e1409
commit 4277fa6
Showing
13 changed files
with
220 additions
and
502 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}; |
Oops, something went wrong.