Multi-language implementations of URL pattern matching utilities for building bespoke server setups that need to preload JS/CSS resources or handle early 404 responses.
This utility is designed for server languages that cannot do SSR/prerendering but still want to provide better experiences. It enables servers to:
- Add preload head tags for JS,CSS before serving HTML
- Return early 404 pages for unmatched routes
- Generate dynamic titles based on route parameters
Typical implementation flow:
-
Build-time Setup:
- Write your routes as an array in a JS file
- Create a build script that exports route patterns and entry files to a
.jsonfile - Configure your frontend build tool to output a
manifestfile mapping entry files to final fingerprinted/hashed output JS/CSS files and dependencies
-
Server-time Processing:
- Load the JSON route file when a request comes in
- Match the requested URL against each route pattern until you find a match
- Once matched, you have the source entry
.jsxfile - Load the build manifest file to find which JS chunk contains that code and its dependency files
- Generate
<link rel="preload">tags for each dependency (JS, CSS, images, icons) - Inject those head tags into the HTML before serving
-
Result:
- Browsers start downloading critical resources immediately
- Faster page loads without full SSR complexity
- Early 404s for invalid routes
Here's how you might integrate this into a server setup:
[
{
"path": "/users/:userId/posts",
"component": "pages/UserPosts.jsx",
"title": "Posts by :userId"
},
{
"path": "/products/:category/:id",
"component": "pages/Product.jsx",
"title": "Product :id"
}
]{
"pages/UserPosts.jsx": {
"file": "assets/UserPosts-abc123.js",
"css": ["assets/UserPosts-def456.css"],
"imports": ["chunks/shared-ghi789.js"]
}
}# Python example
import json
routes = json.load(open('routes.json'))
manifest = json.load(open('manifest.json'))
def handle_request(url_path):
for route in routes:
matches = preact_iso_url_pattern_match(url_path, route['path'])
if matches:
# Generate preload tags
component = route['component']
entry_info = manifest[component]
preload_tags = []
for js_file in [entry_info['file']] + entry_info.get('imports', []):
preload_tags.append(f'<link rel="modulepreload" crossorigin href="{js_file}">')
for css_file in entry_info.get('css', []):
preload_tags.append(f'<link rel="stylesheet" crossorigin href="{css_file}">')
# Generate dynamic title
title = route['title']
for param, value in matches['params'].items():
title = title.replace(f':{param}', value)
return {
'preload_tags': preload_tags,
'title': title,
'params': matches['params']
}
# No match found - return early 404
return NoneThis approach gives you the performance benefits of resource preloading without the complexity of full server-side rendering!
Go, PHP, Python and Ruby.
Find the corresponding language's sub-directory. Each language has a README that contains usage examples and API reference.
# Run all tests across all languages
./run_tests.sh
# Or run individual language tests
cd go && go test -v
cd python && python3 test_preact_iso_url_pattern.py
cd ruby && ruby test_preact_iso_url_pattern.rb
cd php && php test_preact_iso_url_pattern.php