Optimization & Cleanup Guide
This document outlines the strategies and processes used to optimize the theme for production, addressing key performance requirements such as critical CSS, asset preloading, and render-blocking resources.
1. Production Build & Tailwind Optimization
Goal: Optimize the loading of the Tailwind CSS file for production.
We use npm scripts to handle the build pipeline, ensuring assets are minified and ready for deployment.
Steps to Build
- Open your terminal and navigate to the theme directory (
wp-content/themes/wp-scratch). - Ensure dependencies are installed:
npm install. - Run the build command:
npm run build
What it does:
- Tailwind Compilation: Compiles
src/input.csstostyle.css. - Minification: Uses the
--minifyflag to compress the CSS, removing whitespace and comments. - Purging: Tailwind's JIT engine automatically removes unused utility classes based on the
contentconfiguration intailwind.config.js. - Critical CSS Generation: Triggers the
scripts/generate-critical.mjsscript.
Build Configuration Files
There isn't a single "build config" file; rather, the process is controlled by a few key files:
- package.json: Defines the build script command (tailwindcss ... && node ...).
- tailwind.config.js: Configures the CSS compilation, including the content paths for purging unused styles.
- scripts/generate-critical.mjs: Configures the Critical CSS generation (viewport size, source URL, etc.).
2. Critical CSS Strategy
To improve "First Contentful Paint" (FCP) and Core Web Vitals, we generate and inline the CSS required for above-the-fold content.
Generation
Installation:
npm install --save-dev critical
The script scripts/generate-critical.mjs uses the critical NPM package.
- Source: Fetches the local homepage URL defined in the script.
- Output: Writes to
critical.cssin the theme root. - Dimensions: Targets a standard desktop viewport (1300x900) to determine what is "critical".
Implementation (functions.php)
- Inlining: The
wp_scratch_inline_critical_cssfunction readscritical.cssand prints it within<style>tags in the<head>. - Deferral: The
wp_scratch_defer_stylesfilter modifies the mainstyle.csslink tag. It setsmedia="print"initially and switches tomedia="all"via JavaScript (onload), preventing the large stylesheet from blocking rendering.
3. Asset Optimization
Font Preloading
To reduce "Flash of Unstyled Text" (FOUT) and improve Cumulative Layout Shift (CLS), we preload key font files.
- Function:
wp_scratch_preload_fontsinfunctions.php. - Method: Adds
<link rel="preload" ...>tags to the<head>for primary font files (e.g., IBM Plex Sans, Anybody).
Cache Busting
We ensure users always receive the latest assets after a deployment without needing a hard refresh.
- Method: In
wp_enqueue_scripts, we usefilemtime()to get the file's modification timestamp and use it as the version number forstyle.cssand custom scripts.
4. Code Cleanup Checklist
Before deploying to production, ensure the following steps are taken:
- Remove Debugging: Delete
console.logstatements and temporary debug scripts (e.g., block registry loggers inadmin_footer). - Disable Dev Helpers: Remove aggressive cache clearing actions (like
wp_clean_themes_cacheon init) used during development to prevent performance hits. - Verify Block Metadata: Ensure
block.jsonfiles have correct titles, descriptions, and categories. - Check
theme.json: Ensure no duplicate font definitions or unused palette colors remain.
5. Future Considerations
- Image Optimization: Consider adding automated image compression (WebP generation) if not handled by a plugin.
- Script Concatenation: If the number of custom block scripts grows significantly, consider a build step to bundle them, though HTTP/2 makes this less critical.