zero-query 1.0.1 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -9
- package/cli/help.js +2 -1
- package/cli/scaffold/default/app/app.js +3 -6
- package/cli/scaffold/default/global.css +12 -3
- package/cli/scaffold/default/index.html +8 -8
- package/cli/scaffold/minimal/app/app.js +3 -9
- package/cli/scaffold/minimal/global.css +12 -3
- package/cli/scaffold/minimal/index.html +3 -3
- package/cli/scaffold/ssr/app/app.js +18 -6
- package/cli/scaffold/ssr/app/components/about.js +42 -15
- package/cli/scaffold/ssr/app/components/blog/index.js +65 -0
- package/cli/scaffold/ssr/app/components/blog/post.js +86 -0
- package/cli/scaffold/ssr/app/routes.js +4 -2
- package/cli/scaffold/ssr/global.css +117 -1
- package/cli/scaffold/ssr/index.html +8 -2
- package/cli/scaffold/ssr/server/data/posts.js +144 -0
- package/cli/scaffold/ssr/server/index.js +117 -23
- package/dist/zquery.dist.zip +0 -0
- package/dist/zquery.js +85 -20
- package/dist/zquery.min.js +2 -2
- package/index.d.ts +5 -2
- package/index.js +5 -4
- package/package.json +1 -1
- package/src/component.js +17 -2
- package/src/router.js +61 -12
- package/src/ssr.js +100 -0
- package/tests/component.test.js +480 -0
- package/tests/router.test.js +65 -1
- package/tests/ssr.test.js +175 -0
- package/tests/test-minifier.js +4 -4
- package/types/misc.d.ts +23 -1
- package/types/router.d.ts +25 -0
- package/types/ssr.d.ts +33 -0
|
@@ -49,6 +49,8 @@ body {
|
|
|
49
49
|
padding: 0.4rem 0.8rem;
|
|
50
50
|
border-radius: var(--radius);
|
|
51
51
|
transition: color 0.2s, background 0.2s;
|
|
52
|
+
cursor: pointer;
|
|
53
|
+
user-select: none;
|
|
52
54
|
}
|
|
53
55
|
.nav-link:hover, .nav-link.active {
|
|
54
56
|
color: var(--accent);
|
|
@@ -58,7 +60,7 @@ body {
|
|
|
58
60
|
/* -- Main content -- */
|
|
59
61
|
z-outlet {
|
|
60
62
|
display: block;
|
|
61
|
-
max-width:
|
|
63
|
+
max-width: 1200px;
|
|
62
64
|
margin: 2rem auto;
|
|
63
65
|
padding: 0 1.5rem;
|
|
64
66
|
}
|
|
@@ -91,6 +93,15 @@ z-outlet {
|
|
|
91
93
|
border-radius: 4px;
|
|
92
94
|
font-size: 0.9em;
|
|
93
95
|
}
|
|
96
|
+
.card a {
|
|
97
|
+
color: var(--accent);
|
|
98
|
+
text-decoration: none;
|
|
99
|
+
transition: color 0.15s;
|
|
100
|
+
}
|
|
101
|
+
.card a:hover {
|
|
102
|
+
color: var(--accent-hover);
|
|
103
|
+
text-decoration: underline;
|
|
104
|
+
}
|
|
94
105
|
|
|
95
106
|
.badge {
|
|
96
107
|
display: inline-block;
|
|
@@ -102,6 +113,111 @@ z-outlet {
|
|
|
102
113
|
.badge-ssr { background: rgba(0,212,255,0.15); color: var(--accent); }
|
|
103
114
|
.badge-csr { background: rgba(255,165,0,0.15); color: #ffa500; }
|
|
104
115
|
|
|
116
|
+
/* -- Blog Grid -- */
|
|
117
|
+
.blog-grid {
|
|
118
|
+
display: grid;
|
|
119
|
+
grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
|
|
120
|
+
gap: var(--gap);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
.blog-card {
|
|
124
|
+
display: block;
|
|
125
|
+
text-decoration: none;
|
|
126
|
+
color: inherit;
|
|
127
|
+
background: var(--surface);
|
|
128
|
+
border: 1px solid rgba(255,255,255,0.06);
|
|
129
|
+
border-radius: var(--radius);
|
|
130
|
+
padding: 1.5rem;
|
|
131
|
+
transition: border-color 0.2s, transform 0.2s;
|
|
132
|
+
cursor: pointer;
|
|
133
|
+
}
|
|
134
|
+
.blog-card:hover {
|
|
135
|
+
border-color: rgba(0, 212, 255, 0.25);
|
|
136
|
+
transform: translateY(-2px);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.blog-card-header {
|
|
140
|
+
display: flex;
|
|
141
|
+
align-items: center;
|
|
142
|
+
justify-content: space-between;
|
|
143
|
+
margin-bottom: 0.75rem;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
.blog-date {
|
|
147
|
+
font-size: 0.8em;
|
|
148
|
+
color: var(--muted);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
.blog-title {
|
|
152
|
+
font-size: 1.1rem;
|
|
153
|
+
margin-bottom: 0.5rem;
|
|
154
|
+
color: var(--text);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.blog-summary {
|
|
158
|
+
color: var(--muted);
|
|
159
|
+
font-size: 0.9rem;
|
|
160
|
+
line-height: 1.6;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/* -- Blog Post Detail -- */
|
|
164
|
+
.back-link {
|
|
165
|
+
display: inline-block;
|
|
166
|
+
color: var(--accent);
|
|
167
|
+
font-size: 0.9rem;
|
|
168
|
+
margin-bottom: 1rem;
|
|
169
|
+
transition: opacity 0.15s;
|
|
170
|
+
text-decoration: none;
|
|
171
|
+
cursor: pointer;
|
|
172
|
+
}
|
|
173
|
+
.back-link:hover { opacity: 0.75; }
|
|
174
|
+
|
|
175
|
+
.blog-post-meta {
|
|
176
|
+
display: flex;
|
|
177
|
+
align-items: center;
|
|
178
|
+
gap: 0.75rem;
|
|
179
|
+
margin-top: 0.5rem;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
.blog-post-body {
|
|
183
|
+
background: var(--surface);
|
|
184
|
+
border: 1px solid rgba(255,255,255,0.06);
|
|
185
|
+
border-radius: var(--radius);
|
|
186
|
+
padding: 2rem;
|
|
187
|
+
line-height: 1.8;
|
|
188
|
+
font-size: 0.95rem;
|
|
189
|
+
}
|
|
190
|
+
.blog-post-body h4 {
|
|
191
|
+
color: var(--accent);
|
|
192
|
+
margin: 1.5rem 0 0.75rem;
|
|
193
|
+
font-size: 1.05rem;
|
|
194
|
+
}
|
|
195
|
+
.blog-post-body h4:first-child { margin-top: 0; }
|
|
196
|
+
.blog-post-body ul, .blog-post-body ol {
|
|
197
|
+
padding-left: 1.5rem;
|
|
198
|
+
margin: 0.5rem 0;
|
|
199
|
+
}
|
|
200
|
+
.blog-post-body li { margin-bottom: 0.4rem; }
|
|
201
|
+
.blog-post-body pre {
|
|
202
|
+
background: rgba(0,0,0,0.3);
|
|
203
|
+
border-radius: var(--radius);
|
|
204
|
+
padding: 1rem 1.25rem;
|
|
205
|
+
overflow-x: auto;
|
|
206
|
+
margin: 0.75rem 0;
|
|
207
|
+
font-size: 0.85rem;
|
|
208
|
+
line-height: 1.5;
|
|
209
|
+
}
|
|
210
|
+
.blog-post-body code {
|
|
211
|
+
background: rgba(255,255,255,0.06);
|
|
212
|
+
padding: 0.15em 0.4em;
|
|
213
|
+
border-radius: 4px;
|
|
214
|
+
font-size: 0.9em;
|
|
215
|
+
}
|
|
216
|
+
.blog-post-body pre code {
|
|
217
|
+
background: none;
|
|
218
|
+
padding: 0;
|
|
219
|
+
}
|
|
220
|
+
|
|
105
221
|
/* -- Footer -- */
|
|
106
222
|
.footer {
|
|
107
223
|
text-align: center;
|
|
@@ -4,6 +4,11 @@
|
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
6
|
<title>{{NAME}}</title>
|
|
7
|
+
<meta name="description" content="">
|
|
8
|
+
<meta property="og:title" content="{{NAME}}">
|
|
9
|
+
<meta property="og:description" content="">
|
|
10
|
+
<meta property="og:type" content="website">
|
|
11
|
+
<meta name="robots" content="index, follow">
|
|
7
12
|
<base href="/">
|
|
8
13
|
<link rel="stylesheet" href="global.css">
|
|
9
14
|
<script src="zquery.min.js"></script>
|
|
@@ -15,8 +20,9 @@
|
|
|
15
20
|
<nav class="navbar">
|
|
16
21
|
<span class="brand">⚡ {{NAME}}</span>
|
|
17
22
|
<div class="nav-links">
|
|
18
|
-
<a z-link="/" class="nav-link">Home</a>
|
|
19
|
-
<a z-link="/
|
|
23
|
+
<a z-link="/" class="nav-link" z-active-route="/" z-active-exact>Home</a>
|
|
24
|
+
<a z-link="/blog" class="nav-link" z-active-route="/blog">Blog</a>
|
|
25
|
+
<a z-link="/about" class="nav-link" z-active-route="/about">About</a>
|
|
20
26
|
</div>
|
|
21
27
|
</nav>
|
|
22
28
|
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// server/data/posts.js - Blog post data
|
|
2
|
+
//
|
|
3
|
+
// Simulates a database or CMS. In a real app you'd fetch from a DB,
|
|
4
|
+
// headless CMS, or markdown files. The server imports this data and
|
|
5
|
+
// passes it as props to blog components during SSR.
|
|
6
|
+
|
|
7
|
+
export const posts = [
|
|
8
|
+
{
|
|
9
|
+
slug: 'why-ssr-matters',
|
|
10
|
+
title: 'Why Server-Side Rendering Matters',
|
|
11
|
+
date: '2025-12-15',
|
|
12
|
+
tag: 'SSR',
|
|
13
|
+
summary:
|
|
14
|
+
'SSR delivers fully rendered HTML to the browser, improving first contentful paint, SEO indexability, and perceived performance on slower connections.',
|
|
15
|
+
body: `
|
|
16
|
+
<p>Server-Side Rendering (SSR) generates the full HTML for a page on the server
|
|
17
|
+
before sending it to the browser. Instead of shipping an empty shell and waiting
|
|
18
|
+
for JavaScript to populate it, the user sees content immediately.</p>
|
|
19
|
+
|
|
20
|
+
<h4>Key Benefits</h4>
|
|
21
|
+
<ul>
|
|
22
|
+
<li><strong>Faster First Paint</strong> — HTML arrives ready to display.
|
|
23
|
+
No waiting for JS bundles to download and execute.</li>
|
|
24
|
+
<li><strong>SEO Friendly</strong> — Search engine crawlers see complete
|
|
25
|
+
content without running JavaScript.</li>
|
|
26
|
+
<li><strong>Social Sharing</strong> — Open Graph meta tags are present in
|
|
27
|
+
the initial response, so link previews work everywhere.</li>
|
|
28
|
+
<li><strong>Accessibility</strong> — Content is available even if JS fails
|
|
29
|
+
to load or is disabled.</li>
|
|
30
|
+
</ul>
|
|
31
|
+
|
|
32
|
+
<h4>The Trade-Off</h4>
|
|
33
|
+
<p>SSR adds server-side compute cost per request. Strategies like render
|
|
34
|
+
caching, edge deployment, and stale-while-revalidate headers help keep
|
|
35
|
+
response times low while maintaining the benefits of dynamic rendering.</p>
|
|
36
|
+
`,
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
slug: 'hydration-explained',
|
|
40
|
+
title: 'Hydration: Picking Up Where the Server Left Off',
|
|
41
|
+
date: '2025-12-20',
|
|
42
|
+
tag: 'Architecture',
|
|
43
|
+
summary:
|
|
44
|
+
'After the server sends HTML, the client "hydrates" the page — attaching event listeners and reactive state so the SPA takes over without a full re-render.',
|
|
45
|
+
body: `
|
|
46
|
+
<p>Hydration is the process where the client-side framework takes ownership
|
|
47
|
+
of server-rendered HTML. The DOM is already there — hydration wires up event
|
|
48
|
+
handlers, reactive state, and component lifecycles on top of it.</p>
|
|
49
|
+
|
|
50
|
+
<h4>How It Works in zQuery</h4>
|
|
51
|
+
<ol>
|
|
52
|
+
<li>The server renders a component to HTML with <code>app.renderToString()</code>,
|
|
53
|
+
adding a <code>data-zq-ssr</code> marker.</li>
|
|
54
|
+
<li>The HTML is injected into the page shell and sent to the browser.</li>
|
|
55
|
+
<li>The client loads the same component definitions and registers them
|
|
56
|
+
with <code>$.component()</code>.</li>
|
|
57
|
+
<li>The router detects the SSR marker and hydrates instead of re-rendering,
|
|
58
|
+
preserving the existing DOM while activating reactivity.</li>
|
|
59
|
+
</ol>
|
|
60
|
+
|
|
61
|
+
<h4>Why This Matters</h4>
|
|
62
|
+
<p>Without hydration, the client would discard the server HTML and re-render
|
|
63
|
+
from scratch — causing a flash of empty content. Hydration gives you the best
|
|
64
|
+
of both worlds: fast server-rendered first paint with full client interactivity.</p>
|
|
65
|
+
`,
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
slug: 'shared-components',
|
|
69
|
+
title: 'Shared Components Across Client and Server',
|
|
70
|
+
date: '2026-01-05',
|
|
71
|
+
tag: 'Components',
|
|
72
|
+
summary:
|
|
73
|
+
'Write your component once and use it everywhere. The same definition object that powers client rendering can be imported by the SSR server to produce identical markup.',
|
|
74
|
+
body: `
|
|
75
|
+
<p>In zQuery, a component is a plain JavaScript object with <code>state</code>,
|
|
76
|
+
<code>render()</code>, and optional lifecycle hooks. This simple contract means
|
|
77
|
+
the same file works on both sides:</p>
|
|
78
|
+
|
|
79
|
+
<pre><code>// components/greeting.js
|
|
80
|
+
export const greeting = {
|
|
81
|
+
state: () => ({ name: 'World' }),
|
|
82
|
+
render() {
|
|
83
|
+
return \`<h1>Hello, \${this.state.name}!</h1>\`;
|
|
84
|
+
}
|
|
85
|
+
};</code></pre>
|
|
86
|
+
|
|
87
|
+
<h4>Client</h4>
|
|
88
|
+
<pre><code>import { greeting } from './components/greeting.js';
|
|
89
|
+
$.component('greeting', greeting);</code></pre>
|
|
90
|
+
|
|
91
|
+
<h4>Server</h4>
|
|
92
|
+
<pre><code>import { greeting } from '../app/components/greeting.js';
|
|
93
|
+
app.component('greeting', greeting);
|
|
94
|
+
const html = await app.renderToString('greeting', { name: 'Tony' });</code></pre>
|
|
95
|
+
|
|
96
|
+
<p>No special file conventions, no build-time transforms. Just JavaScript
|
|
97
|
+
modules shared between environments.</p>
|
|
98
|
+
`,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
slug: 'ssr-caching-strategies',
|
|
102
|
+
title: 'Caching Strategies for SSR Pages',
|
|
103
|
+
date: '2026-01-18',
|
|
104
|
+
tag: 'Performance',
|
|
105
|
+
summary:
|
|
106
|
+
'Combine stale-while-revalidate headers, in-memory render caching, and CDN edge caching to serve SSR pages at static-site speed without sacrificing freshness.',
|
|
107
|
+
body: `
|
|
108
|
+
<p>SSR is powerful but adds per-request compute. Smart caching lets you keep
|
|
109
|
+
the dynamic benefits while serving at near-static speed.</p>
|
|
110
|
+
|
|
111
|
+
<h4>Layer 1: In-Memory Cache</h4>
|
|
112
|
+
<p>Cache rendered HTML in a <code>Map</code> keyed by route. Set a TTL
|
|
113
|
+
(e.g. 60 seconds) and serve cached responses until they expire. Simple and
|
|
114
|
+
effective for moderate traffic.</p>
|
|
115
|
+
|
|
116
|
+
<h4>Layer 2: HTTP Cache Headers</h4>
|
|
117
|
+
<pre><code>Cache-Control: public, s-maxage=60, stale-while-revalidate=300</code></pre>
|
|
118
|
+
<p>This tells CDNs to serve cached content for 60s, then revalidate in the
|
|
119
|
+
background for up to 5 minutes. Users always get a fast response.</p>
|
|
120
|
+
|
|
121
|
+
<h4>Layer 3: CDN Edge Caching</h4>
|
|
122
|
+
<p>Deploy behind a CDN (Cloudflare, Vercel Edge, Fastly) so cached pages are
|
|
123
|
+
served from the nearest edge node. Combine with geographic routing for
|
|
124
|
+
sub-100ms global TTFB.</p>
|
|
125
|
+
|
|
126
|
+
<h4>When to Skip Caching</h4>
|
|
127
|
+
<p>User-specific pages (dashboards, settings) shouldn't be edge-cached.
|
|
128
|
+
Use <code>Cache-Control: private</code> and rely on in-memory caching
|
|
129
|
+
or skip caching entirely for authenticated routes.</p>
|
|
130
|
+
`,
|
|
131
|
+
},
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
/** Find a single post by slug. Returns undefined if not found. */
|
|
135
|
+
export function getPostBySlug(slug) {
|
|
136
|
+
return posts.find(p => p.slug === slug);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Return all posts (summary only — no body). */
|
|
140
|
+
export function getAllPosts() {
|
|
141
|
+
return posts.map(({ slug, title, date, tag, summary }) => ({
|
|
142
|
+
slug, title, date, tag, summary,
|
|
143
|
+
}));
|
|
144
|
+
}
|
|
@@ -12,14 +12,19 @@ import { createServer } from 'node:http';
|
|
|
12
12
|
import { readFile } from 'node:fs/promises';
|
|
13
13
|
import { join, extname, resolve } from 'node:path';
|
|
14
14
|
import { fileURLToPath } from 'node:url';
|
|
15
|
-
import { createSSRApp } from 'zero-query/ssr';
|
|
15
|
+
import { createSSRApp, matchRoute } from 'zero-query/ssr';
|
|
16
16
|
|
|
17
17
|
// Shared component definitions - same ones the client registers
|
|
18
18
|
import { homePage } from '../app/components/home.js';
|
|
19
19
|
import { aboutPage } from '../app/components/about.js';
|
|
20
|
+
import { blogList } from '../app/components/blog/index.js';
|
|
21
|
+
import { blogPost } from '../app/components/blog/post.js';
|
|
20
22
|
import { notFound } from '../app/components/not-found.js';
|
|
21
23
|
import { routes } from '../app/routes.js';
|
|
22
24
|
|
|
25
|
+
// Server-side data — simulates a database or CMS
|
|
26
|
+
import { getAllPosts, getPostBySlug } from './data/posts.js';
|
|
27
|
+
|
|
23
28
|
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
|
24
29
|
const ROOT = join(__dirname, '..');
|
|
25
30
|
const PORT = parseInt(process.env.PORT || '3000', 10);
|
|
@@ -29,20 +34,91 @@ const PORT = parseInt(process.env.PORT || '3000', 10);
|
|
|
29
34
|
const app = createSSRApp();
|
|
30
35
|
app.component('home-page', homePage);
|
|
31
36
|
app.component('about-page', aboutPage);
|
|
37
|
+
app.component('blog-list', blogList);
|
|
38
|
+
app.component('blog-post', blogPost);
|
|
32
39
|
app.component('not-found', notFound);
|
|
33
40
|
|
|
34
|
-
// ---
|
|
41
|
+
// --- Server-side data fetching ----------------------------------------------
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Fetch data for a matched route. This is where you'd query a database,
|
|
45
|
+
* call an API, or read from the filesystem in a real application.
|
|
46
|
+
* The returned object is passed as props to the component during SSR.
|
|
47
|
+
*/
|
|
48
|
+
function getPropsForRoute(component, params) {
|
|
49
|
+
switch (component) {
|
|
50
|
+
case 'blog-list':
|
|
51
|
+
return { posts: getAllPosts() };
|
|
52
|
+
case 'blog-post':
|
|
53
|
+
return { post: getPostBySlug(params.slug) || null };
|
|
54
|
+
default:
|
|
55
|
+
return {};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// --- SEO metadata per route -------------------------------------------------
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Return page-specific metadata for SEO, social sharing, and browser tabs.
|
|
63
|
+
* The server injects these into the HTML shell's <head> before sending.
|
|
64
|
+
*
|
|
65
|
+
* In a real app you'd pull this from a CMS or database alongside the content.
|
|
66
|
+
* Open Graph tags ensure rich link previews on social media.
|
|
67
|
+
*/
|
|
68
|
+
function getMetaForRoute(component, params, props) {
|
|
69
|
+
const base = {
|
|
70
|
+
title: '{{NAME}}',
|
|
71
|
+
description: 'A zQuery SSR application.',
|
|
72
|
+
ogType: 'website',
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
switch (component) {
|
|
76
|
+
case 'home-page':
|
|
77
|
+
return {
|
|
78
|
+
...base,
|
|
79
|
+
title: '{{NAME}} — Home',
|
|
80
|
+
description: 'A server-rendered application built with zQuery. Fast first paint, SEO-friendly, zero dependencies.',
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
case 'blog-list':
|
|
84
|
+
return {
|
|
85
|
+
...base,
|
|
86
|
+
title: 'Blog — {{NAME}}',
|
|
87
|
+
description: 'Articles on server-side rendering, hydration, shared components, and modern web architecture.',
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
case 'blog-post': {
|
|
91
|
+
const post = props.post;
|
|
92
|
+
if (!post) return { ...base, title: 'Post Not Found — {{NAME}}' };
|
|
93
|
+
return {
|
|
94
|
+
...base,
|
|
95
|
+
title: `${post.title} — {{NAME}}`,
|
|
96
|
+
description: post.summary,
|
|
97
|
+
ogType: 'article',
|
|
98
|
+
};
|
|
99
|
+
}
|
|
35
100
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
101
|
+
case 'about-page':
|
|
102
|
+
return {
|
|
103
|
+
...base,
|
|
104
|
+
title: 'About — {{NAME}}',
|
|
105
|
+
description: 'Learn about zQuery — a zero-dependency frontend micro-library for reactive components, routing, SSR, and state management.',
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
default:
|
|
109
|
+
return {
|
|
110
|
+
...base,
|
|
111
|
+
title: 'Page Not Found — {{NAME}}',
|
|
112
|
+
description: 'The page you\'re looking for doesn\'t exist.',
|
|
113
|
+
};
|
|
114
|
+
}
|
|
39
115
|
}
|
|
40
116
|
|
|
41
117
|
// --- Render a full HTML page ------------------------------------------------
|
|
42
118
|
|
|
43
119
|
// Read the index.html shell once at startup — it already has z-link nav,
|
|
44
120
|
// client scripts (zquery.min.js + app/app.js), and the <z-outlet> tag.
|
|
45
|
-
// On each request we
|
|
121
|
+
// On each request we render the matched component into the shell.
|
|
46
122
|
let shellCache = null;
|
|
47
123
|
async function getShell() {
|
|
48
124
|
if (!shellCache) shellCache = await readFile(join(ROOT, 'index.html'), 'utf-8');
|
|
@@ -50,23 +126,22 @@ async function getShell() {
|
|
|
50
126
|
}
|
|
51
127
|
|
|
52
128
|
async function render(pathname) {
|
|
53
|
-
const component = matchRoute(pathname);
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return html;
|
|
129
|
+
const { component, params } = matchRoute(routes, pathname);
|
|
130
|
+
const props = getPropsForRoute(component, params);
|
|
131
|
+
const meta = getMetaForRoute(component, params, props);
|
|
132
|
+
|
|
133
|
+
return app.renderShell(await getShell(), {
|
|
134
|
+
component,
|
|
135
|
+
props,
|
|
136
|
+
title: meta.title,
|
|
137
|
+
description: meta.description,
|
|
138
|
+
og: {
|
|
139
|
+
title: meta.title,
|
|
140
|
+
description: meta.description,
|
|
141
|
+
type: meta.ogType,
|
|
142
|
+
},
|
|
143
|
+
ssrData: { component, params, props, meta },
|
|
144
|
+
});
|
|
70
145
|
}
|
|
71
146
|
|
|
72
147
|
// --- Static files -----------------------------------------------------------
|
|
@@ -100,6 +175,25 @@ createServer(async (req, res) => {
|
|
|
100
175
|
// Static assets (CSS, images, etc.)
|
|
101
176
|
if (pathname !== '/' && await serveStatic(res, pathname)) return;
|
|
102
177
|
|
|
178
|
+
// --- JSON API for client-side navigation ----------------------------------
|
|
179
|
+
// The client fetches data here when navigating via SPA (after initial SSR).
|
|
180
|
+
// In a real app, these would query a database or external API.
|
|
181
|
+
|
|
182
|
+
if (pathname === '/api/posts') {
|
|
183
|
+
const json = JSON.stringify(getAllPosts());
|
|
184
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
185
|
+
res.end(json);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const postMatch = /^\/api\/posts\/([^/]+)$/.exec(pathname);
|
|
190
|
+
if (postMatch) {
|
|
191
|
+
const post = getPostBySlug(postMatch[1]) || null;
|
|
192
|
+
res.writeHead(post ? 200 : 404, { 'Content-Type': 'application/json' });
|
|
193
|
+
res.end(JSON.stringify(post));
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
103
197
|
// SSR route
|
|
104
198
|
try {
|
|
105
199
|
const html = await render(pathname);
|
package/dist/zquery.dist.zip
CHANGED
|
Binary file
|
package/dist/zquery.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* zQuery (zeroQuery) v1.0.
|
|
2
|
+
* zQuery (zeroQuery) v1.0.5
|
|
3
3
|
* Lightweight Frontend Library
|
|
4
4
|
* https://github.com/tonywied17/zero-query
|
|
5
5
|
* (c) 2026 Anthony Wiedman - MIT License
|
|
@@ -3498,13 +3498,28 @@ class Component {
|
|
|
3498
3498
|
if (el.contains(e.target)) continue;
|
|
3499
3499
|
}
|
|
3500
3500
|
|
|
3501
|
-
// Key modifiers - filter keyboard events by key
|
|
3501
|
+
// Key modifiers - filter keyboard events by key.
|
|
3502
|
+
// Named shortcuts map common names to their e.key values.
|
|
3503
|
+
// Any modifier not recognised as a built-in behaviour, timing,
|
|
3504
|
+
// or system modifier is matched against e.key (case-insensitive)
|
|
3505
|
+
// so that arbitrary keys work: .a, .f1, .+, .0, .arrowup, etc.
|
|
3502
3506
|
const _keyMap = { enter: 'Enter', escape: 'Escape', tab: 'Tab', space: ' ', delete: 'Delete|Backspace', up: 'ArrowUp', down: 'ArrowDown', left: 'ArrowLeft', right: 'ArrowRight' };
|
|
3507
|
+
const _nonKeyMods = new Set(['prevent','stop','self','once','outside','capture','passive','debounce','throttle','ctrl','shift','alt','meta']);
|
|
3503
3508
|
let keyFiltered = false;
|
|
3504
|
-
for (
|
|
3509
|
+
for (let mi = 0; mi < modifiers.length; mi++) {
|
|
3510
|
+
const mod = modifiers[mi];
|
|
3505
3511
|
if (_keyMap[mod]) {
|
|
3506
3512
|
const keys = _keyMap[mod].split('|');
|
|
3507
3513
|
if (!e.key || !keys.includes(e.key)) { keyFiltered = true; break; }
|
|
3514
|
+
} else if (_nonKeyMods.has(mod)) {
|
|
3515
|
+
continue;
|
|
3516
|
+
} else if (/^\d+$/.test(mod) && mi > 0 && (modifiers[mi - 1] === 'debounce' || modifiers[mi - 1] === 'throttle')) {
|
|
3517
|
+
// Numeric value following debounce/throttle — skip (it's a ms parameter)
|
|
3518
|
+
continue;
|
|
3519
|
+
} else {
|
|
3520
|
+
// Dynamic key match — compare modifier against e.key
|
|
3521
|
+
// Case-insensitive: .a matches 'a' and 'A', .f1 matches 'F1'
|
|
3522
|
+
if (!e.key || e.key.toLowerCase() !== mod.toLowerCase()) { keyFiltered = true; break; }
|
|
3508
3523
|
}
|
|
3509
3524
|
}
|
|
3510
3525
|
if (keyFiltered) continue;
|
|
@@ -4455,24 +4470,15 @@ class Router {
|
|
|
4455
4470
|
|
|
4456
4471
|
add(route) {
|
|
4457
4472
|
// Compile path pattern into regex
|
|
4458
|
-
const keys =
|
|
4459
|
-
const pattern = route.path
|
|
4460
|
-
.replace(/:(\w+)/g, (_, key) => { keys.push(key); return '([^/]+)'; })
|
|
4461
|
-
.replace(/\*/g, '(.*)');
|
|
4462
|
-
const regex = new RegExp(`^${pattern}$`);
|
|
4463
|
-
|
|
4473
|
+
const { regex, keys } = compilePath(route.path);
|
|
4464
4474
|
this._routes.push({ ...route, _regex: regex, _keys: keys });
|
|
4465
4475
|
|
|
4466
4476
|
// Per-route fallback: register an alias path for the same component.
|
|
4467
4477
|
// e.g. { path: '/docs/:section', fallback: '/docs', component: 'docs-page' }
|
|
4468
4478
|
// When matched via fallback, missing params are undefined.
|
|
4469
4479
|
if (route.fallback) {
|
|
4470
|
-
const
|
|
4471
|
-
|
|
4472
|
-
.replace(/:(\w+)/g, (_, key) => { fbKeys.push(key); return '([^/]+)'; })
|
|
4473
|
-
.replace(/\*/g, '(.*)');
|
|
4474
|
-
const fbRegex = new RegExp(`^${fbPattern}$`);
|
|
4475
|
-
this._routes.push({ ...route, path: route.fallback, _regex: fbRegex, _keys: fbKeys });
|
|
4480
|
+
const fb = compilePath(route.fallback);
|
|
4481
|
+
this._routes.push({ ...route, path: route.fallback, _regex: fb.regex, _keys: fb.keys });
|
|
4476
4482
|
}
|
|
4477
4483
|
|
|
4478
4484
|
return this;
|
|
@@ -4969,6 +4975,64 @@ class Router {
|
|
|
4969
4975
|
}
|
|
4970
4976
|
|
|
4971
4977
|
|
|
4978
|
+
// ---------------------------------------------------------------------------
|
|
4979
|
+
// Path compilation (shared by Router.add and matchRoute)
|
|
4980
|
+
// ---------------------------------------------------------------------------
|
|
4981
|
+
|
|
4982
|
+
/**
|
|
4983
|
+
* Compile a route path pattern into a RegExp and param key list.
|
|
4984
|
+
* Supports `:param` segments and `*` wildcard.
|
|
4985
|
+
* @param {string} path - e.g. '/user/:id' or '/files/*'
|
|
4986
|
+
* @returns {{ regex: RegExp, keys: string[] }}
|
|
4987
|
+
*/
|
|
4988
|
+
function compilePath(path) {
|
|
4989
|
+
const keys = [];
|
|
4990
|
+
const pattern = path
|
|
4991
|
+
.replace(/:(\w+)/g, (_, key) => { keys.push(key); return '([^/]+)'; })
|
|
4992
|
+
.replace(/\*/g, '(.*)');
|
|
4993
|
+
return { regex: new RegExp(`^${pattern}$`), keys };
|
|
4994
|
+
}
|
|
4995
|
+
|
|
4996
|
+
// ---------------------------------------------------------------------------
|
|
4997
|
+
// Standalone route matcher (DOM-free — usable on server and client)
|
|
4998
|
+
// ---------------------------------------------------------------------------
|
|
4999
|
+
|
|
5000
|
+
/**
|
|
5001
|
+
* Match a pathname against an array of route definitions.
|
|
5002
|
+
* Returns `{ component, params }`. If no route matches, falls back to the
|
|
5003
|
+
* `fallback` component name (default `'not-found'`).
|
|
5004
|
+
*
|
|
5005
|
+
* This is the same matching logic the client-side router uses internally,
|
|
5006
|
+
* extracted so SSR servers can resolve URLs without the DOM.
|
|
5007
|
+
*
|
|
5008
|
+
* @param {Array<{ path: string, component: string, fallback?: string }>} routes
|
|
5009
|
+
* @param {string} pathname - URL path to match, e.g. '/blog/my-post'
|
|
5010
|
+
* @param {string} [fallback='not-found'] - Component name when nothing matches
|
|
5011
|
+
* @returns {{ component: string, params: Record<string, string> }}
|
|
5012
|
+
*/
|
|
5013
|
+
function matchRoute(routes, pathname, fallback = 'not-found') {
|
|
5014
|
+
for (const route of routes) {
|
|
5015
|
+
const { regex, keys } = compilePath(route.path);
|
|
5016
|
+
const m = pathname.match(regex);
|
|
5017
|
+
if (m) {
|
|
5018
|
+
const params = {};
|
|
5019
|
+
keys.forEach((key, i) => { params[key] = m[i + 1]; });
|
|
5020
|
+
return { component: route.component, params };
|
|
5021
|
+
}
|
|
5022
|
+
// Per-route fallback alias (same as Router.add)
|
|
5023
|
+
if (route.fallback) {
|
|
5024
|
+
const fb = compilePath(route.fallback);
|
|
5025
|
+
const fbm = pathname.match(fb.regex);
|
|
5026
|
+
if (fbm) {
|
|
5027
|
+
const params = {};
|
|
5028
|
+
fb.keys.forEach((key, i) => { params[key] = fbm[i + 1]; });
|
|
5029
|
+
return { component: route.component, params };
|
|
5030
|
+
}
|
|
5031
|
+
}
|
|
5032
|
+
}
|
|
5033
|
+
return { component: fallback, params: {} };
|
|
5034
|
+
}
|
|
5035
|
+
|
|
4972
5036
|
// ---------------------------------------------------------------------------
|
|
4973
5037
|
// Factory
|
|
4974
5038
|
// ---------------------------------------------------------------------------
|
|
@@ -6124,8 +6188,9 @@ $.morphElement = morphElement;
|
|
|
6124
6188
|
$.safeEval = safeEval;
|
|
6125
6189
|
|
|
6126
6190
|
// --- Router ----------------------------------------------------------------
|
|
6127
|
-
$.router
|
|
6128
|
-
$.getRouter
|
|
6191
|
+
$.router = createRouter;
|
|
6192
|
+
$.getRouter = getRouter;
|
|
6193
|
+
$.matchRoute = matchRoute;
|
|
6129
6194
|
|
|
6130
6195
|
// --- Store -----------------------------------------------------------------
|
|
6131
6196
|
$.store = createStore;
|
|
@@ -6189,9 +6254,9 @@ $.validate = validate;
|
|
|
6189
6254
|
$.formatError = formatError;
|
|
6190
6255
|
|
|
6191
6256
|
// --- Meta ------------------------------------------------------------------
|
|
6192
|
-
$.version = '1.0.
|
|
6193
|
-
$.libSize = '~
|
|
6194
|
-
$.unitTests = {"passed":
|
|
6257
|
+
$.version = '1.0.5';
|
|
6258
|
+
$.libSize = '~107 KB';
|
|
6259
|
+
$.unitTests = {"passed":1931,"failed":0,"total":1931,"suites":523,"duration":3815,"ok":true};
|
|
6195
6260
|
$.meta = {}; // populated at build time by CLI bundler
|
|
6196
6261
|
|
|
6197
6262
|
$.noConflict = () => {
|