Title: Sentry does not add sentry-header due to Webpack import re-ordering · Issue #4439 · getsentry/sentry-javascript · GitHub
Open Graph Title: Sentry does not add sentry-header due to Webpack import re-ordering · Issue #4439 · getsentry/sentry-javascript
X Title: Sentry does not add sentry-header due to Webpack import re-ordering · Issue #4439 · getsentry/sentry-javascript
Description: Description Sentry documentation suggests to initialize Sentry "as early as possible", and the example does it after all application imports: import Vue from "vue"; import Router from "vue-router"; import * as Sentry from "@sentry/vue"; ...
Open Graph Description: Description Sentry documentation suggests to initialize Sentry "as early as possible", and the example does it after all application imports: import Vue from "vue"; import Router from "vue-router";...
X Description: Description Sentry documentation suggests to initialize Sentry "as early as possible", and the example does it after all application imports: import Vue from "vue"; import Route...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/4439
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Sentry does not add sentry-header due to Webpack import re-ordering","articleBody":"# Description\r\n\r\nSentry documentation suggests to initialize Sentry \"as early as possible\", and the example does it _after all application imports_:\r\n\r\n```ts\r\nimport Vue from \"vue\";\r\nimport Router from \"vue-router\";\r\nimport * as Sentry from \"@sentry/vue\";\r\nimport { Integrations } from \"@sentry/tracing\";\r\n\r\nVue.use(Router);\r\n\r\nconst router = new Router();\r\n\r\n// Sentry is initialized after imports\r\nSentry.init(...)\r\n```\r\n\r\nThis approach will cause Sentry to be initialized _too late_ in certain (perhaps common) scenarios, and causes the `sentry-header` not to be sent when using Api clients that read `window.fetch` during its initialization.\r\n\r\nBecause this is unexpected behavior that is hard to diagnose, and the solution is also non-obvious, I would propose to add caution remarks in the Documentation, or to alter the documented code snippet.\r\n\r\n### Root Cause: Sentry should be initialized before imports that use `window.fetch`\r\n\r\nApplication imports will commonly end up at the top. Either by convention, or by force when using webpack. Vue JS does this.\r\n\r\nA common scenario is importing an API module that initializes an API client by passing `window.fetch` into the constructor.\r\n\r\n```ts\r\n// api.ts\r\nexport default const apiClient = new ApiClient('https://api.example.com', window.fetch);\r\n```\r\n\r\n```ts\r\n// main.ts\r\nimport Vue from 'vue'\r\nimport { ApiModule} from '@/api' // this module reads `window.fetch` and won't add any sentry headers.\r\nimport * as Sentry from '@sentry/vue'\r\nimport { Integrations } from '@sentry/tracing'\r\n\r\nvar router = ...\r\n\r\n// It looks like we're initializing Sentry as early as possible, but we're not.\r\nSentry.init(...)\r\n\r\nVue.use(AuthModule)\r\n// ... rest of initialization\r\n``` \r\n\r\nIn this scenario, the initialization is too late.\r\n\r\n## Moving the logic before the import doesn't work\r\n\r\n```ts\r\nimport Vue from 'vue'\r\n\r\n// Trying to initialize before loading the ApiModule.\r\n// Webpack (or babel?) will (secretly) move the block of code AFTER the imports!\r\nSentry.init(...)\r\n\r\nimport { ApiModule } from '@/api'\r\nimport * as Sentry from '@sentry/vue'\r\nimport { Integrations } from '@sentry/tracing'\r\n``` \r\n\r\nWebpack will forcefully move the initialization logic _after_ the import statements.\r\n\r\n- Best case: This will happen while running `npm run serve`, and the source file is updated by `eslint`. The change is obvious.\r\n- Worst case: This re-ordering happens during `npm run build`, and the order is only changed in the `dist` output. Then it's completely mysterious why the init logic is out of order.\r\n\r\n\r\n## Solution\r\n\r\n- Moving the sentry initialization logic to a separate file, and importing it at the **top** of the main application file.\r\n- Unfortunately, because in the case of Vue, `Sentry` relies on the Router, and the main file also relies on the Router, the router definition has to be extracted in another file as well.\r\n\r\nThis works because the import order is maintained (at least in my project), whereas code statements are always moved after imports.\r\n\r\n```ts\r\n// main.ts\r\nimport \"./sentry\"\r\nimport Vue from \"vue\";\r\nimport { router } from \"./router\"\r\nVue.use(router);\r\n// rest of vue initialization\r\n```\r\n\r\n```ts\r\n// router.ts\r\nimport Vue from \"vue\";\r\nexport const router = new Router({\r\n // ...\r\n});\r\n\r\n```\r\n\r\n```ts\r\n// sentry.ts\r\nimport * as Sentry from \"@sentry/vue\";\r\nimport { Integrations } from \"@sentry/tracing\";\r\nimport { router } from \"./router\"\r\n\r\nSentry.init(...)\r\n```\r\n\r\nThe solution is not ideal because it makes integrating Sentry seem more complex than it is, and requires changing the structure of the application files.\r\n\r\nBetter solutions are welcome of course.\r\n\r\n\r\nIn any case, I hope that Sentry users who are trying to figure out why Sentry Headers are not being added to their fetch request, will find this issue and can solve it without opening yet another ticket 😉","author":{"url":"https://github.com/boukeversteegh","@type":"Person","name":"boukeversteegh"},"datePublished":"2022-01-21T12:46:32Z","url":"https://github.com/getsentry/sentry-javascript/issues/4439"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f91c1858-d891-56d2-888c-abc4ce05cca7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C664:192A8C:6CB97A:98B886:6B044D81 |
| html-safe-nonce | 742dca548cab9a59b532d309d55539aa2f5c1980a1c73be0dc875735d92103cc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjY0OjE5MkE4Qzo2Q0I5N0E6OThCODg2OjZCMDQ0RDgxIiwidmlzaXRvcl9pZCI6IjMyMzUwNjY2MTMxNTI0OTkzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1df5919042c6ebafe9a500a87c91a13a5030b9d3694c29a9d58028c41091661e |
| hovercard-subject-tag | issue:1110432040 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/4439/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b7a443203e5f573d1f3ea90bbd9b9ad68ca05680c36ad83eff4aff40586063b5/getsentry/sentry-javascript/issues/4439 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b7a443203e5f573d1f3ea90bbd9b9ad68ca05680c36ad83eff4aff40586063b5/getsentry/sentry-javascript/issues/4439 |
| og:image:alt | Description Sentry documentation suggests to initialize Sentry "as early as possible", and the example does it after all application imports: import Vue from "vue"; import Router from "vue-router";... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | boukeversteegh |
| hostname | github.com |
| expected-hostname | github.com |
| None | 002aa6b95ffa730b0aec436a5c383f536ad677284e60056037fb120afe37b730 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 059f24c781e3a0d7df3f27a97da0e93fab06ae39 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width