index.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {Config} from 'sentry/types';
  2. const BOOTSTRAP_URL = '/api/client-config/';
  3. const bootApplication = (data: Config) => {
  4. window.csrfCookieName = data.csrfCookieName;
  5. window.superUserCookieName = data.superUserCookieName;
  6. window.superUserCookieDomain = data.superUserCookieDomain ?? undefined;
  7. return data;
  8. };
  9. /**
  10. * Load the client configuration data using the BOOTSTRAP_URL. Used when
  11. * running in standalone SPA mode.
  12. */
  13. async function bootWithHydration() {
  14. const response = await fetch(BOOTSTRAP_URL);
  15. const data: Config = await response.json();
  16. window.__initialData = data;
  17. return bootApplication(data);
  18. }
  19. /**
  20. * Load client configuration bootstrap data. This will detect if the app is
  21. * running in SPA mode or being booted from the django-rendered layout.html
  22. * template.
  23. */
  24. export async function bootstrap() {
  25. const bootstrapData = window.__initialData;
  26. // If __initialData is not already set on the window, we are likely running in
  27. // pure SPA mode, meaning django is not serving our frontend application and we
  28. // need to make an API request to hydrate the bootstrap data to boot the app.
  29. if (bootstrapData === undefined) {
  30. return await bootWithHydration();
  31. }
  32. return bootApplication(bootstrapData);
  33. }