Skip to content

Commit aebf63f

Browse files
fixup! make site work with the Cloudflare OpenNext adapter
remove all changes related to async context
1 parent cc9d071 commit aebf63f

File tree

15 files changed

+45
-64
lines changed

15 files changed

+45
-64
lines changed

apps/site/app/[locale]/feed/[feed]/route.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const GET = async (_: Request, props: StaticParams) => {
1414
const params = await props.params;
1515

1616
// Generate the Feed for the given feed type (blog, releases, etc)
17-
const websiteFeed = await provideWebsiteFeeds(params.feed);
17+
const websiteFeed = provideWebsiteFeeds(params.feed);
1818

1919
return new NextResponse(websiteFeed, {
2020
headers: { 'Content-Type': 'application/xml' },

apps/site/app/[locale]/next-data/api-data/route.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const getPathnameForApiFile = (name: string, version: string) =>
2121
// for a digest and metadata of all API pages from the Node.js Website
2222
// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers
2323
export const GET = async () => {
24-
const releases = await provideReleaseData();
24+
const releases = provideReleaseData();
2525

2626
const { versionWithPrefix } = releases.find(
2727
release => release.status === 'LTS'

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ export const GET = async (_: Request, props: StaticParams) => {
2020

2121
const requestedPage = Number(params.page);
2222

23-
const data = await (requestedPage >= 1
24-
? // This allows us to blindly get all blog posts from a given category
25-
// if the page number is 0 or something smaller than 1
26-
providePaginatedBlogPosts(params.category, requestedPage)
27-
: provideBlogPosts(params.category));
23+
const data =
24+
requestedPage >= 1
25+
? // This allows us to blindly get all blog posts from a given category
26+
// if the page number is 0 or something smaller than 1
27+
providePaginatedBlogPosts(params.category, requestedPage)
28+
: provideBlogPosts(params.category);
2829

2930
return Response.json(data, { status: data.posts.length ? 200 : 404 });
3031
};

apps/site/app/[locale]/next-data/download-snippets/route.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const GET = async (_: Request, props: StaticParams) => {
1111
const params = await props.params;
1212

1313
// Retrieve all available Download snippets for a given locale if available
14-
const snippets = await provideDownloadSnippets(params.locale);
14+
const snippets = provideDownloadSnippets(params.locale);
1515

1616
// We append always the default/fallback snippets when a result is found
1717
return Response.json(snippets, {

apps/site/app/[locale]/next-data/release-data/route.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { defaultLocale } from '@/next.locales.mjs';
55
// for generating static data related to the Node.js Release Data
66
// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers
77
export const GET = async () => {
8-
const releaseData = await provideReleaseData();
8+
const releaseData = provideReleaseData();
99

1010
return Response.json(releaseData);
1111
};

apps/site/app/[locale]/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const getPage: FC<DynamicParams> = async props => {
9292
// Gets the current full pathname for a given path
9393
const pathname = dynamicRouter.getPathname(path);
9494

95-
const staticGeneratedLayout = (await DYNAMIC_ROUTES()).get(pathname);
95+
const staticGeneratedLayout = DYNAMIC_ROUTES.get(pathname);
9696

9797
// If the current pathname is a statically generated route
9898
// it means it does not have a Markdown file nor exists under the filesystem

apps/site/components/withDownloadSection.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import getDownloadSnippets from '@/next-data/downloadSnippets';
77
import getReleaseData from '@/next-data/releaseData';
88
import { defaultLocale } from '@/next.locales.mjs';
99
import { ReleaseProvider, ReleasesProvider } from '@/providers/releaseProvider';
10-
import type { DownloadSnippet } from '@/types';
1110

1211
// By default the translated languages do not contain all the download snippets
1312
// Hence we always merge any translated snippet with the fallbacks for missing snippets
14-
let fallbackSnippets: Array<DownloadSnippet>;
13+
const fallbackSnippets = await getDownloadSnippets(defaultLocale.code);
1514

1615
const WithDownloadSection: FC<PropsWithChildren> = async ({ children }) => {
17-
fallbackSnippets ??= await getDownloadSnippets(defaultLocale.code);
18-
1916
const locale = await getLocale();
2017
const releases = await getReleaseData();
2118
const snippets = await getDownloadSnippets(locale);

apps/site/next-data/downloadSnippets.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default async function getDownloadSnippets(
2929
const { default: provideDownloadSnippets } = await import(
3030
'@/next-data/providers/downloadSnippets'
3131
);
32-
return (await provideDownloadSnippets(lang))!;
32+
return provideDownloadSnippets(lang)!;
3333
}
3434

3535
// Applies the language to the URL, since this content is actually localized

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

+7-14
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@ import { cache } from 'react';
22

33
import generateBlogData from '@/next-data/generators/blogData.mjs';
44
import { BLOG_POSTS_PER_PAGE } from '@/next.constants.mjs';
5-
import type { BlogCategory, BlogPostsRSC, BlogPost } from '@/types';
5+
import type { BlogCategory, BlogPostsRSC } from '@/types';
66

7-
let blogData: {
8-
categories: Array<BlogCategory>;
9-
posts: Array<BlogPost>;
10-
};
7+
const { categories, posts } = await generateBlogData();
118

12-
export const provideBlogCategories = cache(async () => {
13-
blogData ??= await generateBlogData();
14-
return blogData.categories;
15-
});
9+
export const provideBlogCategories = cache(() => categories);
1610

1711
export const provideBlogPosts = cache(
18-
async (category: BlogCategory): Promise<BlogPostsRSC> => {
19-
blogData ??= await generateBlogData();
20-
const categoryPosts = blogData.posts
12+
(category: BlogCategory): BlogPostsRSC => {
13+
const categoryPosts = posts
2114
.filter(post => post.categories.includes(category))
2215
.sort((a, b) => b.date.getTime() - a.date.getTime());
2316

@@ -39,8 +32,8 @@ export const provideBlogPosts = cache(
3932
);
4033

4134
export const providePaginatedBlogPosts = cache(
42-
async (category: BlogCategory, page: number): Promise<BlogPostsRSC> => {
43-
const { posts, pagination } = await provideBlogPosts(category);
35+
(category: BlogCategory, page: number): BlogPostsRSC => {
36+
const { posts, pagination } = provideBlogPosts(category);
4437

4538
// This autocorrects if invalid numbers are given to only allow
4639
// actual valid numbers to be provided

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { cache } from 'react';
22

33
import generateDownloadSnippets from '@/next-data/generators/downloadSnippets.mjs';
44

5-
const provideDownloadSnippets = cache(async (language: string) => {
6-
const downloadSnippets = await generateDownloadSnippets();
5+
const downloadSnippets = await generateDownloadSnippets();
76

7+
const provideDownloadSnippets = cache((language: string) => {
88
if (downloadSnippets.has(language)) {
99
return downloadSnippets.get(language)!;
1010
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { cache } from 'react';
22

33
import generateReleaseData from '@/next-data/generators/releaseData.mjs';
44

5-
const provideReleaseData = cache(() => generateReleaseData());
5+
const releaseData = await generateReleaseData();
6+
7+
const provideReleaseData = cache(() => releaseData);
68

79
export default provideReleaseData;

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ import { cache } from 'react';
33
import generateWebsiteFeeds from '@/next-data/generators/websiteFeeds.mjs';
44
import { provideBlogPosts } from '@/next-data/providers/blogData';
55

6-
const getWebsiteFeeds = cache(async () =>
7-
generateWebsiteFeeds(await provideBlogPosts('all'))
8-
);
9-
10-
const provideWebsiteFeeds = cache(async (feed: string) => {
11-
const websiteFeeds = await getWebsiteFeeds();
6+
const websiteFeeds = generateWebsiteFeeds(provideBlogPosts('all'));
127

8+
const provideWebsiteFeeds = cache((feed: string) => {
139
if (feed.includes('.xml') && websiteFeeds.has(feed)) {
1410
return websiteFeeds.get(feed)!.rss2();
1511
}

apps/site/next.dynamic.constants.mjs

+16-24
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,23 @@ export const IGNORED_ROUTES = [
2727
* counterpart route. This is useful for providing routes with matching Layout Names
2828
* but that do not have Markdown content and a matching file for the route
2929
*
30-
* @type {() => Promise<Map<string, import('./types').Layouts>>} A Map of pathname and Layout Name
30+
* @type {Map<string, import('./types').Layouts>} A Map of pathname and Layout Name
3131
*/
32-
export const DYNAMIC_ROUTES = async () => {
33-
const blogCategories = await provideBlogCategories();
34-
35-
const pages = [];
36-
for (const c of blogCategories) {
37-
const categoryPages = (await provideBlogPosts(c)).pagination.pages;
38-
pages.push([c, categoryPages]);
39-
}
40-
41-
return new Map([
42-
// Provides Routes for all Blog Categories
43-
...blogCategories.map(c => [`blog/${c}`, 'blog-category']),
44-
// Provides Routes for all Blog Categories w/ Pagination
45-
...pages
46-
// creates a numeric array for each page and define a pathname for
47-
// each page for a category (i.e. blog/all/page/1)
48-
.map(([c, t]) => [...Array(t).keys()].map(p => `blog/${c}/page/${p + 1}`))
49-
// creates a tuple of each pathname and layout for the route
50-
.map(paths => paths.map(path => [path, 'blog-category']))
51-
// flattens the array since we have a .map inside another .map
52-
.flat(),
53-
]);
54-
};
32+
export const DYNAMIC_ROUTES = new Map([
33+
// Provides Routes for all Blog Categories
34+
...provideBlogCategories().map(c => [`blog/${c}`, 'blog-category']),
35+
// Provides Routes for all Blog Categories w/ Pagination
36+
...provideBlogCategories()
37+
// retrieves the amount of pages for each blog category
38+
.map(c => [c, provideBlogPosts(c).pagination.pages])
39+
// creates a numeric array for each page and define a pathname for
40+
// each page for a category (i.e. blog/all/page/1)
41+
.map(([c, t]) => [...Array(t).keys()].map(p => `blog/${c}/page/${p + 1}`))
42+
// creates a tuple of each pathname and layout for the route
43+
.map(paths => paths.map(path => [path, 'blog-category']))
44+
// flattens the array since we have a .map inside another .map
45+
.flat(),
46+
]);
5547

5648
/**
5749
* This is the default Next.js Page Metadata for all pages

apps/site/next.dynamic.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const getDynamicRouter = async () => {
9393

9494
return [...pathnameToFilename.keys()]
9595
.filter(shouldIgnoreStaticRoute)
96-
.concat([...(await DYNAMIC_ROUTES()).keys()]);
96+
.concat([...DYNAMIC_ROUTES.keys()]);
9797
};
9898

9999
/**

apps/site/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"test": "turbo test:unit",
2222
"build:worker": "npx opennextjs-cloudflare build",
2323
"postbuild:worker": "node ./.cloudflare/prepare-build.mjs",
24-
"cf:preview": "npm run build:worker && npx wrangler dev",
24+
"cf:preview": "npm run build:worker && sleep 3 && npx wrangler dev",
2525
"cf:deploy": "npx wrangler deploy"
2626
},
2727
"dependencies": {

0 commit comments

Comments
 (0)