|
1 | 1 | import { NextResponse } from 'next/server'; |
2 | 2 |
|
3 | | -import { generateWebsiteFeeds } from '@/next.data.mjs'; |
4 | | -import { blogData } from '@/next.json.mjs'; |
| 3 | +import provideWebsiteFeeds from '@/next-data/providers/websiteFeeds'; |
| 4 | +import { siteConfig } from '@/next.json.mjs'; |
5 | 5 | import { defaultLocale } from '@/next.locales.mjs'; |
6 | 6 |
|
7 | | -// loads all the data from the blog-posts-data.json file |
8 | | -const websiteFeeds = generateWebsiteFeeds(blogData); |
| 7 | +// We only support fetching these pages from the /en/ locale code |
| 8 | +const locale = defaultLocale.code; |
9 | 9 |
|
10 | | -type StaticParams = { params: { feed: string } }; |
| 10 | +type StaticParams = { params: { feed: string; locale: string } }; |
11 | 11 |
|
12 | 12 | // This is the Route Handler for the `GET` method which handles the request |
13 | | -// for Blog Feeds within the Node.js Website |
| 13 | +// for the Node.js Website Blog Feeds (RSS) |
14 | 14 | // @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers |
15 | | -export const GET = (_: Request, { params }: StaticParams) => { |
16 | | - if (params.feed.includes('.xml') && websiteFeeds.has(params.feed)) { |
17 | | - return new NextResponse(websiteFeeds.get(params.feed)?.rss2(), { |
18 | | - headers: { 'Content-Type': 'application/xml' }, |
19 | | - }); |
20 | | - } |
21 | | - |
22 | | - return new NextResponse(null, { status: 404 }); |
| 15 | +export const GET = async (_: Request, { params }: StaticParams) => { |
| 16 | + // Generate the Feed for the given feed type (blog, releases, etc) |
| 17 | + const websiteFeed = await provideWebsiteFeeds(params.feed); |
| 18 | + |
| 19 | + return new NextResponse(websiteFeed, { |
| 20 | + headers: { 'Content-Type': 'application/xml' }, |
| 21 | + status: websiteFeed ? 200 : 404, |
| 22 | + }); |
23 | 23 | }; |
24 | 24 |
|
25 | 25 | // This function generates the static paths that come from the dynamic segments |
26 | | -// `en/feeds/[feed]` and returns an array of all available static paths |
27 | | -// this is useful for static exports, for example. |
28 | | -// Note that differently from the App Router these don't get built at the build time |
29 | | -// only if the export is already set for static export |
30 | | -export const generateStaticParams = () => |
31 | | - [...websiteFeeds.keys()].map(feed => ({ feed, locale: defaultLocale.code })); |
32 | | - |
33 | | -// Enforces that this route is used as static rendering |
| 26 | +// `[locale]/feeds/[feed]` and returns an array of all available static paths |
| 27 | +// This is used for ISR static validation and generation |
| 28 | +export const generateStaticParams = async () => |
| 29 | + siteConfig.rssFeeds.map(feed => ({ feed: feed.file, locale })); |
| 30 | + |
| 31 | +// Forces that only the paths from `generateStaticParams` are allowed, giving 404 on the contrary |
| 32 | +// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams |
| 33 | +export const dynamicParams = false; |
| 34 | + |
| 35 | +// Enforces that this route is cached and static as much as possible |
34 | 36 | // @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic |
35 | 37 | export const dynamic = 'error'; |
0 commit comments