From 6f66a885006406a3cc6b95be2b16fa499fa9f649 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 10 Aug 2022 11:41:02 -0400 Subject: [PATCH 1/4] experiment with returning response --- packages/remix/src/utils/instrumentServer.ts | 23 ++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/remix/src/utils/instrumentServer.ts b/packages/remix/src/utils/instrumentServer.ts index 2a498ec9e6d2..10adce7d2398 100644 --- a/packages/remix/src/utils/instrumentServer.ts +++ b/packages/remix/src/utils/instrumentServer.ts @@ -269,6 +269,20 @@ function getTraceAndBaggage(): { sentryTrace?: string; sentryBaggage?: string } return {}; } +export type JsonFunction = (data: Data, init?: number | ResponseInit) => Response; + +const json: JsonFunction = (data, init = {}) => { + const responseInit = typeof init === 'number' ? { status: init } : init; + const headers = new Headers(responseInit.headers); + if (!headers.has('Content-Type')) { + headers.set('Content-Type', 'application/json; charset=utf-8'); + } + return new Response(JSON.stringify(data), { + ...responseInit, + headers, + }); +}; + function makeWrappedRootLoader(origLoader: DataFunction): DataFunction { return async function (this: unknown, args: DataFunctionArgs): Promise { const res = await origLoader.call(this, args); @@ -277,12 +291,13 @@ function makeWrappedRootLoader(origLoader: DataFunction): DataFunction { // Note: `redirect` and `catch` responses do not have bodies to extract if (isResponse(res) && !isRedirectResponse(res) && !isCatchResponse(res)) { const data = await extractData(res); - if (typeof data === 'object') { - return { ...data, ...traceAndBaggage }; + return json( + { ...data, ...traceAndBaggage }, + { headers: res.headers, statusText: res.statusText, status: res.status }, + ); } else { - __DEBUG_BUILD__ && logger.warn('Skipping injection of trace and baggage as the response body is not an object'); - return data; + return json({ ...traceAndBaggage }, { headers: res.headers, statusText: res.statusText, status: res.status }); } } From 097a93820192bfd2b34c83ad145f55cbc272781e Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 10 Aug 2022 11:42:34 -0400 Subject: [PATCH 2/4] return res --- packages/remix/src/utils/instrumentServer.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/remix/src/utils/instrumentServer.ts b/packages/remix/src/utils/instrumentServer.ts index 10adce7d2398..e5259ef1c34d 100644 --- a/packages/remix/src/utils/instrumentServer.ts +++ b/packages/remix/src/utils/instrumentServer.ts @@ -297,7 +297,8 @@ function makeWrappedRootLoader(origLoader: DataFunction): DataFunction { { headers: res.headers, statusText: res.statusText, status: res.status }, ); } else { - return json({ ...traceAndBaggage }, { headers: res.headers, statusText: res.statusText, status: res.status }); + __DEBUG_BUILD__ && logger.warn('Skipping injection of trace and baggage as the response body is not an object'); + return res; } } From fb8cd459857e97c20fd30bca79645bc6fef87ab6 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 10 Aug 2022 11:48:43 -0400 Subject: [PATCH 3/4] revert tsconfig change --- packages/remix/test/integration/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/remix/test/integration/tsconfig.json b/packages/remix/test/integration/tsconfig.json index b1870b9c0d07..2129c1a599f6 100644 --- a/packages/remix/test/integration/tsconfig.json +++ b/packages/remix/test/integration/tsconfig.json @@ -4,7 +4,7 @@ "lib": ["DOM", "DOM.Iterable", "ES2019"], "isolatedModules": true, "esModuleInterop": true, - "jsx": "react", + "jsx": "react-jsx", "moduleResolution": "node", "resolveJsonModule": true, "target": "ES2019", From d96809afe8d03548f76ac216940d051b1fa4cfac Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 10 Aug 2022 11:50:09 -0400 Subject: [PATCH 4/4] add comments --- packages/remix/src/utils/instrumentServer.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/remix/src/utils/instrumentServer.ts b/packages/remix/src/utils/instrumentServer.ts index e5259ef1c34d..fec537d65a56 100644 --- a/packages/remix/src/utils/instrumentServer.ts +++ b/packages/remix/src/utils/instrumentServer.ts @@ -269,8 +269,17 @@ function getTraceAndBaggage(): { sentryTrace?: string; sentryBaggage?: string } return {}; } +// https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/responses.ts#L1-L4 export type JsonFunction = (data: Data, init?: number | ResponseInit) => Response; +/** + * This is a shortcut for creating `application/json` responses. Converts `data` + * to JSON and sets the `Content-Type` header. + * + * @see https://remix.run/api/remix#json + * + * https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/responses.ts#L12-L24 + */ const json: JsonFunction = (data, init = {}) => { const responseInit = typeof init === 'number' ? { status: init } : init; const headers = new Headers(responseInit.headers);