-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathroute.ts
More file actions
79 lines (64 loc) · 3.03 KB
/
route.ts
File metadata and controls
79 lines (64 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { deflateSync } from 'node:zlib';
import matter from 'gray-matter';
import { dynamicRouter } from '#site/next.dynamic.mjs';
import { defaultLocale } from '#site/next.locales.mjs';
import { parseRichTextIntoPlainText } from '#site/util/string';
// This is the Route Handler for the `GET` method which handles the request
// for a digest and metadata of all existing pages on Node.js Website
// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers
export const GET = async () => {
// Retrieves all available routes for the default locale
const allAvailbleRoutes = await dynamicRouter.getRoutesByLanguage(
defaultLocale.code
);
// We exclude the blog routes from the available pages metadata
// as they are generated separately and are not part of the static pages
// and are not part of the static pages metadata
const routesExceptBlog = allAvailbleRoutes.filter(
route => !route.startsWith('blog')
);
const availablePagesMetadata = routesExceptBlog.map(async pathname => {
const { source, filename } = await dynamicRouter.getMarkdownFile(
defaultLocale.code,
pathname
);
// Gets the title and the Description from the Page Metadata
const { title, description } = await dynamicRouter.getPageMetadata(
defaultLocale.code,
pathname
);
// Parser the Markdown source with `gray-matter` and then only
// grabs the markdown content and cleanses it by removing HTML/JSX tags
// removing empty/blank lines or lines just with spaces and trims each line
// from leading and trailing paddings/spaces
const cleanedContent = parseRichTextIntoPlainText(matter(source).content);
// Deflates a String into a base64 string-encoded (zlib compressed)
const content = deflateSync(cleanedContent).toString('base64');
// Returns metadata of each page available on the Website
return {
filename,
pathname,
title,
description,
content,
};
});
const data = await Promise.all(availablePagesMetadata);
return Response.json(data);
};
// This function generates the static paths that come from the dynamic segments
// `[locale]/next-data/page-data/` and returns an array of all available static paths
// This is used for ISR static validation and generation
export const generateStaticParams = async () => [
{ locale: defaultLocale.code },
];
// Enforces that only the paths from `generateStaticParams` are allowed, giving 404 on the contrary
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams
export const dynamicParams = false;
// Enforces that this route is used as static rendering
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic
export const dynamic = 'error';
// Ensures that this endpoint is invalidated and re-executed every X minutes
// so that when new deployments happen, the data is refreshed
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#revalidate
export const revalidate = 300;