Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimize performance in product page route | inlang.com 🌎 #2946

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 20 additions & 26 deletions inlang/source-code/website/src/pages/m/+onBeforeRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type { PageProps } from "./+Page.jsx"
import { getRedirectPath } from "./helper/getRedirectPath.js"

const repositoryRoot = import.meta.url.slice(0, import.meta.url.lastIndexOf("inlang/source-code"))
let renderedMarkdown = {} as Record<string, string>
let renderedMarkdown = {} as string | undefined
//let tabelOfContents = {} as Record<string, Record<string, string[]>>
let pageData = {} as Record<string, Record<string, unknown>>
let pageData = {} as Record<string, unknown> | undefined

/*
* This function is called before rendering the page.
Expand All @@ -25,9 +25,9 @@ let pageData = {} as Record<string, Record<string, unknown>>
*
*/
export default async function onBeforeRender(pageContext: PageContext) {
renderedMarkdown = {}
renderedMarkdown = undefined
//tabelOfContents = {}
pageData = {}
pageData = undefined

// check if uid is defined
const uid = pageContext.routeParams?.uid
Expand Down Expand Up @@ -95,20 +95,18 @@ export default async function onBeforeRender(pageContext: PageContext) {
for (const [slug, page] of Object.entries(flattenPages(item.pages))) {
if (!page || !fileExists(page)) redirect(itemPath as `/${string}`, 301)

try {
const content = await getContentString(page)
const markdown = await convert(content)

renderedMarkdown[slug] = markdown.html
if (markdown.data?.frontmatter) {
pageData[slug] = markdown.data?.frontmatter
if (slug === pagePath) {
try {
const content = await getContentString(page)
const markdown = await convert(content)

renderedMarkdown = markdown.html
if (markdown.data?.frontmatter) {
pageData = markdown.data?.frontmatter
}
} catch (error) {
// pages do not getting prerendered because they are link
}

// await generateTableOfContents(markdown.html).then((table) => {
// tabelOfContents[slug] = table
// })
} catch (error) {
// pages do not getting prerendered because they are link
}
}
} else if (item.readme) {
Expand All @@ -121,14 +119,10 @@ export default async function onBeforeRender(pageContext: PageContext) {
try {
const readmeMarkdown = await convert(await getContentString(readme()!))

renderedMarkdown["/"] = readmeMarkdown.html
renderedMarkdown = readmeMarkdown.html
if (readmeMarkdown.data?.frontmatter) {
pageData["/"] = readmeMarkdown.data?.frontmatter
pageData = readmeMarkdown.data?.frontmatter
}

// await generateTableOfContents(readmeMarkdown.html).then((table) => {
// tabelOfContents["/"] = table
// })
} catch (error) {
console.error("Error while accessing the readme file")
throw redirect("/not-found", 301)
Expand All @@ -139,7 +133,7 @@ export default async function onBeforeRender(pageContext: PageContext) {
}

//check if the markdown is available for this route
if (renderedMarkdown[pagePath] === undefined) {
if (renderedMarkdown === undefined) {
console.error("No content available this route.")
if (pagePath !== "/") {
throw (console.info("5"), redirect(itemPath as `/${string}`, 301))
Expand All @@ -161,9 +155,9 @@ export default async function onBeforeRender(pageContext: PageContext) {
return {
pageContext: {
pageProps: {
markdown: renderedMarkdown[pagePath],
markdown: renderedMarkdown,
pages: item.pages,
pageData: pageData ? pageData[pagePath] : undefined,
pageData: pageData,
pagePath,
tableOfContents: {},
manifest: item,
Expand Down
Loading