index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {Config} from 'sentry/types';
  2. import {extractSlug} from 'sentry/utils/extractSlug';
  3. const BOOTSTRAP_URL = '/api/client-config/';
  4. const bootApplication = (data: Config) => {
  5. window.csrfCookieName = data.csrfCookieName;
  6. window.superUserCookieName = data.superUserCookieName;
  7. window.superUserCookieDomain = data.superUserCookieDomain ?? undefined;
  8. return data;
  9. };
  10. /**
  11. * Load the client configuration data using the BOOTSTRAP_URL. Used when
  12. * running in standalone SPA mode.
  13. */
  14. async function bootWithHydration() {
  15. const response = await fetch(BOOTSTRAP_URL);
  16. const data: Config = await response.json();
  17. // Shim up the initialData payload to quack like it came from
  18. // a customer-domains initial request. Because our initial call to BOOTSTRAP_URL
  19. // will not be on a customer domain, the response will not include this context.
  20. if (data.customerDomain === null && window.__SENTRY_DEV_UI) {
  21. const domain = extractSlug(window.location.host);
  22. if (domain) {
  23. data.customerDomain = {
  24. organizationUrl: `https://${domain.slug}.sentry.io`,
  25. sentryUrl: 'https://sentry.io',
  26. subdomain: domain.slug,
  27. };
  28. }
  29. }
  30. window.__initialData = data;
  31. bootApplication(data);
  32. preloadOrganizationData(data);
  33. return data;
  34. }
  35. function promiseRequest(url: string): Promise<any> {
  36. return new Promise(function (resolve, reject) {
  37. const xhr = new XMLHttpRequest();
  38. xhr.open('GET', url);
  39. xhr.withCredentials = true;
  40. xhr.onload = function () {
  41. try {
  42. this.status >= 200 && this.status < 300
  43. ? resolve([JSON.parse(xhr.response), this.statusText, xhr])
  44. : reject([this.status, this.statusText]);
  45. } catch (e) {
  46. reject();
  47. }
  48. };
  49. xhr.onerror = function () {
  50. reject([this.status, this.statusText]);
  51. };
  52. xhr.send();
  53. });
  54. }
  55. function preloadOrganizationData(config: Config) {
  56. if (!config.user) {
  57. // Don't send requests if there is no logged in user.
  58. return;
  59. }
  60. let slug = config.lastOrganization;
  61. if (!slug && config.customerDomain) {
  62. slug = config.customerDomain.subdomain;
  63. }
  64. let host = '';
  65. if (config.links?.regionUrl && config.links?.regionUrl !== config.links?.sentryUrl) {
  66. host = config.links.regionUrl;
  67. }
  68. // When running in 'dev-ui' mode we need to use /region/$region instead of
  69. // subdomains so that webpack/vercel can proxy requests.
  70. if (host && window.__SENTRY_DEV_UI) {
  71. const domainpattern = /https?\:\/\/([^.]*)\.sentry.io/;
  72. const domainmatch = host.match(domainpattern);
  73. if (domainmatch) {
  74. host = `/region/${domainmatch[1]}`;
  75. }
  76. }
  77. function makeUrl(suffix: string) {
  78. return host + '/api/0/organizations/' + slug + suffix;
  79. }
  80. const preloadPromises: Record<string, any> = {orgSlug: slug};
  81. window.__sentry_preload = preloadPromises;
  82. try {
  83. preloadPromises.organization = promiseRequest(makeUrl('/?detailed=0'));
  84. preloadPromises.projects = promiseRequest(
  85. makeUrl('/projects/?all_projects=1&collapse=latestDeploys')
  86. );
  87. preloadPromises.teams = promiseRequest(makeUrl('/teams/'));
  88. } catch (e) {
  89. // eslint-disable-next-line
  90. console.error(e);
  91. }
  92. }
  93. /**
  94. * Load client configuration bootstrap data. This will detect if the app is
  95. * running in SPA mode or being booted from the django-rendered layout.html
  96. * template.
  97. */
  98. export async function bootstrap() {
  99. const bootstrapData = window.__initialData;
  100. // If __initialData is not already set on the window, we are likely running in
  101. // pure SPA mode, meaning django is not serving our frontend application and we
  102. // need to make an API request to hydrate the bootstrap data to boot the app.
  103. if (bootstrapData === undefined) {
  104. return await bootWithHydration();
  105. }
  106. return bootApplication(bootstrapData);
  107. }