Skip to content

Commit cb9ee90

Browse files
make sure all the necessary files are generated at build time
1 parent 812078b commit cb9ee90

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ dist/
3737
apps/site/.open-next
3838
apps/site/.wrangler
3939

40-
.next.helpers.mjs
40+
.next.helpers.mjs
41+
.blogData.mjs

apps/site/next-data/generators/blogData.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ const generateBlogData = async () => {
6363
'**/index.md',
6464
]);
6565

66+
const result = { /* generated at build time */ };
67+
68+
if (Object.keys(result).length > 0) {
69+
return {
70+
...result,
71+
posts: result.posts.map(post => ({ ...post, date: new Date(post.date) })),
72+
};
73+
}
74+
6675
return new Promise(resolve => {
6776
const posts = [];
6877
const rawFrontmatter = [];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { cache } from 'react';
22

3-
import generateBlogData from '@/next-data/generators/blogData.mjs';
3+
import generateBlogData from '@/.blogData.mjs';
44
import { BLOG_POSTS_PER_PAGE } from '@/next.constants.mjs';
55
import type { BlogPostsRSC } from '@/types';
66

apps/site/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"scripts:release-post": "cross-env NODE_NO_WARNINGS=1 node scripts/release-post/index.mjs",
2020
"dev": "cross-env NODE_NO_WARNINGS=1 next dev --turbo",
2121
"serve": "npm run dev",
22-
"prebuild": "node scripts/build.helpers.mjs",
22+
"prebuild": "node scripts/prebuild.generator.mjs",
2323
"build": "cross-env NODE_NO_WARNINGS=1 next build",
2424
"start": "cross-env NODE_NO_WARNINGS=1 next start",
2525
"deploy": "cross-env NEXT_PUBLIC_STATIC_EXPORT=true npm run build",

apps/site/scripts/build.helpers.mjs renamed to apps/site/scripts/prebuild.generator.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { resolve } from 'node:path';
55

66
import { glob } from 'glob';
77

8+
import generateBlogData from '../next-data/generators/blogData.mjs'
89
import { getRelativePath } from '../next.helpers.mjs';
910

1011
const __dirname = getRelativePath(import.meta.url);
@@ -26,3 +27,20 @@ writeFileSync(
2627
resolve(__dirname, '..', '.next.helpers.mjs'),
2728
outputNextHelpersMjs
2829
);
30+
31+
const blogDataMjs = readFileSync(
32+
resolve(__dirname, '..', 'next-data', 'generators', 'blogData.mjs'),
33+
'utf8'
34+
);
35+
36+
const blogData = await generateBlogData();
37+
38+
const outputBlogDataMjs = blogDataMjs.replace(
39+
/const result = \{\s*\/\* generated at build time \*\/\s*\};/,
40+
`const result = ${JSON.stringify(blogData)};`
41+
).replace(" from '../../.next.helpers.mjs';", " from './.next.helpers.mjs';");
42+
43+
writeFileSync(
44+
resolve(__dirname, '..', '.blogData.mjs'),
45+
outputBlogDataMjs
46+
);

0 commit comments

Comments
 (0)