index.tsx 839 B

12345678910111213141516171819202122232425262728293031
  1. import {Config} from 'app/types';
  2. const BOOTSTRAP_URL = '/api/client-config/';
  3. const bootApplication = (data: Config) => {
  4. window.csrfCookieName = data.csrfCookieName;
  5. return data;
  6. };
  7. async function bootWithHydration() {
  8. const response = await fetch(BOOTSTRAP_URL);
  9. const data: Config = await response.json();
  10. window.__initialData = data;
  11. return bootApplication(data);
  12. }
  13. export async function bootstrap() {
  14. const bootstrapData = window.__initialData;
  15. // If __initialData is not already set on the window, we are likely running in
  16. // pure SPA mode, meaning django is not serving our frontend application and we
  17. // need to make an API request to hydrate the bootstrap data to boot the app.
  18. if (bootstrapData === undefined) {
  19. return await bootWithHydration();
  20. }
  21. return bootApplication(bootstrapData);
  22. }