1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-21 19:21:18 -04:00

catch ThemeScript minify errors

This commit is contained in:
2022-04-30 09:56:28 -04:00
parent eaeceaa58f
commit a2921d106e
2 changed files with 14 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
import { useMemo } from "react"; import { useMemo } from "react";
import { minify } from "uglify-js"; import { minify } from "uglify-js";
import { clientScript } from "./script"; import { clientScript } from "./client";
import { darkModeQuery, themeStorageKey, themeClassNames } from "../../lib/config/themes"; import { darkModeQuery, themeStorageKey, themeClassNames } from "../../lib/config/themes";
const ThemeScript = () => { const ThemeScript = () => {
@@ -12,8 +12,8 @@ const ThemeScript = () => {
.replace('"__CLASS_NAMES__"', JSON.stringify(themeClassNames)); .replace('"__CLASS_NAMES__"', JSON.stringify(themeClassNames));
// minify the final code, a bit hacky but this is ONLY done at build-time, so uglify-js is never bundled or sent to // minify the final code, a bit hacky but this is ONLY done at build-time, so uglify-js is never bundled or sent to
// the browser to execute: // the browser to execute.
return minify(`(${functionString})()`, { const result = minify(`(${functionString})()`, {
toplevel: true, toplevel: true,
compress: { compress: {
negate_iife: false, negate_iife: false,
@@ -21,15 +21,25 @@ const ThemeScript = () => {
parse: { parse: {
bare_returns: true, bare_returns: true,
}, },
}).code; });
// fail somewhat silenty
if (result.error) {
console.error(result.error);
return;
}
return result.code;
}, []); }, []);
// the script tag injected manually into `<head>` in _document.tsx. // the script tag injected manually into `<head>` in _document.tsx.
// even though it's the proper method, using next/script with `strategy="beforeInteractive"` still causes flash of // even though it's the proper method, using next/script with `strategy="beforeInteractive"` still causes flash of
// white on load. injecting a normal script tag lets us prioritize setting the `<html>` class even more urgently. // white on load. injecting a normal script tag lets us prioritize setting the `<html>` class even more urgently.
// TODO: using next/script *might* be possible after https://github.com/vercel/next.js/pull/36364 is merged.
return ( return (
<script <script
key="restore-theme" key="restore-theme"
id="restore-theme"
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
// make it an IIFE: // make it an IIFE:
__html: `(function(){${minified}})()`, __html: `(function(){${minified}})()`,