|
1 | 1 | import { PostModel, PageModel } from '~/models' |
2 | 2 | import { Post, Page } from './types' |
3 | | -import { unstable_cache } from 'next/cache' |
4 | 3 |
|
5 | | -// Function to get all blog posts with Next.js caching |
6 | | -export const getRemoteBlogPosts = unstable_cache( |
7 | | - async (): Promise<Post['data'][]> => { |
8 | | - try { |
9 | | - const results = await PostModel.find({ |
10 | | - where: { |
11 | | - draft: false |
12 | | - } |
13 | | - }) |
| 4 | +// Function to get all blog posts |
| 5 | +export const getRemoteBlogPosts = async (): Promise<Post['data'][]> => { |
| 6 | + try { |
| 7 | + const results = await PostModel.find({ |
| 8 | + where: { |
| 9 | + draft: false |
| 10 | + } |
| 11 | + }) |
14 | 12 |
|
15 | | - return results.data.map((post) => post.data) |
16 | | - } catch (error) { |
17 | | - console.error('Error fetching remote blog posts:', error) |
18 | | - return [] |
19 | | - } |
20 | | - }, |
21 | | - ['blog-posts'], // cache key |
22 | | - { |
23 | | - revalidate: 3600, // revalidate every hour |
24 | | - tags: ['blog-posts'] |
| 13 | + return results.data.map((post) => post.data) |
| 14 | + } catch (error) { |
| 15 | + console.error('Error fetching remote blog posts:', error) |
| 16 | + return [] |
25 | 17 | } |
26 | | -) |
27 | | - |
28 | | -// Function to get a single blog post by slug with Next.js caching |
29 | | -export const getRemoteBlogPost = unstable_cache( |
30 | | - async (slug: string): Promise<Post['data'] | null> => { |
31 | | - try { |
32 | | - const result = await PostModel.findOne({ |
33 | | - where: { |
34 | | - slug |
35 | | - } |
36 | | - }) |
| 18 | +} |
37 | 19 |
|
38 | | - if (result.exists()) { |
39 | | - return result.data |
40 | | - } else { |
41 | | - console.warn(`No blog post found with slug "${slug}"`) |
42 | | - return null |
| 20 | +// Function to get a single blog post by slug |
| 21 | +export const getRemoteBlogPost = async (slug: string): Promise<Post['data'] | null> => { |
| 22 | + try { |
| 23 | + const result = await PostModel.findOne({ |
| 24 | + where: { |
| 25 | + slug |
43 | 26 | } |
44 | | - } catch (error) { |
45 | | - console.error(`Error fetching remote blog post with slug "${slug}":`, error) |
| 27 | + }) |
| 28 | + |
| 29 | + if (result.exists()) { |
| 30 | + return result.data |
| 31 | + } else { |
| 32 | + console.warn(`No blog post found with slug "${slug}"`) |
46 | 33 | return null |
47 | 34 | } |
48 | | - }, |
49 | | - ['blog-post'], // cache key prefix |
50 | | - { |
51 | | - revalidate: 3600, // revalidate every hour |
52 | | - tags: ['blog-post'] |
| 35 | + } catch (error) { |
| 36 | + console.error(`Error fetching remote blog post with slug "${slug}":`, error) |
| 37 | + return null |
53 | 38 | } |
54 | | -) |
| 39 | +} |
55 | 40 |
|
56 | | -// Function to get a single page by slug with Next.js caching |
57 | | -export const getRemotePage = unstable_cache( |
58 | | - async (slug: string): Promise<Page['data'] | null> => { |
59 | | - try { |
60 | | - const result = await PageModel.findOne({ |
61 | | - where: { |
62 | | - slug, |
63 | | - draft: false |
64 | | - } |
65 | | - }) |
66 | | - if (result.exists()) { |
67 | | - return result.data |
68 | | - } else { |
69 | | - console.warn(`No page found with slug "${slug}"`) |
70 | | - return null |
| 41 | +// Function to get a single page by slug |
| 42 | +export const getRemotePage = async (slug: string): Promise<Page['data'] | null> => { |
| 43 | + try { |
| 44 | + const result = await PageModel.findOne({ |
| 45 | + where: { |
| 46 | + slug, |
| 47 | + draft: false |
71 | 48 | } |
72 | | - } catch (error) { |
73 | | - console.error(`Error fetching remote page with slug "${slug}":`, error) |
| 49 | + }) |
| 50 | + if (result.exists()) { |
| 51 | + return result.data |
| 52 | + } else { |
| 53 | + console.warn(`No page found with slug "${slug}"`) |
74 | 54 | return null |
75 | 55 | } |
76 | | - }, |
77 | | - ['page'], // cache key prefix |
78 | | - { |
79 | | - revalidate: 3600, // revalidate every hour |
80 | | - tags: ['page'] |
| 56 | + } catch (error) { |
| 57 | + console.error(`Error fetching remote page with slug "${slug}":`, error) |
| 58 | + return null |
81 | 59 | } |
82 | | -) |
| 60 | +} |
83 | 61 |
|
84 | | -// Function to get featured blog posts with Next.js caching |
85 | | -export const getFeaturedBlogPosts = unstable_cache( |
86 | | - async (limit?: number): Promise<Post['data'][]> => { |
87 | | - try { |
88 | | - const results = await PostModel.find({ |
89 | | - where: { |
90 | | - draft: false, |
91 | | - featured: true |
92 | | - }, |
93 | | - limit |
94 | | - }) |
| 62 | +// Function to get featured blog posts |
| 63 | +export const getFeaturedBlogPosts = async (limit?: number): Promise<Post['data'][]> => { |
| 64 | + try { |
| 65 | + const results = await PostModel.find({ |
| 66 | + where: { |
| 67 | + draft: false, |
| 68 | + featured: true |
| 69 | + }, |
| 70 | + limit |
| 71 | + }) |
95 | 72 |
|
96 | | - return results.data.map((post) => post.data) |
97 | | - } catch (error) { |
98 | | - console.error('Error fetching featured blog posts:', error) |
99 | | - return [] |
100 | | - } |
101 | | - }, |
102 | | - ['featured-blog-posts'], // cache key |
103 | | - { |
104 | | - revalidate: 3600, // revalidate every hour |
105 | | - tags: ['featured-blog-posts'] |
| 73 | + return results.data.map((post) => post.data) |
| 74 | + } catch (error) { |
| 75 | + console.error('Error fetching featured blog posts:', error) |
| 76 | + return [] |
106 | 77 | } |
107 | | -) |
| 78 | +} |
0 commit comments