Search code examples
javascriptreactjsdocusaurus

rendering logic doesn't work in production static site


The following code renders a warning message conditionally, when user switch site (by clicking the button, or accessing directly using URL), the website will show a warning message, and disappear on refresh.

Things work fine within the docusaurus local development server, but behaves differently once built into a production static site.

import Cookies from 'js-cookie';
import React from 'react';

import { DocProvider } from '@docusaurus/theme-common/internal';
import { HtmlClassNameProvider } from '@docusaurus/theme-common';
import { translate } from '@docusaurus/Translate';
import CommunityLinkGroup from "@site/src/components/LinkGroup";
import DocItemLayout from '@theme/DocItem/Layout';
import DocItemMetadata from '@theme/DocItem/Metadata';
import styles from "./index.module.css";

export default function DocItem(props) {
  const docHtmlClassName = `docs-doc-id-${props.content.metadata.unversionedId}`;
  const MDXComponent = props.content;

  const url = props.location.pathname;
  const prevEdition = Cookies.get('doc_edition');
  var displayAlert = false;
  if (url.includes('/community/')) {
    Cookies.set('doc_edition', 'community');
    if (prevEdition == 'cloud') {
      displayAlert = true;
    }
  } else if (url.includes('/cloud/')) {
    Cookies.set('doc_edition', 'cloud');
    if (prevEdition == 'community') {
      displayAlert = true;
    }
  }
  var alertMsg = "bad msg";
  var alertClass = "badcls";
  var alertRole = "badrole";
  if (displayAlert == true) {
    alertMsg = translate({
      id: 'theme.DocItem.switchDocAlertMsg',
      message: 'you just switched site, please notice.',
    });
    alertClass = "alert alert--warning";
    alertRole = "alert";
  }
  console.log(alertMsg, alertClass, alertRole);
  return (
    <DocProvider content={props.content}>
      <HtmlClassNameProvider className={docHtmlClassName}>
        <div className={alertClass} role={alertRole}>{alertMsg}</div>
        <DocItemMetadata />
        <DocItemLayout>
          <MDXComponent />
        </DocItemLayout>
        <div className={styles.communityLinkContainer}>
          <CommunityLinkGroup />
        </div>
      </HtmlClassNameProvider>
    </DocProvider>

  );
}

Works fine inside the local dev server:

enter image description here

But once built into a production static site, it yields impossible rendering result:

enter image description here

Meanwhile, production site console output shows you just switched site, please notice. alert alert--warning alert, which clearly indicates alertMsg=='you just switched site, please notice.', alertClass=="alert alert--warning", alertRole=="alert", meaning that displayAlert must be true.

But as shown in above screenshot, it looks like as if displayAlert is both false and true at the same time, a totally impossible DOM state.

Also, this only happens when accessed directly using URL path, if I click on the button provided by the website to change site, the website warning message will display normally through dynamic rendering.


Solution

  • I should be using state variables, like https://stackblitz.com/edit/react-pcagrw?file=src/App.js, consider this a React 101.