index.tsx 1.1 KB

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