Skip to content

Commit a1f4000

Browse files
remove blog-data endpoint and use public json file instead
1 parent aebf63f commit a1f4000

File tree

10 files changed

+204
-194
lines changed

10 files changed

+204
-194
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ tsconfig.tsbuildinfo
3333

3434
dist/
3535

36+
# Ignore the blog-data json that we generate during dev and build
37+
apps/site/public/blog-data.json
38+
3639
# Ignore worker artifacts
3740
apps/site/.open-next
3841
apps/site/.wrangler

apps/site/app/[locale]/next-data/blog-data/[category]/[page]/route.ts

-48
This file was deleted.

apps/site/layouts/Blog.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import type { BlogCategory } from '@/types';
1111

1212
import styles from './layouts.module.css';
1313

14-
const getBlogCategory = async (pathname: string) => {
14+
const getBlogCategory = (pathname: string) => {
1515
// pathname format can either be: /en/blog/{category}
1616
// or /en/blog/{category}/page/{page}
1717
// hence we attempt to interpolate the full /en/blog/{category}/page/{page}
1818
// and in case of course no page argument is provided we define it to 1
1919
// note that malformed routes can't happen as they are all statically generated
2020
const [, , category = 'all', , page = 1] = pathname.split('/');
2121

22-
const { posts, pagination } = await getBlogData(
22+
const { posts, pagination } = getBlogData(
2323
category as BlogCategory,
2424
Number(page)
2525
);
@@ -38,7 +38,7 @@ const BlogLayout: FC = async () => {
3838
link: `/blog/${category}`,
3939
}));
4040

41-
const blogData = await getBlogCategory(pathname);
41+
const blogData = getBlogCategory(pathname);
4242

4343
return (
4444
<>

apps/site/next-data/blogData.ts

+10-32
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,16 @@
1-
import {
2-
ENABLE_STATIC_EXPORT,
3-
IS_DEV_ENV,
4-
NEXT_DATA_URL,
5-
VERCEL_ENV,
6-
VERCEL_REGION,
7-
} from '@/next.constants.mjs';
81
import type { BlogCategory, BlogPostsRSC } from '@/types';
92

10-
const getBlogData = (
11-
cat: BlogCategory,
12-
page?: number
13-
): Promise<BlogPostsRSC> => {
14-
const IS_NOT_VERCEL_RUNTIME_ENV =
15-
(!IS_DEV_ENV && VERCEL_ENV && !VERCEL_REGION) ||
16-
(!IS_DEV_ENV && !VERCEL_ENV);
17-
18-
// When we're using Static Exports the Next.js Server is not running (during build-time)
19-
// hence the self-ingestion APIs will not be available. In this case we want to load
20-
// the data directly within the current thread, which will anyways be loaded only once
21-
// We use lazy-imports to prevent `provideBlogData` from executing on import
22-
if (ENABLE_STATIC_EXPORT || IS_NOT_VERCEL_RUNTIME_ENV) {
23-
return import('@/next-data/providers/blogData').then(
24-
({ provideBlogPosts, providePaginatedBlogPosts }) =>
25-
page ? providePaginatedBlogPosts(cat, page) : provideBlogPosts(cat)
26-
);
27-
}
28-
29-
const fetchURL = `${NEXT_DATA_URL}blog-data/${cat}/${page ?? 0}`;
3+
import {
4+
provideBlogPosts,
5+
providePaginatedBlogPosts,
6+
} from './providers/blogData';
307

31-
// This data cannot be cached because it is continuously updated. Caching it would lead to
32-
// outdated information being shown to the user.
33-
return fetch(fetchURL)
34-
.then(response => response.text())
35-
.then(JSON.parse);
8+
const getBlogData = (cat: BlogCategory, page?: number): BlogPostsRSC => {
9+
return page && page >= 1
10+
? // This allows us to blindly get all blog posts from a given category
11+
// if the page number is 0 or something smaller than 1
12+
providePaginatedBlogPosts(cat, page)
13+
: provideBlogPosts(cat);
3614
};
3715

3816
export default getBlogData;

apps/site/next-data/providers/blogData.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import { cache } from 'react';
22

3-
import generateBlogData from '@/next-data/generators/blogData.mjs';
43
import { BLOG_POSTS_PER_PAGE } from '@/next.constants.mjs';
4+
import { blogData as rawBlogData } from '@/next.json.mjs';
55
import type { BlogCategory, BlogPostsRSC } from '@/types';
66

7-
const { categories, posts } = await generateBlogData();
7+
const blogData = {
8+
...rawBlogData,
9+
posts: rawBlogData.posts.map(post => ({
10+
...post,
11+
date: new Date(post.date),
12+
})),
13+
};
14+
15+
const { categories, posts } = blogData;
816

917
export const provideBlogCategories = cache(() => categories);
1018

apps/site/next.json.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import _authors from './authors.json' with { type: 'json' };
44
import _siteNavigation from './navigation.json' with { type: 'json' };
5+
import _blogData from './public/blog-data.json' with { type: 'json' };
56
import _siteRedirects from './redirects.json' with { type: 'json' };
67
import _siteConfig from './site.json' with { type: 'json' };
78

@@ -16,3 +17,6 @@ export const siteRedirects = _siteRedirects;
1617

1718
/** @type {import('./types').SiteConfig} */
1819
export const siteConfig = _siteConfig;
20+
21+
/** @type {import('./types').BlogData} */
22+
export const blogData = _blogData;

apps/site/package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"type": "module",
44
"scripts": {
55
"scripts:release-post": "cross-env NODE_NO_WARNINGS=1 node scripts/release-post/index.mjs",
6-
"dev": "cross-env NODE_NO_WARNINGS=1 next dev --turbopack",
7-
"serve": "npm run dev",
6+
"dev": "npm run build-blog-data -- --watch & cross-env NODE_NO_WARNINGS=1 next dev --turbopack",
7+
"serve": "npm run build-blog-data -- --watch & npm run dev",
8+
"prebuild": "npm run build-blog-data",
89
"build": "cross-env NODE_NO_WARNINGS=1 next build",
910
"start": "cross-env NODE_NO_WARNINGS=1 next start",
1011
"deploy": "cross-env NEXT_PUBLIC_STATIC_EXPORT=true npm run build",
@@ -16,6 +17,7 @@
1617
"lint": "turbo run lint:md lint:snippets lint:js lint:css",
1718
"lint:fix": "turbo run lint:md lint:js lint:css --no-cache -- --fix",
1819
"sync-orama": "node ./scripts/orama-search/sync-orama-cloud.mjs",
20+
"build-blog-data": "node ./scripts/blog-data/generate.mjs",
1921
"test:unit": "cross-env NODE_NO_WARNINGS=1 jest",
2022
"test:unit:watch": "npm run test:unit -- --watch",
2123
"test": "turbo test:unit",
@@ -73,11 +75,13 @@
7375
"devDependencies": {
7476
"@eslint/compat": "~1.2.7",
7577
"@next/eslint-plugin-next": "15.2.0",
78+
"@opennextjs/cloudflare": "^0.6.4",
7679
"@testing-library/jest-dom": "~6.6.3",
7780
"@testing-library/react": "~16.2.0",
7881
"@testing-library/user-event": "~14.6.1",
7982
"@types/jest": "29.5.14",
8083
"@types/semver": "~7.5.8",
84+
"chokidar": "^4.0.3",
8185
"eslint-config-next": "15.2.0",
8286
"eslint-import-resolver-typescript": "~3.8.3",
8387
"eslint-plugin-mdx": "~3.1.5",
@@ -96,7 +100,6 @@
96100
"typescript": "~5.7.2",
97101
"typescript-eslint": "~8.25.0",
98102
"user-agent-data-types": "0.4.2",
99-
"wrangler": "^4.6.0",
100-
"@opennextjs/cloudflare": "^0.6.4"
103+
"wrangler": "^4.6.0"
101104
}
102105
}

apps/site/public/blog-data.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"categories": [],
3+
"posts": []
4+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { writeFileSync } from 'node:fs';
2+
import { dirname } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
import { watch } from 'chokidar';
6+
7+
import generateBlogData from '../../next-data/generators/blogData.mjs';
8+
9+
await generateAndSaveBlogData();
10+
11+
const watchMode =
12+
process.argv.includes('--watch') || process.argv.includes('-w');
13+
14+
if (watchMode) {
15+
watch('pages/en', { ignoreInitial: true }).on('all', generateAndSaveBlogData);
16+
}
17+
18+
async function generateAndSaveBlogData() {
19+
const blogData = await generateBlogData();
20+
21+
const __dirname = dirname(fileURLToPath(import.meta.url));
22+
writeFileSync(
23+
`${__dirname}/../../public/blog-data.json`,
24+
JSON.stringify(blogData),
25+
'utf8'
26+
);
27+
}

0 commit comments

Comments
 (0)