index.tsx 1.2 KB

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