-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch-content.ts
263 lines (221 loc) · 7.83 KB
/
fetch-content.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { v4 as uuid } from 'uuid';
import { PHASE_PRODUCTION_BUILD } from 'next/constants';
import { ContentProps } from 'types/content-props/_content-common';
import { makeErrorProps } from 'utils/make-error-props';
import { stripXpPathPrefix, xpServiceUrl } from 'utils/urls';
import { fetchWithTimeout, objectToQueryString } from 'srcCommon/fetch-utils';
import { MediaProps } from 'types/media';
import { logPageLoadError } from 'utils/errors';
import { stripLineBreaks } from 'utils/string';
import { logger } from 'srcCommon/logger';
import { RedisCache } from 'srcCommon/redis';
export type XpResponseProps = ContentProps | MediaProps;
// This message is returned from the sitecontent-service if the requested content
// was not found. Used to distinquish between content not found and the service
// itself not being found (ie if something is wrong with the nav.no app)
const NOT_FOUND_MSG_PREFIX = 'Site path not found';
const FETCH_TIMEOUT_MS = 60000;
const getXpCacheKey =
process.env.NODE_ENV !== 'development'
? () => ({
cacheKey: global.cacheKey,
})
: () => ({});
const redisCache = await new RedisCache().init(process.env.BUILD_ID, '');
const fetchConfig = {
headers: {
secret: process.env.SERVICE_SECRET,
'Cache-Control': 'no-store, no-cache',
},
};
type FetchSiteContentArgs = {
idOrPath: string;
time?: string;
isDraft?: boolean;
isPreview?: boolean;
locale?: string;
isArchived?: boolean;
};
const fetchSiteContent = async (props: FetchSiteContentArgs) => {
const { isArchived, time } = props;
if (isArchived) {
return fetchSiteContentArchive(props);
}
if (time) {
return fetchSiteContentVersion(props);
}
return fetchSiteContentStandard(props);
};
const fetchSiteContentStandard = async ({
idOrPath,
isDraft = false,
isPreview = false,
locale,
}: FetchSiteContentArgs) => {
const params = objectToQueryString({
id: idOrPath,
...(isDraft && { branch: 'draft' }),
...(!isDraft && getXpCacheKey()), // We don't want to use backend-cache for draft content requests
...(isPreview && { preview: true }),
...(locale && { locale }),
});
const url = `${xpServiceUrl}/sitecontent${params}`;
logger.info(`Fetching content from ${url}`);
return fetchWithTimeout(url, FETCH_TIMEOUT_MS, fetchConfig).catch((e) => {
logger.info(`Sitecontent fetch error for ${url}: ${e}`);
return null;
});
};
const fetchSiteContentVersion = async ({
idOrPath,
time,
isDraft = false,
locale,
}: FetchSiteContentArgs) => {
const params = objectToQueryString({
id: idOrPath,
...(isDraft && { branch: 'draft' }),
...(locale && { locale }),
time,
});
const url = `${xpServiceUrl}/sitecontentVersions${params}`;
logger.info(`Fetching version history content from ${url}`);
return fetchWithTimeout(url, FETCH_TIMEOUT_MS, fetchConfig).catch((e) => {
logger.info(`Sitecontent version fetch error: ${e}`);
return null;
});
};
const fetchSiteContentArchive = async ({ idOrPath, locale, time }: FetchSiteContentArgs) => {
const params = objectToQueryString({
id: idOrPath,
...(locale && { locale }),
...(time && { time }),
});
const url = `${xpServiceUrl}/sitecontentArchive${params}`;
logger.info(`Fetching archived content from ${url}`);
return fetchWithTimeout(url, FETCH_TIMEOUT_MS, fetchConfig).catch((e) => {
logger.info(`Sitecontent archive fetch error: ${e}`);
return null;
});
};
// For pages generated at build-time, any errors thrown will abort the build process.
// Retry a few times, and just throw a generic server error if anything fails.
const fetchAndHandleErrorsBuildtime = async (
props: FetchSiteContentArgs & { retries?: number }
): Promise<ContentProps> => {
const { idOrPath, retries = 5 } = props;
return fetchSiteContentStandard({ idOrPath }).then((res) => {
if (res?.ok) {
return res.json();
}
if (retries > 0) {
return fetchAndHandleErrorsBuildtime({
idOrPath,
retries: retries - 1,
});
}
return makeErrorProps(
idOrPath,
`Build-time fetch error: ${res.status} - ${res.statusText}`
);
});
};
const isCachableRequest = ({ isDraft, isArchived, time, isPreview }: FetchSiteContentArgs) =>
!(isDraft || isArchived || time || isPreview);
const fetchAndHandleErrorsRuntime = async (
props: FetchSiteContentArgs
): Promise<XpResponseProps> => {
const isCachable = isCachableRequest(props);
const { idOrPath } = props;
if (isCachable) {
const cachedResponse = await redisCache.getResponse(stripXpPathPrefix(idOrPath));
if (cachedResponse) {
logger.info(`Response cache hit for ${idOrPath}`);
return cachedResponse;
}
}
const res = await fetchSiteContent(props);
const errorId = uuid();
if (!res) {
return makeErrorProps(idOrPath, undefined, 500, errorId);
}
const isJson = res.headers?.get('content-type')?.includes?.('application/json');
if (res.ok && isJson) {
const json = await res.json();
if (isCachable) {
redisCache.setResponse(stripXpPathPrefix(idOrPath), json);
}
return json;
}
if (res.ok) {
logPageLoadError(
errorId,
`Fetch error: Received an ok-response for ${idOrPath}, but did not receive JSON content`
);
return makeErrorProps(idOrPath, undefined, 500, errorId);
}
const errorMsg = isJson ? (await res.json()).message || res.statusText : res.statusText;
if (res.status === 404) {
// If we get an unexpected 404-error from the sitecontent-service (meaning the service itself
// was not found), treat the error as a server error in order to prevent cache-invalidation
if (!errorMsg.startsWith(NOT_FOUND_MSG_PREFIX)) {
logPageLoadError(
errorId,
`Fetch error: ${res.status} - Failed to fetch content from ${idOrPath} - unexpected 404-response from sitecontent service: ${errorMsg}`
);
return makeErrorProps(idOrPath, errorMsg, 503, errorId);
}
// Regular 404 should not be logged as errors
logger.info(`Content not found ${stripLineBreaks(idOrPath)}`);
return makeErrorProps(idOrPath, undefined, 404, errorId);
}
logPageLoadError(
errorId,
`Fetch error: ${res.status} - Failed to fetch content from ${idOrPath}: ${errorMsg}`
);
return makeErrorProps(idOrPath, errorMsg, res.status, errorId);
};
const fetchAndHandleErrors =
process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD
? fetchAndHandleErrorsBuildtime
: fetchAndHandleErrorsRuntime;
type FetchPageArgs = {
idOrPath: string;
isDraft?: boolean;
timeRequested?: string;
isPreview?: boolean;
locale?: string;
isArchived?: boolean;
};
export const fetchPage = async ({
idOrPath,
timeRequested,
isDraft = false,
isPreview = false,
locale,
isArchived,
}: FetchPageArgs): Promise<XpResponseProps> => {
const content = await fetchAndHandleErrors({
idOrPath,
isDraft,
isPreview,
time: timeRequested,
locale,
isArchived,
});
if (!content?.type) {
const errorId = uuid();
logPageLoadError(
errorId,
`Fetch error: Unknown error for ${idOrPath} - no valid content received`
);
return makeErrorProps(idOrPath, undefined, 500, errorId);
}
return {
...content,
isDraft,
serverEnv: process.env.ENV,
isPagePreview: isPreview,
...(timeRequested && { timeRequested: timeRequested }),
};
};