Skip to content

Commit 6ece550

Browse files
committed
add pages router example in next 13 (v7)
1 parent 0ee3a3c commit 6ece550

File tree

9 files changed

+947
-2
lines changed

9 files changed

+947
-2
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
SENTRY_DSN=
1+
SENTRY_DSN=
2+
NEXT_PUBLIC_SENTRY_DSN=
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { NextApiRequest, NextApiResponse } from 'next';
2+
import * as Sentry from '@sentry/nextjs';
3+
4+
export default function handler(
5+
req: NextApiRequest,
6+
res: NextApiResponse<{ exceptionId: string; paramWas: string }>,
7+
) {
8+
const exceptionId = Sentry.captureException(new Error('This is an error'));
9+
res.status(500).json({ exceptionId, paramWas: req.query.param?.toString() || '' });
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { NextApiRequest, NextApiResponse } from 'next';
2+
3+
export default function handler(req: NextApiRequest, res: NextApiResponse<{ paramWas: string }>) {
4+
res.status(200).json({ paramWas: req.query.param?.toString() || '' });
5+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { GetServerSideProps, NextPage } from 'next';
2+
import React from 'react';
3+
import { useFetchData } from '@/utils/fetchData';
4+
5+
const displayData = (data: any) => (data ? JSON.stringify(data, null, 2) : 'No data');
6+
7+
/***************************************************************
8+
* IMPORTANT: when running the pages-router, you have to set `appDir: false` in next.config.js
9+
****************************************************************/
10+
11+
const Page: NextPage<{ propFromServerSide: string }> = ({ propFromServerSide }) => {
12+
const { data: dataParamSuccess, fetchData: testParamSuccess } = useFetchData(
13+
'/api/pages-router/test-param-success/1337',
14+
);
15+
const { data: dataParamError, fetchData: testParamError } = useFetchData(
16+
'/api/pages-router/test-param-error/1337',
17+
);
18+
19+
return (
20+
<>
21+
<p>{`Prop from 'getServerSideProps': ${propFromServerSide}`}</p>
22+
23+
<button onClick={() => testParamSuccess()}>Test Param Success</button>
24+
<p>{displayData(dataParamSuccess)}</p>
25+
26+
<button onClick={() => testParamError()}>Test Param Error</button>
27+
<p>{displayData(dataParamError)}</p>
28+
</>
29+
);
30+
};
31+
32+
export default Page;
33+
34+
export const getServerSideProps = (async () => {
35+
return { props: { propFromServerSide: 'success' } };
36+
}) satisfies GetServerSideProps<{ propFromServerSide: string }>;

apps/nextjs-13_2_0/sentry.client.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Sentry from '@sentry/nextjs';
22

33
Sentry.init({
44
environment: 'qa', // dynamic sampling bias to keep transactions
5-
dsn: process.env.SENTRY_DSN,
5+
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
66
includeLocalVariables: true,
77
tunnel: `http://localhost:3031/`, // proxy server
88
tracesSampleRate: 1,

0 commit comments

Comments
 (0)