Question

SyntaxError: Unexpected token '<' on new build of React

I have a React application and every time I deploy it to digital ocean I’m getting this error SyntaxError: Unexpected token '<', but once you reload the page there’s no error and it loads properly. From my knowledge, it’s because it’s trying to locate the cached versions of the build files, but does anyone know how to fix this issue? Or bust the cache so that whenever I deploy, people don’t get broken pages until the reload?

I run env-cmd -f .env.production react-scripts build to create a new production build before deploying the code.

package.json

{
    "name": "react-app",
    "version": "0.1.0",
    "private": true,
    "proxy": "https://50874c4689b8.ngrok.io",
    "dependencies": {
        "@alexcambose/react-infinite-scroll": "^1.0.1",
        "@emotion/react": "^11.10.4",
        "@emotion/styled": "^11.10.4",
        "@mui/icons-material": "^5.10.3",
        "@mui/lab": "^5.0.0-alpha.98",
        "@mui/material": "^5.10.4",
        "@mui/styles": "^5.10.3",
        "@stripe/react-stripe-js": "^1.6.0",
        "@stripe/stripe-js": "^1.20.3",
        "@testing-library/jest-dom": "^5.11.4",
        "@testing-library/react": "^11.1.0",
        "@testing-library/user-event": "^12.1.10",
        "@tinymce/tinymce-react": "^3.12.6",
        "@types/array-move": "^2.0.0",
        "@types/axios": "^0.14.0",
        "@types/draftjs-to-html": "^0.8.1",
        "@types/html-to-draftjs": "^1.4.0",
        "@types/jest": "^26.0.22",
        "@types/node": "^12.20.7",
        "@types/react": "^17.0.3",
        "@types/react-bootstrap": "^0.32.25",
        "@types/react-dom": "^17.0.3",
        "@types/react-draft-wysiwyg": "^1.13.3",
        "@types/react-icons": "^3.0.0",
        "@types/react-infinite-scroll-component": "^5.0.0",
        "@types/react-infinite-scroller": "^1.2.2",
        "@types/react-redux": "^7.1.16",
        "@types/react-sortable-hoc": "^0.7.1",
        "@types/react-toastify": "^4.1.0",
        "@types/redux-logger": "^3.0.8",
        "@types/redux-persist": "^4.3.1",
        "@types/redux-thunk": "^2.1.0",
        "@types/socket.io-client": "^3.0.0",
        "@types/styled-components": "^5.1.9",
        "array-move": "^3.0.1",
        "axios": "^0.21.1",
        "axios-hooks": "^2.6.1",
        "bootstrap": "^4.6.0",
        "draftjs-to-html": "^0.9.1",
        "env-cmd": "^10.1.0",
        "formik": "^2.2.6",
        "gatsby-plugin-react-helmet": "^3.10.0",
        "gatsby-plugin-react-helmet-async": "^1.2.1",
        "html-to-draftjs": "^1.5.0",
        "laravel-echo": "^1.11.3",
        "laravel-echo-server": "^1.6.3",
        "moment": "^2.29.1",
        "moment-timezone": "^0.5.34",
        "node-sass": "^7.0.1",
        "pusher-js": "^7.0.6",
        "react": "^17.0.2",
        "react-adsense": "^0.1.0",
        "react-apple-signin-auth": "^1.0.0",
        "react-bootstrap": "^1.5.2",
        "react-device-detect": "^2.1.2",
        "react-dom": "^17.0.2",
        "react-draft-wysiwyg": "^1.14.7",
        "react-facebook-login": "^4.1.1",
        "react-ga": "^3.3.0",
        "react-google-button": "^0.7.2",
        "react-google-login": "^5.2.2",
        "react-helmet": "^6.1.0",
        "react-helmet-async": "^1.3.0",
        "react-icons": "^4.2.0",
        "react-infinite-scroll-component": "^6.1.0",
        "react-moment": "^1.1.1",
        "react-redux": "^7.2.3",
        "react-scripts": "^5.0.1",
        "react-select": "^5.4.0",
        "react-sliding-side-panel": "^2.0.3",
        "react-smartbanner": "^5.1.4",
        "react-sortable-hoc": "^2.0.0",
        "react-toastify": "^7.0.4",
        "recharts": "^2.1.10",
        "redux-logger": "^3.0.6",
        "redux-persist": "^6.0.0",
        "redux-thunk": "^2.3.0",
        "socket.io-client": "^4.1.3",
        "styled-components": "^5.2.2",
        "typescript": "^4.2.3",
        "web-vitals": "^1.0.1",
        "webpack": "^5.74.0",
        "yup": "^0.32.9"
    },
    "resolutions": {
        "@types/react": "17.0.3",
        "@types/react-dom": "17.0.3"
    },
    "scripts": {
        "start": "env-cmd -f .env.development react-scripts start",
        "start:prod": "env-cmd -f .env.production react-scripts start",
        "build": "react-scripts build",
        "build:prod": "env-cmd -f .env.production react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
    },
    "eslintConfig": {
        "extends": [
            "react-app",
            "react-app/jest"
        ]
    },
    "browserslist": {
        "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
        ],
        "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
        ]
    },
    "devDependencies": {
        "@types/react-facebook-login": "^4.1.3",
        "@types/react-router-dom": "^5.1.7",
        "react-router-dom": "^5.2.0"
    }
}

src/reportWebVitals

import { ReportHandler } from "web-vitals";

const reportWebVitals = (onPerfEntry?: ReportHandler) => {
    if (onPerfEntry && onPerfEntry instanceof Function) {
        import("web-vitals").then(
            ({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
                getCLS(onPerfEntry);
                getFID(onPerfEntry);
                getFCP(onPerfEntry);
                getLCP(onPerfEntry);
                getTTFB(onPerfEntry);
            }
        );
    }
};

export default reportWebVitals;

src/index.tsx

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { Provider } from "react-redux";
import { persistor, store } from "./store";
import { PersistGate } from "redux-persist/integration/react";
import SmartBanner from "react-smartbanner";
import { isMobile } from "react-device-detect";
import "react-smartbanner/dist/main.css";

//imports for toastify css
import "react-toastify/dist/ReactToastify.css";

//import react sliding panel css
import "react-sliding-side-panel/lib/index.css";

//import my css last to overwrite anything above it
import "./index.css";

//imports for yup
import "core-js/es/promise";
import "core-js/es/set";
import "core-js/es/map";
import "core-js/es/array";

const userAgent = navigator.userAgent || navigator.vendor;
let device = undefined;
if (/android/i.test(userAgent)) {
    device = "android";
} else if (/iPad|iPhone|iPod/.test(userAgent)) {
    device = "ios";
}

ReactDOM.render(
    <Provider store={store}>
        <SmartBanner
            force={isMobile ? device : undefined}
            title="React-App"
        />
        <PersistGate loading={null} persistor={persistor}>
            <App />
        </PersistGate>
    </Provider>,
    document.getElementById("root")
);

reportWebVitals();

App.tsx

import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { ThemeProvider } from "styled-components";
import { Elements } from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";
import ReactGA from "react-ga";
import { HelmetProvider, Helmet } from "react-helmet-async";

import PublicRouter from "./router/Public";
import { RootState } from "./store";
import { setTheme } from "./store/theme/actions";
import { ThemeState } from "./store/theme/types";

import darkTheme from "./themes/dark";
import lightTheme from "./themes/light";

import "bootstrap/dist/css/bootstrap.min.css";
import "./styles/app.scss";

declare module "styled-components" {
    interface DefaultTheme extends ThemeState {}
}

ReactGA.initialize("UA-xxxxxxx-x");
ReactGA.pageview(window.location.pathname + window.location.search);
const stripePromise = loadStripe("pk_live_xxxxxxxxxxxxxxxxx");

function App() {
    const dispatch = useDispatch();
    const user = useSelector((state: RootState) => state.user);
    const theme = useSelector((state: RootState) => state.theme);
    const subscription = useSelector((state: RootState) => state.subscription);

    useEffect(() => {
        dispatch(setTheme(user.dark_mode ? darkTheme : lightTheme));
        document.body.style.backgroundColor = theme.primary;
    }, [user.dark_mode]);

    useEffect(() => {
        if (subscription) {
            // Get the element you want to remove
            var element = document.querySelectorAll(".google-ads-script");
            // Get the parent and remove the element since it is a child of the parent
            if (element) {
                element.forEach((ele) => {
                    ele.remove();
                });
            }
        }
    }, [subscription]);

    return (
        <HelmetProvider>
            <ThemeProvider theme={theme}>
                <Elements stripe={stripePromise}>
                    {!subscription && (
                        <Helmet>
                            <script
                                id="google-ads-script"
                                async
                       src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxxxxxxx"
                                crossorigin="anonymous"
                            ></script>
                        </Helmet>
                    )}
                    <PublicRouter />
                </Elements>
            </ThemeProvider>
        </HelmetProvider>
    );
}

export default App;

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer