-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nextjs): Connect trace between data-fetching methods and pageload #5655
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d54314f
feat(nextjs): Connect trace between data-fetching methods and pageload
b91ff50
Parse baggage correctly
31c51e6
Please linter
5b310dd
Fix tests
faaba12
Remove redundant separate data flags
0f205ec
Rename test
df1d3d9
Add webpack plugin experiment to integration tests
3f192f7
Add clientside getServerSideProps integration test
5a76709
Add clientside getInitialProps integration test
663f765
Add serverside integration tests
cee6dc3
Add additional assertions
5c86e9a
Merge remote-tracking branch 'origin/master' into lforst-next-connect…
019751b
Fix error capturing
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,20 +22,14 @@ type StartTransactionCb = (context: TransactionContext) => Transaction | undefin | |
* Describes data located in the __NEXT_DATA__ script tag. This tag is present on every page of a Next.js app. | ||
*/ | ||
interface SentryEnhancedNextData extends NextData { | ||
// contains props returned by `getInitialProps` - except for `pageProps`, these are the props that got returned by `getServerSideProps` or `getStaticProps` | ||
props: { | ||
_sentryGetInitialPropsTraceData?: string; // trace parent info, if injected by server-side `getInitialProps` | ||
_sentryGetInitialPropsBaggage?: string; // baggage, if injected by server-side `getInitialProps` | ||
Comment on lines
-25
to
-28
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. I got this wrong in a previous PR |
||
pageProps?: { | ||
_sentryGetServerSidePropsTraceData?: string; // trace parent info, if injected by server-side `getServerSideProps` | ||
_sentryGetServerSidePropsBaggage?: string; // baggage, if injected by server-side `getServerSideProps` | ||
|
||
// The following two values are only injected in a very special case with the following conditions: | ||
_sentryTraceData?: string; // trace parent info, if injected by a data-fetcher | ||
_sentryBaggage?: string; // baggage, if injected by a data-fetcher | ||
// These two values are only injected by `getStaticProps` in a very special case with the following conditions: | ||
// 1. The page's `getStaticPaths` method must have returned `fallback: 'blocking'`. | ||
// 2. The requested page must be a "miss" in terms of "Incremental Static Regeneration", meaning the requested page has not been generated before. | ||
// In this case, a page is requested and only served when `getStaticProps` is done. There is not even a fallback page or similar. | ||
_sentryGetStaticPropsTraceData?: string; // trace parent info, if injected by server-side `getStaticProps` | ||
_sentryGetStaticPropsBaggage?: string; // baggage, if injected by server-side `getStaticProps` | ||
}; | ||
}; | ||
} | ||
|
@@ -79,29 +73,19 @@ function extractNextDataTagInformation(): NextDataTagInfo { | |
|
||
const { page, query, props } = nextData; | ||
|
||
// `nextData.page` always contains the parameterized route | ||
// `nextData.page` always contains the parameterized route - except for when an error occurs in a data fetching | ||
// function, then it is "/_error", but that isn't a problem since users know which route threw by looking at the | ||
// parent transaction | ||
nextDataTagInfo.route = page; | ||
nextDataTagInfo.params = query; | ||
|
||
if (props) { | ||
const { pageProps } = props; | ||
|
||
const getInitialPropsBaggage = props._sentryGetInitialPropsBaggage; | ||
const getServerSidePropsBaggage = pageProps && pageProps._sentryGetServerSidePropsBaggage; | ||
const getStaticPropsBaggage = pageProps && pageProps._sentryGetStaticPropsBaggage; | ||
// Ordering of the following shouldn't matter but `getInitialProps` generally runs before `getServerSideProps` or `getStaticProps` so we give it priority. | ||
const baggage = getInitialPropsBaggage || getServerSidePropsBaggage || getStaticPropsBaggage; | ||
if (baggage) { | ||
nextDataTagInfo.baggage = baggage; | ||
if (props && props.pageProps) { | ||
if (props.pageProps._sentryBaggage) { | ||
nextDataTagInfo.baggage = props.pageProps._sentryBaggage; | ||
} | ||
|
||
const getInitialPropsTraceData = props._sentryGetInitialPropsTraceData; | ||
const getServerSidePropsTraceData = pageProps && pageProps._sentryGetServerSidePropsTraceData; | ||
const getStaticPropsTraceData = pageProps && pageProps._sentryGetStaticPropsTraceData; | ||
// Ordering of the following shouldn't matter but `getInitialProps` generally runs before `getServerSideProps` or `getStaticProps` so we give it priority. | ||
const traceData = getInitialPropsTraceData || getServerSidePropsTraceData || getStaticPropsTraceData; | ||
if (traceData) { | ||
nextDataTagInfo.traceParentData = extractTraceparentData(traceData); | ||
if (props.pageProps._sentryTraceData) { | ||
nextDataTagInfo.traceParentData = extractTraceparentData(props.pageProps._sentryTraceData); | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/nextjs/test/integration/pages/[id]/withInitialProps.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const WithInitialPropsPage = ({ data }: { data: string }) => <h1>WithInitialPropsPage {data}</h1>; | ||
|
||
WithInitialPropsPage.getInitialProps = () => { | ||
return { data: '[some getInitialProps data]' }; | ||
}; | ||
|
||
export default WithInitialPropsPage; |
7 changes: 7 additions & 0 deletions
7
packages/nextjs/test/integration/pages/[id]/withServerSideProps.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const WithServerSidePropsPage = ({ data }: { data: string }) => <h1>WithServerSidePropsPage {data}</h1>; | ||
|
||
export async function getServerSideProps() { | ||
return { props: { data: '[some getServerSideProps data]' } }; | ||
} | ||
|
||
export default WithServerSidePropsPage; |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why did you choose to wrap this line in a function? (Same goes for the setter.)
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.
I don't know to be honest. It just made sense to me to put this behind a little bit of abstraction to provide more context why this field exists on the
req
object. Feel free to remove it in the future though! I think the ambient type (declare module 'http' {
at the top of the file) provides enough context.