Sahajanand Static Search Index
Self-contained WordPress plugin that builds a local search-index.json and powers instant fuzzy search with local Fuse.js — designed for sites exported with the free Simply Static plugin (no MySQL, PHP, or CDN at runtime).
Folder structure
static-search-index/
├── sahajanand-static-search-index.php
├── README.md
├── package.json
├── phpunit.xml.dist
├── includes/
│ └── class-updater.php
├── assets/
│ ├── js/
│ │ ├── fuse.min.js ← must be present (see below)
│ │ └── static-search.js
│ └── css/
│ └── static-search.css
└── tests/
├── bootstrap.php
├── run-tests.php
├── test-static-search.php
├── test-updater.php
└── static-search.test.js
1. Install Fuse.js locally (required)
Fuse.js must live in the plugin — do not load it from a CDN in production.
Option A — curl (recommended)
From the plugin root:
curl -fsSL "https://cdn.jsdelivr.net/npm/[email protected]/dist/fuse.min.js" \
-o assets/js/fuse.min.js
Option B — npm
npm pack [email protected]
tar -xzf fuse.js-6.6.2.tgz
cp package/dist/fuse.min.js assets/js/fuse.min.js
rm -rf package fuse.js-6.6.2.tgz
Option C — manual download
- Open https://cdn.jsdelivr.net/npm/[email protected]/dist/fuse.min.js
- Save the file as
assets/js/fuse.min.js
Confirm the file header mentions Fuse.js v6.6.x (or newer 6.x).
This repository already includes
fuse.min.jsv6.6.2. Re-run the curl command only if you need to refresh it.
2. Install the plugin in WordPress
- Copy the
static-search-indexfolder intowp-content/plugins/. - Activate Sahajanand Static Search Index in Plugins.
- On activation, the plugin writes
wp-content/uploads/search-index.json. - The index regenerates automatically when published posts, pages, or public custom post types are saved, trashed, or deleted.
3. Verify live search (before export)
- Visit the front end and use the theme’s search form (
form[role="search"],.search-form, or the Search block). - Type at least 2 characters — results should appear in
#static-search-results. - Confirm these assets load from your domain (Network tab):
/wp-content/plugins/static-search-index/assets/js/fuse.min.js/wp-content/plugins/static-search-index/assets/js/static-search.js/wp-content/plugins/static-search-index/assets/css/static-search.css/wp-content/uploads/search-index.json
4. Export with Simply Static
- Install and activate Simply Static (free).
- Run a static export as usual.
- The plugin registers
search-index.jsonvia Simply Static filters (simply_static_additional_urls/ss_additional_urlsand file variants) so the JSON is included even if the crawler misses it. - Plugin JS/CSS are linked from pages, so Simply Static should crawl and copy them automatically.
After export, open the static HTML and confirm search still works offline / without PHP.
Index entry shape
{
"id": 12,
"title": "About Us",
"permalink": "/about-us/",
"excerpt": "Short preview…",
"content": "Full plain-text content…",
"categories": ["News"],
"type": "page"
}
Fuse.js weights: title 0.5, excerpt 0.3, content 0.2.
🌐 Subdirectory & Destination URL Handling
When your WordPress staging environment is installed in a subdirectory (for example https://projects.sahajananddigital.in/shajananddigital) and you export to a static root domain (such as https://shajananddigital.in):
- Automatic Subdirectory Stripping: The plugin automatically detects and strips the WordPress staging subfolder from all indexed permalinks, producing clean root-relative paths like
/my-article/instead of/shajananddigital/my-article/. - Simply Static Destination URL Detection: If Simply Static has a custom Destination URL configured (e.g. GitHub Pages
https://username.github.io/my-repo/), the destination subpath is automatically applied (/my-repo/my-article/). - Dual Environment Resolver: In the browser,
static-search.jsautomatically resolves links relative to the active environment — preserving subfolder paths when tested locally on the dynamic staging site, and ensuring clean root paths on the static export. - Developer Filters:
sahajanand_static_search_destination_url: Override or customize the static destination URL.sahajanand_static_search_relative_path: Customize the computed relative path for any post permalink.sahajanand_static_search_entry: Customize or append data to the JSON index entry before saving.
// Example: Customize static destination URL
add_filter( 'sahajanand_static_search_destination_url', function( $dest_url ) {
return 'https://shajananddigital.in';
} );
🚀 Cache Busting & Static Site Versioning (?version=1, 2, ...)
To prevent browsers, proxies, and CDNs (such as Cloudflare, GitHub Pages, or Netlify) from serving stale cached copies of your search index and assets when updating a static site:
- Automatic Increment: Whenever a static site export begins in Simply Static (
ss_before_static_export) or the index is regenerated (save_post, etc.), the plugin automatically increments an internal version number (1,2,3...). - Dynamic URL:
search-index.jsonis requested with?version=X(e.g.search-index.json?version=1).- Plugin CSS and JS are enqueued with the current static version.
- The JSON index itself stores the current
versionfor easy verification.
- Whole-Site Asset Cache Busting (Optional):
You can also apply the unique static version query string to all enqueued theme and plugin styles and scripts across the static site by enabling the filter:
add_filter( 'sahajanand_static_search_cache_bust_all_assets', '__return_true' );
- Custom Version Filter:
Override or provide custom versioning logic (e.g. git commit hash or timestamp):
add_filter( 'sahajanand_static_search_version', function( $version ) {
return 'v' . time();
} );
🔄 Self-Hosted & Git Updates
The plugin supports two methods to manage updates directly via Git:
1. Native Self-Hosted Updates (No External Plugins Required)
Implements native WordPress core update hooks (Misha Rudrastyh approach) via site_transient_update_plugins and plugins_api:
- Automatically generates and publishes
info.jsoninto thedocs/static-search-index/folder for deployment via GitHub Pages. - Checks the GitHub Pages endpoint (
https://sahajananddigital.github.io/wordpress-plugins/static-search-index/info.json) for newer releases and changelog details. - Displays the native update notification in the WordPress Plugins screen and updates via the standard one-click WordPress updater.
- Update cache is refreshed every 12 hours and purged automatically upon update.
To customize the update manifest endpoint URL, use the filter:
add_filter( 'sahajanand_static_search_update_url', function() {
return 'https://example.com/path/to/info.json';
} );
2. Git Updater Plugin Compatibility
Contains standard Git Updater headers in the main plugin file header (GitHub Plugin URI, Primary Branch, Release Asset), allowing it to be managed, installed, and updated directly via the Git Updater WordPress plugin.
🧪 Testing
The plugin includes unit test suites for both PHP core functionality and client-side JavaScript search resolution.
Run All Tests
npm test
Run PHP Tests Standalone (Zero Dependencies)
php tests/run-tests.php
Or via npm:
npm run test:php
Run Frontend JavaScript Tests
node tests/static-search.test.js
Or via npm:
npm run test:js
PHPUnit (CI / Local)
phpunit
Requirements
- WordPress 5.8+
- PHP 7.4+
- Writable
wp-content/uploads/ - Fuse.js ≥ 6.6 in
assets/js/fuse.min.js