index.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. return bootApplication(data);
  32. }
  33. /**
  34. * Load client configuration bootstrap data. This will detect if the app is
  35. * running in SPA mode or being booted from the django-rendered layout.html
  36. * template.
  37. */
  38. export async function bootstrap() {
  39. const bootstrapData = window.__initialData;
  40. // If __initialData is not already set on the window, we are likely running in
  41. // pure SPA mode, meaning django is not serving our frontend application and we
  42. // need to make an API request to hydrate the bootstrap data to boot the app.
  43. if (bootstrapData === undefined) {
  44. return await bootWithHydration();
  45. }
  46. return bootApplication(bootstrapData);
  47. }