index.tsx 902 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. // Once data hydration is done we can initialize the app
  6. const {initializeMain} = require('./bootstrap/initializeMain');
  7. initializeMain(data);
  8. };
  9. async function bootWithHydration() {
  10. const response = await fetch(BOOTSTRAP_URL);
  11. const data: Config = await response.json();
  12. window.__initialData = data;
  13. bootApplication(data);
  14. }
  15. const bootstrapData = window.__initialData;
  16. // If __initialData is not already set on the window, we are likely running in
  17. // pure SPA mode, meaning django is not serving our frontend application and we
  18. // need to make an API request to hydrate the bootstrap data to boot the app.
  19. if (bootstrapData === undefined) {
  20. bootWithHydration();
  21. } else {
  22. bootApplication(bootstrapData);
  23. }