The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
improve performance of getSortedPosts
Daniel Cruzbb579cc3c958
diff --git a/apps/www/lib/posts.tsx b/apps/www/lib/posts.tsxindex d9e8177d82d..0cbe02d0bd5 100644--- a/apps/www/lib/posts.tsx+++ b/apps/www/lib/posts.tsx@@ -12,7 +12,7 @@ const FILENAME_SUBSTRING = 11type GetSortedPostsParams = {directory: Directorieslimit?: number- tags?: unknown[]+ tags?: string[]runner?: unknowncurrentPostSlug?: string}@@ -29,33 +29,40 @@ export const getSortedPosts = ({//Reads all the files in the post directoryconst fileNames = fs.readdirSync(postDirectory)- // categories stored in this array+ const posts = []- const allPostsData = fileNames.map((filename) => {- let slug = ''- slug = filename.replace('.mdx', '')- if (directory === '_blog') {- slug = slug.substring(FILENAME_SUBSTRING)+ for (const filename of fileNames) {+ const slug =+ directory === '_blog'+ ? filename.replace('.mdx', '').substring(FILENAME_SUBSTRING)+ : filename.replace('.mdx', '')++ // avoid reading content if it's the same post as the one the user is already reading+ if (slug === currentPostSlug) {+ continue}const fullPath = path.join(postDirectory, filename)//Extracts contents of the MDX fileconst fileContents = fs.readFileSync(fullPath, 'utf8')- const { data, content } = matter(fileContents)+ const { data, content } = matter(fileContents) as unknown as {+ data: { [key: string]: any; tags?: string[] }+ content: string+ }++ // if no matching tags, there's no need to do the rest+ if (tags && !tags.some((tag) => data.tags?.includes(tag))) {+ continue+ }const options: Intl.DateTimeFormatOptions = { month: 'long', day: 'numeric', year: 'numeric' }const formattedDate = new Date(data.date).toLocaleDateString('en-IN', options)const readingTime = generateReadingTime(content)- // construct url to link to blog posts- // based on datestamp in file name- let url = ''- let contentPath = ''-- url = `/${directory.replace('_', '')}/${slug}`- contentPath = `/${directory.replace('_', '')}/${slug}`+ const url = `/${directory.replace('_', '')}/${slug}`+ const contentPath = `/${directory.replace('_', '')}/${slug}`const frontmatter = {...data,@@ -65,39 +72,30 @@ export const getSortedPosts = ({url: url,path: contentPath,}- return {++ const post = {slug,...frontmatter,}- })- let sortedPosts = [...allPostsData]+ posts.push(post)+ }- sortedPosts = sortedPosts.sort((a, b) => {+ /* Array.prototype.sort(), mutates the original array ¯\_(ツ)_/¯+ * so no need to re-assign -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort+ */+ posts.sort((a, b) => {const isPublishedAtBefore =a.publishedAt && b.publishedAt && Date.parse(a.publishedAt) < Date.parse(b.publishedAt)if (isPublishedAtBefore || new Date(a.date) < new Date(b.date)) {return 1- } else {- return -1}- })- if (tags) {- sortedPosts = sortedPosts.filter((post: any) => {- const found = tags.some((tag: any) => post.tags?.includes(tag))- return found- })- }-- if (currentPostSlug) {- sortedPosts = sortedPosts.filter((post) => post.slug !== currentPostSlug)- }-- if (limit) sortedPosts = sortedPosts.slice(0, limit)+ return -1+ })- return sortedPosts+ return posts.slice(0, limit)}// Get Slugs