-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(remix): Support merging json
responses from root loader functions.
#5548
Changes from all commits
f4032f6
80d1711
2dff4d6
63419d2
8919b68
2fae5d2
57fb656
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,7 +100,7 @@ export interface RouteMatch<Route> { | |
} | ||
|
||
// Taken from Remix Implementation | ||
// https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/responses.ts#L54-L62 | ||
// https://github.com/remix-run/remix/blob/32300ec6e6e8025602cea63e17a2201989589eab/packages/remix-server-runtime/responses.ts#L60-L77 | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function isResponse(value: any): value is Response { | ||
return ( | ||
|
@@ -114,6 +114,15 @@ function isResponse(value: any): value is Response { | |
); | ||
} | ||
|
||
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]); | ||
function isRedirectResponse(response: Response): boolean { | ||
return redirectStatusCodes.has(response.status); | ||
} | ||
|
||
function isCatchResponse(response: Response): boolean { | ||
return response.headers.get('X-Remix-Catch') != null; | ||
} | ||
|
||
// Based on Remix Implementation | ||
// https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/data.ts#L131-L145 | ||
function extractData(response: Response): Promise<unknown> { | ||
|
@@ -207,6 +216,7 @@ function makeWrappedDataFunction(origFn: DataFunction, name: 'action' | 'loader' | |
try { | ||
const span = activeTransaction.startChild({ | ||
op: `remix.server.${name}`, | ||
// TODO: Consider using the `id` of the route this function belongs to | ||
description: activeTransaction.name, | ||
tags: { | ||
name, | ||
|
@@ -235,8 +245,8 @@ function makeWrappedAction(origAction: DataFunction): DataFunction { | |
return makeWrappedDataFunction(origAction, 'action'); | ||
} | ||
|
||
function makeWrappedLoader(origAction: DataFunction): DataFunction { | ||
return makeWrappedDataFunction(origAction, 'loader'); | ||
function makeWrappedLoader(origLoader: DataFunction): DataFunction { | ||
return makeWrappedDataFunction(origLoader, 'loader'); | ||
} | ||
|
||
function getTraceAndBaggage(): { sentryTrace?: string; sentryBaggage?: string } { | ||
|
@@ -262,8 +272,21 @@ function getTraceAndBaggage(): { sentryTrace?: string; sentryBaggage?: string } | |
function makeWrappedRootLoader(origLoader: DataFunction): DataFunction { | ||
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> { | ||
const res = await origLoader.call(this, args); | ||
const traceAndBaggage = getTraceAndBaggage(); | ||
|
||
// 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 }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to even return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait I'm dumb - yes we do, nvm. |
||
} else { | ||
__DEBUG_BUILD__ && logger.warn('Skipping injection of trace and baggage as the response body is not an object'); | ||
return data; | ||
} | ||
} | ||
|
||
return { ...res, ...getTraceAndBaggage() }; | ||
return { ...res, ...traceAndBaggage }; | ||
}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import type { MetaFunction } from '@remix-run/node'; | ||
import { MetaFunction, LoaderFunction, json, redirect } from '@remix-run/node'; | ||
import { Links, LiveReload, Meta, Outlet, Scripts, ScrollRestoration } from '@remix-run/react'; | ||
import { withSentry } from '@sentry/remix'; | ||
|
||
|
@@ -10,7 +10,35 @@ export const meta: MetaFunction = ({ data }) => ({ | |
baggage: data.sentryBaggage, | ||
}); | ||
|
||
export function App() { | ||
export const loader: LoaderFunction = async ({ request }) => { | ||
const url = new URL(request.url); | ||
const type = url.searchParams.get('type'); | ||
|
||
switch (type) { | ||
case 'empty': | ||
return {}; | ||
case 'plain': | ||
return { | ||
data_one: [], | ||
data_two: 'a string', | ||
}; | ||
case 'json': | ||
return json({ data_one: [], data_two: 'a string' }, { headers: { 'Cache-Control': 'max-age=300' } }); | ||
case 'null': | ||
return null; | ||
case 'undefined': | ||
return undefined; | ||
case 'throwRedirect': | ||
throw redirect('/?type=plain'); | ||
case 'returnRedirect': | ||
return redirect('/?type=plain'); | ||
default: { | ||
return {}; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need a |
||
}; | ||
|
||
function App() { | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { test, expect, Page } from '@playwright/test'; | ||
|
||
async function getRouteData(page: Page): Promise<any> { | ||
return page.evaluate('window.__remixContext.routeData').catch(err => { | ||
console.warn(err); | ||
|
||
return {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log out the error? |
||
}); | ||
} | ||
|
||
async function extractTraceAndBaggageFromMeta( | ||
page: Page, | ||
): Promise<{ sentryTrace?: string | null; sentryBaggage?: string | null }> { | ||
const sentryTraceTag = await page.$('meta[name="sentry-trace"]'); | ||
const sentryTraceContent = await sentryTraceTag?.getAttribute('content'); | ||
|
||
const sentryBaggageTag = await page.$('meta[name="baggage"]'); | ||
AbhiPrasad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const sentryBaggageContent = await sentryBaggageTag?.getAttribute('content'); | ||
|
||
return { sentryTrace: sentryTraceContent, sentryBaggage: sentryBaggageContent }; | ||
} | ||
|
||
test('should inject `sentry-trace` and `baggage` into root loader returning an empty object.', async ({ page }) => { | ||
await page.goto('/?type=empty'); | ||
|
||
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page); | ||
|
||
expect(sentryTrace).toEqual(expect.any(String)); | ||
expect(sentryBaggage).toEqual(expect.any(String)); | ||
|
||
const rootData = (await getRouteData(page))['root']; | ||
|
||
expect(rootData).toMatchObject({ | ||
sentryTrace, | ||
sentryBaggage, | ||
}); | ||
}); | ||
|
||
test('should inject `sentry-trace` and `baggage` into root loader returning a plain object.', async ({ page }) => { | ||
await page.goto('/?type=plain'); | ||
|
||
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page); | ||
|
||
expect(sentryTrace).toEqual(expect.any(String)); | ||
expect(sentryBaggage).toEqual(expect.any(String)); | ||
|
||
const rootData = (await getRouteData(page))['root']; | ||
|
||
expect(rootData).toMatchObject({ | ||
data_one: [], | ||
data_two: 'a string', | ||
sentryTrace: sentryTrace, | ||
sentryBaggage: sentryBaggage, | ||
}); | ||
}); | ||
|
||
test('should inject `sentry-trace` and `baggage` into root loader returning a `JSON response`.', async ({ page }) => { | ||
await page.goto('/?type=json'); | ||
|
||
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page); | ||
|
||
expect(sentryTrace).toEqual(expect.any(String)); | ||
expect(sentryBaggage).toEqual(expect.any(String)); | ||
|
||
const rootData = (await getRouteData(page))['root']; | ||
|
||
expect(rootData).toMatchObject({ | ||
data_one: [], | ||
data_two: 'a string', | ||
sentryTrace: sentryTrace, | ||
sentryBaggage: sentryBaggage, | ||
}); | ||
}); | ||
|
||
test('should inject `sentry-trace` and `baggage` into root loader returning `null`.', async ({ page }) => { | ||
await page.goto('/?type=null'); | ||
|
||
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page); | ||
|
||
expect(sentryTrace).toEqual(expect.any(String)); | ||
expect(sentryBaggage).toEqual(expect.any(String)); | ||
|
||
const rootData = (await getRouteData(page))['root']; | ||
|
||
expect(rootData).toMatchObject({ | ||
sentryTrace: sentryTrace, | ||
sentryBaggage: sentryBaggage, | ||
}); | ||
}); | ||
|
||
test('should inject `sentry-trace` and `baggage` into root loader returning `undefined`.', async ({ page }) => { | ||
await page.goto('/?type=undefined'); | ||
|
||
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page); | ||
|
||
expect(sentryTrace).toEqual(expect.any(String)); | ||
expect(sentryBaggage).toEqual(expect.any(String)); | ||
|
||
const rootData = (await getRouteData(page))['root']; | ||
|
||
expect(rootData).toMatchObject({ | ||
sentryTrace: sentryTrace, | ||
sentryBaggage: sentryBaggage, | ||
}); | ||
}); | ||
|
||
test('should inject `sentry-trace` and `baggage` into root loader throwing a redirection to a plain object.', async ({ | ||
page, | ||
}) => { | ||
await page.goto('/?type=throwRedirect'); | ||
|
||
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page); | ||
|
||
expect(sentryTrace).toEqual(expect.any(String)); | ||
expect(sentryBaggage).toEqual(expect.any(String)); | ||
|
||
const rootData = (await getRouteData(page))['root']; | ||
|
||
expect(rootData).toMatchObject({ | ||
sentryTrace: sentryTrace, | ||
sentryBaggage: sentryBaggage, | ||
}); | ||
}); | ||
|
||
test('should inject `sentry-trace` and `baggage` into root loader returning a redirection to a plain object', async ({ | ||
page, | ||
}) => { | ||
await page.goto('/?type=returnRedirect'); | ||
|
||
const { sentryTrace, sentryBaggage } = await extractTraceAndBaggageFromMeta(page); | ||
|
||
expect(sentryTrace).toEqual(expect.any(String)); | ||
expect(sentryBaggage).toEqual(expect.any(String)); | ||
|
||
const rootData = (await getRouteData(page))['root']; | ||
|
||
expect(rootData).toMatchObject({ | ||
sentryTrace: sentryTrace, | ||
sentryBaggage: sentryBaggage, | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,23 +9,6 @@ import { | |
jest.spyOn(console, 'error').mockImplementation(); | ||
|
||
describe('Remix API Loaders', () => { | ||
it('does not add a loader if there is not one defined.', async () => { | ||
const baseURL = await runServer(); | ||
const url = `${baseURL}/`; | ||
const envelope = await getEnvelopeRequest(url); | ||
const transaction = envelope[2]; | ||
|
||
assertSentryTransaction(transaction, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did we delete this test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We no longer have a case where there's no Trying to figure out now, will add that to this PR, if it doesn't require a large structural change. |
||
transaction: 'root', | ||
spans: [ | ||
{ | ||
description: 'root', | ||
op: 'remix.server.documentRequest', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('reports an error thrown from the loader', async () => { | ||
const baseURL = await runServer(); | ||
const url = `${baseURL}/loader-json-response/-2`; | ||
|
@@ -75,6 +58,13 @@ describe('Remix API Loaders', () => { | |
source: 'route', | ||
}, | ||
spans: [ | ||
// TODO: These two spans look exactly the same, but they are not. | ||
// One is from the parent route, and the other is from the route we are reaching. | ||
// We need to pass the names of the routes as their descriptions while wrapping loaders and actions. | ||
{ | ||
description: 'routes/loader-json-response/$id', | ||
op: 'remix.server.loader', | ||
}, | ||
{ | ||
description: 'routes/loader-json-response/$id', | ||
op: 'remix.server.loader', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AbhiPrasad, the
else
case of this is a bit weird (when data is primitive). It doesn't sound right to return a primitive from aloader
, but TS definitions allow it.So, would it make sense if we skip injection in such cases?
It may mess up the response when we spread it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I would rather we skip injection here.