Skip to content

Commit 5d7e5ec

Browse files
committed
pass through cookies on dev server
1 parent aa37e09 commit 5d7e5ec

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

dotcom-rendering/src/server/lib/get-content-from-url.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ const isStringTuple = (_: [string, unknown]): _ is [string, string] =>
88
/**
99
* Get DCR content from a `theguardian.com` URL.
1010
* Takes in optional `X-Gu-*` headers to send.
11+
* Returns the parsed JSON config and any cookies set by the request.
1112
*/
1213
async function getContentFromURL(
1314
url: URL,
1415
_headers: IncomingHttpHeaders,
15-
): Promise<unknown> {
16+
): Promise<{ config: unknown; cookies: string[] }> {
1617
// searchParams will only work for the first set of query params because 'url' is already a query param itself
1718
const searchparams = url.searchParams.toString();
1819

@@ -30,10 +31,13 @@ async function getContentFromURL(
3031
.filter(isStringTuple),
3132
);
3233

33-
// pick all the keys from the JSON except `html`
34-
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- we don't want `html` in the config
35-
const { html, ...config } = await fetch(jsonUrl, { headers })
36-
.then((response) => response.json())
34+
const { cookies, json } = await fetch(jsonUrl, { headers })
35+
.then(async (response) => {
36+
return {
37+
json: (await response.json()) as unknown,
38+
cookies: response.headers.getSetCookie(),
39+
};
40+
})
3741
.catch((error) => {
3842
if (isObject(error) && error.type === 'invalid-json') {
3943
throw new Error(
@@ -43,7 +47,11 @@ async function getContentFromURL(
4347
throw error;
4448
});
4549

46-
return config;
50+
// pick all the keys from the JSON except `html`
51+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- we don't want `html` in the config
52+
const { html, ...config } = json as Record<string, unknown>;
53+
54+
return { config, cookies };
4755
}
4856

4957
/**
@@ -65,8 +73,14 @@ export const getContentFromURLMiddleware: Handler = async (req, res, next) => {
6573
const url = new URL(
6674
'https://www.theguardian.com/crosswords/digital-edition',
6775
);
68-
const content = await getContentFromURL(url, req.headers);
69-
req.body = content;
76+
const { config, cookies } = await getContentFromURL(
77+
url,
78+
req.headers,
79+
);
80+
req.body = config;
81+
for (const cookie of cookies) {
82+
res.append('Set-Cookie', cookie);
83+
}
7084
next();
7185
} catch (error) {
7286
console.error(error);
@@ -88,7 +102,14 @@ export const getContentFromURLMiddleware: Handler = async (req, res, next) => {
88102
}
89103

90104
try {
91-
req.body = await getContentFromURL(sourceURL, req.headers);
105+
const { config, cookies } = await getContentFromURL(
106+
sourceURL,
107+
req.headers,
108+
);
109+
req.body = config;
110+
for (const cookie of cookies) {
111+
res.append('Set-Cookie', cookie);
112+
}
92113
} catch (error) {
93114
console.error(error);
94115
next(error);

0 commit comments

Comments
 (0)