Skip to content

Commit

Permalink
feat(remix): Export a type to use for MetaFunction parameters (#9493)
Browse files Browse the repository at this point in the history
Exported a type to be used like:

```typescript
export const meta = ({ data }: SentryMetaArgs<MetaFunction<typeof loader>>) => {
  return [
    {
      env: data.ENV,
    },
    {
      name: 'sentry-trace',
      content: data.sentryTrace,
    },
    {
      name: 'baggage',
      content: data.sentryBaggage,
    },
  ];
};

```

So the types from `loader` function and our injections are both
available in type checking.

Also, updated e2e tests to include `meta` injection checks for root and
sub-routes.
  • Loading branch information
onurtemizkan authored Nov 9, 2023
1 parent 8b84905 commit b531549
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cssBundleHref } from '@remix-run/css-bundle';
import { json, LinksFunction } from '@remix-run/node';
import { json, LinksFunction, MetaFunction } from '@remix-run/node';
import {
Links,
LiveReload,
Expand All @@ -11,6 +11,7 @@ import {
useRouteError,
} from '@remix-run/react';
import { captureRemixErrorBoundaryError, withSentry } from '@sentry/remix';
import type { SentryMetaArgs } from '@sentry/remix';

export const links: LinksFunction = () => [...(cssBundleHref ? [{ rel: 'stylesheet', href: cssBundleHref }] : [])];

Expand All @@ -22,6 +23,22 @@ export const loader = () => {
});
};

export const meta = ({ data }: SentryMetaArgs<MetaFunction<typeof loader>>) => {
return [
{
env: data.ENV,
},
{
name: 'sentry-trace',
content: data.sentryTrace,
},
{
name: 'baggage',
content: data.sentryBaggage,
},
];
};

export function ErrorBoundary() {
const error = useRouteError();
const eventId = captureRemixErrorBoundaryError(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,31 @@ test('Sends a client-side ErrorBoundary exception to Sentry', async ({ page }) =
)
.toBe(200);
});

test('Renders `sentry-trace` and `baggage` meta tags for the root route', async ({ page }) => {
await page.goto('/');

const sentryTraceMetaTag = await page.waitForSelector('meta[name="sentry-trace"]', {
state: 'attached',
});
const baggageMetaTag = await page.waitForSelector('meta[name="baggage"]', {
state: 'attached',
});

expect(sentryTraceMetaTag).toBeTruthy();
expect(baggageMetaTag).toBeTruthy();
});

test('Renders `sentry-trace` and `baggage` meta tags for a sub-route', async ({ page }) => {
await page.goto('/user/123');

const sentryTraceMetaTag = await page.waitForSelector('meta[name="sentry-trace"]', {
state: 'attached',
});
const baggageMetaTag = await page.waitForSelector('meta[name="baggage"]', {
state: 'attached',
});

expect(sentryTraceMetaTag).toBeTruthy();
expect(baggageMetaTag).toBeTruthy();
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cssBundleHref } from '@remix-run/css-bundle';
import { json, LinksFunction } from '@remix-run/node';
import { json, LinksFunction, MetaFunction } from '@remix-run/node';
import {
Links,
LiveReload,
Expand All @@ -22,6 +22,19 @@ export const loader = () => {
});
};

export const meta: MetaFunction = ({ data }) => {
return [
{
name: 'sentry-trace',
content: data.sentryTrace,
},
{
name: 'baggage',
content: data.sentryBaggage,
},
];
};

export function ErrorBoundary() {
const error = useRouteError();
const eventId = captureRemixErrorBoundaryError(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,31 @@ test('Sends a client-side ErrorBoundary exception to Sentry', async ({ page }) =
)
.toBe(200);
});

test('Renders `sentry-trace` and `baggage` meta tags for the root route', async ({ page }) => {
await page.goto('/');

const sentryTraceMetaTag = await page.waitForSelector('meta[name="sentry-trace"]', {
state: 'attached',
});
const baggageMetaTag = await page.waitForSelector('meta[name="baggage"]', {
state: 'attached',
});

expect(sentryTraceMetaTag).toBeTruthy();
expect(baggageMetaTag).toBeTruthy();
});

test('Renders `sentry-trace` and `baggage` meta tags for a sub-route', async ({ page }) => {
await page.goto('/user/123');

const sentryTraceMetaTag = await page.waitForSelector('meta[name="sentry-trace"]', {
state: 'attached',
});
const baggageMetaTag = await page.waitForSelector('meta[name="baggage"]', {
state: 'attached',
});

expect(sentryTraceMetaTag).toBeTruthy();
expect(baggageMetaTag).toBeTruthy();
});
2 changes: 2 additions & 0 deletions packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export { remixRouterInstrumentation, withSentry } from './client/performance';
export { captureRemixErrorBoundaryError } from './client/errors';
export { wrapExpressCreateRequestHandler } from './utils/serverAdapters/express';

export type { SentryMetaArgs } from './utils/types';

function sdkAlreadyInitialized(): boolean {
const hub = getCurrentHub();
return !!hub.getClient();
Expand Down
6 changes: 6 additions & 0 deletions packages/remix/src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type SentryMetaArgs<MetaFN extends (...args: any) => any> = Parameters<MetaFN>[0] & {
data: {
sentryTrace: string;
sentryBaggage: string;
};
};

0 comments on commit b531549

Please sign in to comment.