-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathserver.ts
105 lines (93 loc) · 3.67 KB
/
server.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { PrerenderManifest } from 'next/dist/build'
import { NodeRequestHandler, Options } from 'next/dist/server/next-server'
import {
netlifyApiFetch,
NextServerType,
normalizeRoute,
localizeRoute,
localizeDataRoute,
unlocalizeRoute,
} from './handlerUtils'
interface NetlifyConfig {
revalidateToken?: string
}
const getNetlifyNextServer = (NextServer: NextServerType) => {
class NetlifyNextServer extends NextServer {
private netlifyConfig: NetlifyConfig
private netlifyPrerenderManifest: PrerenderManifest
public constructor(options: Options, netlifyConfig: NetlifyConfig) {
super(options)
this.netlifyConfig = netlifyConfig
// copy the prerender manifest so it doesn't get mutated by Next.js
const manifest = this.getPrerenderManifest()
this.netlifyPrerenderManifest = {
...manifest,
routes: { ...manifest.routes },
dynamicRoutes: { ...manifest.dynamicRoutes },
}
}
public getRequestHandler(): NodeRequestHandler {
const handler = super.getRequestHandler()
return async (req, res, parsedUrl) => {
// preserve the URL before Next.js mutates it for i18n
const { url, headers } = req
// handle the original res.revalidate() request
await handler(req, res, parsedUrl)
// handle on-demand revalidation by purging the ODB cache
if (res.statusCode === 200 && headers['x-prerender-revalidate'] && this.netlifyConfig.revalidateToken) {
await this.netlifyRevalidate(url)
}
}
}
private async netlifyRevalidate(route: string) {
try {
// call netlify API to revalidate the path
const result = await netlifyApiFetch<{ ok: boolean; code: number; message: string }>({
endpoint: `sites/${process.env.SITE_ID}/refresh_on_demand_builders`,
payload: {
paths: this.getNetlifyPathsForRoute(route),
domain: this.hostname,
},
token: this.netlifyConfig.revalidateToken,
method: 'POST',
})
if (!result.ok) {
throw new Error(result.message)
}
} catch (error) {
console.log(`Error revalidating ${route}:`, error.message)
throw error
}
}
private getNetlifyPathsForRoute(route: string): string[] {
const { i18n } = this.nextConfig
const { routes, dynamicRoutes } = this.netlifyPrerenderManifest
// matches static routes
const normalizedRoute = normalizeRoute(i18n ? localizeRoute(route, i18n) : route)
if (normalizedRoute in routes) {
const { dataRoute } = routes[normalizedRoute]
const normalizedDataRoute = i18n ? localizeDataRoute(dataRoute, normalizedRoute) : dataRoute
return [route, normalizedDataRoute]
}
// matches dynamic routes
const unlocalizedRoute = i18n ? unlocalizeRoute(normalizedRoute, i18n) : normalizedRoute
for (const dynamicRoute in dynamicRoutes) {
const { dataRoute, routeRegex } = dynamicRoutes[dynamicRoute]
const matches = unlocalizedRoute.match(routeRegex)
if (matches && matches.length !== 0) {
// remove the first match, which is the full route
matches.shift()
// replace the dynamic segments with the actual values
const interpolatedDataRoute = dataRoute.replace(/\[(.*?)]/g, () => matches.shift())
const normalizedDataRoute = i18n
? localizeDataRoute(interpolatedDataRoute, normalizedRoute)
: interpolatedDataRoute
return [route, normalizedDataRoute]
}
}
throw new Error(`not an ISR route`)
}
}
return NetlifyNextServer
}
export { getNetlifyNextServer, NetlifyConfig }