fondu 0.6.0 → 0.6.2
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 +2 -2
- package/dist/client.js +5 -1
- package/dist/server/app.js +9 -2
- package/dist/server/document.js +1 -1
- package/dist/server/flash.js +2 -1
- package/dist/server/prerender.js +5 -2
- package/dist/server/props.js +1 -4
- package/dist/server/protocol.js +28 -8
- package/dist/server/validate.js +10 -3
- package/dist/virtual.d.ts +3 -1
- package/dist/vite/hmr.js +2 -1
- package/dist/vite/index.js +15 -5
- package/dist/vite/layouts.js +3 -1
- package/dist/vite/prerender.js +9 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,8 +23,8 @@ bun dev
|
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
It asks where the app runs (Bun, Cloudflare Workers or Node; or `--target bun|cloudflare|node`) and whether to add
|
|
26
|
-
code checks (oxfmt, oxlint, lefthook, commitlint; or `--checks`), and writes a small app: a route, its page, the
|
|
27
|
-
page, Tailwind CSS, editor snippets and an `AGENTS.md` for coding agents.
|
|
26
|
+
code checks (oxfmt, oxlint, lefthook, commitlint; or `--checks`), and writes a small app: a route, its page, the
|
|
27
|
+
error page, Tailwind CSS, editor snippets and an `AGENTS.md` for coding agents. `--yes` asks nothing more.
|
|
28
28
|
|
|
29
29
|
[demo.fondu.dev](https://demo.fondu.dev) ([apps/demo](https://github.com/fondujs/fondu/tree/main/apps/demo)) shows every Inertia feature, one page each, on
|
|
30
30
|
Cloudflare Workers with D1 and Better Auth.
|
package/dist/client.js
CHANGED
|
@@ -23,6 +23,10 @@ export function prerendered() {
|
|
|
23
23
|
const page = JSON.parse(response.data);
|
|
24
24
|
if (!page.version || page.version === version)
|
|
25
25
|
return response;
|
|
26
|
-
return {
|
|
26
|
+
return {
|
|
27
|
+
status: 409,
|
|
28
|
+
data: '',
|
|
29
|
+
headers: { 'x-inertia-location': page.url, 'x-inertia-version': page.version },
|
|
30
|
+
};
|
|
27
31
|
});
|
|
28
32
|
}
|
package/dist/server/app.js
CHANGED
|
@@ -87,14 +87,21 @@ export function createApp(options = {}) {
|
|
|
87
87
|
const routes = files
|
|
88
88
|
.filter(isRouteFile)
|
|
89
89
|
// static before dynamic before catch-all, segment by segment
|
|
90
|
-
.map((file) => ({
|
|
90
|
+
.map((file) => ({
|
|
91
|
+
file,
|
|
92
|
+
pattern: withSlash(routePattern(file), append),
|
|
93
|
+
mod: modules[ROOT + file],
|
|
94
|
+
}))
|
|
91
95
|
.sort((a, b) => rank(a.pattern).localeCompare(rank(b.pattern)));
|
|
92
96
|
for (const { file, pattern, mod } of routes) {
|
|
93
97
|
const mws = middlewareFor(file, middlewareFiles).flatMap((f) => chain(modules[ROOT + f].default ?? []));
|
|
94
98
|
prerendered.push({
|
|
95
99
|
file,
|
|
96
100
|
pattern,
|
|
97
|
-
settings: [
|
|
101
|
+
settings: [
|
|
102
|
+
file,
|
|
103
|
+
...middlewareFor(file, [...middlewareFiles, '_middleware.ts']).reverse(),
|
|
104
|
+
].map((f) => modules[ROOT + f]?.prerender),
|
|
98
105
|
get: Boolean(mod.loader ?? mod.GET),
|
|
99
106
|
others: [
|
|
100
107
|
...(mod.action || mod.actions || mod.POST ? ['POST'] : []),
|
package/dist/server/document.js
CHANGED
|
@@ -51,5 +51,5 @@ export async function renderDocument(c, page) {
|
|
|
51
51
|
const [before, between, after, ...more] = html.split(new RegExp(`${HEAD}|${BODY}`));
|
|
52
52
|
if (after === undefined || more.length || html.indexOf(HEAD) > html.indexOf(BODY))
|
|
53
53
|
throw new Error('fondu: the document must have `head` once, in <head>, and `body` once, in <body>');
|
|
54
|
-
return before + (ssr?.head.join('') ?? '') + assetTags(page.component, nonce) + between + body + after;
|
|
54
|
+
return (before + (ssr?.head.join('') ?? '') + assetTags(page.component, nonce) + between + body + after);
|
|
55
55
|
}
|
package/dist/server/flash.js
CHANGED
|
@@ -7,7 +7,8 @@ const state = (c) => {
|
|
|
7
7
|
return s;
|
|
8
8
|
};
|
|
9
9
|
/** https, directly or behind a proxy that says so */
|
|
10
|
-
export const isSecure = (c) => new URL(c.req.url).protocol === 'https:' ||
|
|
10
|
+
export const isSecure = (c) => new URL(c.req.url).protocol === 'https:' ||
|
|
11
|
+
c.req.header('x-forwarded-proto')?.split(',')[0]?.trim() === 'https';
|
|
11
12
|
const cookieName = (c) => (isSecure(c) ? '__Host-fondu-flash' : 'fondu-flash');
|
|
12
13
|
/** Flash data for the next page (`page.flash` / `usePage().flash`, Inertia's `flash` event). */
|
|
13
14
|
export function flash(c, data) {
|
package/dist/server/prerender.js
CHANGED
|
@@ -65,7 +65,9 @@ export function prerenderRoutes(app, routes, version) {
|
|
|
65
65
|
// security headers give it a new one
|
|
66
66
|
const nonce = c.get('secureHeadersNonce');
|
|
67
67
|
if (!data && nonce) {
|
|
68
|
-
const html = new TextDecoder()
|
|
68
|
+
const html = new TextDecoder()
|
|
69
|
+
.decode(body)
|
|
70
|
+
.replace(/(<(?:script|link)\b[^>]*\snonce=")[^"]*"/gi, `$1${nonce}"`);
|
|
69
71
|
return c.html(html, 200);
|
|
70
72
|
}
|
|
71
73
|
return c.body(body, 200, {
|
|
@@ -89,7 +91,8 @@ export function prerenderRoutes(app, routes, version) {
|
|
|
89
91
|
});
|
|
90
92
|
app.get('/__fondu/prerender', async (c) => {
|
|
91
93
|
// set for the build only: a Worker binding, or the environment of `node` / `bun dist/index.js`
|
|
92
|
-
const env = c.env?.FONDU_PRERENDER ??
|
|
94
|
+
const env = c.env?.FONDU_PRERENDER ??
|
|
95
|
+
globalThis.process?.env?.FONDU_PRERENDER;
|
|
93
96
|
if (!env)
|
|
94
97
|
return c.notFound();
|
|
95
98
|
return c.json({ version, ...(await prerenderPaths(routes())) });
|
package/dist/server/props.js
CHANGED
|
@@ -40,8 +40,5 @@ const list = (value) => [value ?? []].flat();
|
|
|
40
40
|
const merger = (strategy) => (value, options = {}) => make(strategy === 'append' ? 'merge' : 'prepend', value, { merge: { strategy, paths: list(options.path), matchOn: list(options.matchOn) } }, ['defer', 'once']);
|
|
41
41
|
export const merge = merger('append');
|
|
42
42
|
export const prepend = merger('prepend');
|
|
43
|
-
export const deepMerge = (value, options = {}) => make('deepMerge', value, { merge: { strategy: 'deep', paths: [], matchOn: list(options.matchOn) } }, [
|
|
44
|
-
'defer',
|
|
45
|
-
'once',
|
|
46
|
-
]);
|
|
43
|
+
export const deepMerge = (value, options = {}) => make('deepMerge', value, { merge: { strategy: 'deep', paths: [], matchOn: list(options.matchOn) } }, ['defer', 'once']);
|
|
47
44
|
export const resolve = async (value) => typeof value === 'function' ? value() : value;
|
package/dist/server/protocol.js
CHANGED
|
@@ -23,7 +23,8 @@ export const encryptHistory = () => async (c, next) => {
|
|
|
23
23
|
const devPages = import.meta.env.DEV ? import.meta.glob('/app/pages/**/*.{tsx,jsx,mdx}') : {};
|
|
24
24
|
export const hasPage = (name) => pages
|
|
25
25
|
? pages.includes(name)
|
|
26
|
-
: isPageFile(`${name}.tsx`) &&
|
|
26
|
+
: isPageFile(`${name}.tsx`) &&
|
|
27
|
+
['tsx', 'jsx', 'mdx'].some((ext) => `/app/pages/${name}.${ext}` in devPages);
|
|
27
28
|
const headerSet = (c, name) => {
|
|
28
29
|
const value = c.req.header(name);
|
|
29
30
|
return value === undefined
|
|
@@ -33,7 +34,8 @@ const headerSet = (c, name) => {
|
|
|
33
34
|
.map((s) => s.trim())
|
|
34
35
|
.filter(Boolean));
|
|
35
36
|
};
|
|
36
|
-
const isPrefetch = (c) => c.req.header('purpose') === 'prefetch' ||
|
|
37
|
+
const isPrefetch = (c) => c.req.header('purpose') === 'prefetch' ||
|
|
38
|
+
c.req.header('sec-purpose')?.includes('prefetch') === true;
|
|
37
39
|
/** This URL as the page's: the request's for a GET, the page the request came from otherwise. */
|
|
38
40
|
function pageUrl(c) {
|
|
39
41
|
const here = new URL(c.req.url);
|
|
@@ -73,19 +75,28 @@ export async function render(c, component, ...[props = {}, options = {}]) {
|
|
|
73
75
|
const onceProps = {};
|
|
74
76
|
const pending = [];
|
|
75
77
|
const listOnce = (key, once) => {
|
|
76
|
-
onceProps[once.key ?? key] = {
|
|
78
|
+
onceProps[once.key ?? key] = {
|
|
79
|
+
prop: key,
|
|
80
|
+
expiresAt: once.ttl ? Date.now() + once.ttl * 1000 : null,
|
|
81
|
+
};
|
|
77
82
|
};
|
|
78
83
|
const listMerge = (key, spec) => {
|
|
79
84
|
if (reset.has(key))
|
|
80
85
|
return;
|
|
81
86
|
if (spec.scroll) {
|
|
82
87
|
// <InfiniteScroll> says which side it loads: the previous page goes before what the client holds
|
|
83
|
-
const intent = c.req.header('x-inertia-infinite-scroll-merge-intent') === 'prepend'
|
|
88
|
+
const intent = c.req.header('x-inertia-infinite-scroll-merge-intent') === 'prepend'
|
|
89
|
+
? 'prependProps'
|
|
90
|
+
: 'mergeProps';
|
|
84
91
|
lists[intent].push(`${key}.data`);
|
|
85
92
|
return;
|
|
86
93
|
}
|
|
87
94
|
const { strategy, paths, matchOn } = spec.merge;
|
|
88
|
-
const into = strategy === 'append'
|
|
95
|
+
const into = strategy === 'append'
|
|
96
|
+
? 'mergeProps'
|
|
97
|
+
: strategy === 'prepend'
|
|
98
|
+
? 'prependProps'
|
|
99
|
+
: 'deepMergeProps';
|
|
89
100
|
lists[into].push(...(paths.length ? paths.map((path) => `${key}.${path}`) : [key]));
|
|
90
101
|
lists.matchPropsOn.push(...matchOn.map((field) => `${key}.${field}`));
|
|
91
102
|
};
|
|
@@ -182,8 +193,15 @@ export async function render(c, component, ...[props = {}, options = {}]) {
|
|
|
182
193
|
// ---- the protocol middleware (installed by createApp)
|
|
183
194
|
const versionCheck = async (c, next) => {
|
|
184
195
|
// an Inertia visit made by a client built from other assets: reload the page as a document
|
|
185
|
-
if (version &&
|
|
186
|
-
|
|
196
|
+
if (version &&
|
|
197
|
+
c.req.method === 'GET' &&
|
|
198
|
+
isInertia(c) &&
|
|
199
|
+
c.req.header('x-inertia-version') !== version) {
|
|
200
|
+
return c.body(null, 409, {
|
|
201
|
+
'X-Inertia-Location': c.req.url,
|
|
202
|
+
'X-Inertia-Version': version,
|
|
203
|
+
Vary: 'X-Inertia',
|
|
204
|
+
});
|
|
187
205
|
}
|
|
188
206
|
await next();
|
|
189
207
|
// a URL answers a visit and a document differently: caches keep them apart
|
|
@@ -191,7 +209,9 @@ const versionCheck = async (c, next) => {
|
|
|
191
209
|
if (!isInertia(c))
|
|
192
210
|
return;
|
|
193
211
|
// an empty answer to a visit (an action that returns nothing to show): back to the page, as inertia-laravel does
|
|
194
|
-
if (c.res.status === 200 &&
|
|
212
|
+
if (c.res.status === 200 &&
|
|
213
|
+
!c.res.headers.has('x-inertia') &&
|
|
214
|
+
(await c.res.clone().text()) === '') {
|
|
195
215
|
c.res = back(c);
|
|
196
216
|
return;
|
|
197
217
|
}
|
package/dist/server/validate.js
CHANGED
|
@@ -51,7 +51,10 @@ export async function readBody(c) {
|
|
|
51
51
|
const name = segment === '' ? String(Object.keys(target).length) : segment; // `a[]`: the next index
|
|
52
52
|
const has = Object.hasOwn(target, name);
|
|
53
53
|
if (i === path.length - 1) {
|
|
54
|
-
if (has &&
|
|
54
|
+
if (has &&
|
|
55
|
+
typeof target[name] === 'object' &&
|
|
56
|
+
!(target[name] instanceof File) &&
|
|
57
|
+
!Array.isArray(target[name]))
|
|
55
58
|
throw malformed(`\`${key}\` is both a value and an object`);
|
|
56
59
|
if (has && path.length === 1)
|
|
57
60
|
target[name] = [target[name], value].flat(); // `a=1&a=2`
|
|
@@ -61,7 +64,9 @@ export async function readBody(c) {
|
|
|
61
64
|
else {
|
|
62
65
|
if (!has)
|
|
63
66
|
target[name] = Object.create(null);
|
|
64
|
-
else if (typeof target[name] !== 'object' ||
|
|
67
|
+
else if (typeof target[name] !== 'object' ||
|
|
68
|
+
target[name] instanceof File ||
|
|
69
|
+
Array.isArray(target[name]))
|
|
65
70
|
throw malformed(`\`${key}\` is both a value and an object`);
|
|
66
71
|
target = target[name];
|
|
67
72
|
}
|
|
@@ -107,7 +112,9 @@ export function validator(schemas) {
|
|
|
107
112
|
}
|
|
108
113
|
if (c.req.header('precognition') === 'true') {
|
|
109
114
|
const only = c.req.header('precognition-validate-only')?.split(',').filter(Boolean);
|
|
110
|
-
const shown = only
|
|
115
|
+
const shown = only
|
|
116
|
+
? Object.fromEntries(Object.entries(errors).filter(([field]) => only.includes(field)))
|
|
117
|
+
: errors;
|
|
111
118
|
const headers = { Precognition: 'true', Vary: 'Precognition' };
|
|
112
119
|
return Object.keys(shown).length
|
|
113
120
|
? c.json({ errors: shown }, 422, headers)
|
package/dist/virtual.d.ts
CHANGED
|
@@ -11,7 +11,9 @@ declare module 'virtual:fondu/assets' {
|
|
|
11
11
|
}
|
|
12
12
|
declare module 'virtual:fondu/ssr' {
|
|
13
13
|
/** the app's `createInertiaApp(...)` rendering a page on the server (null: `fondu({ ssr: false })`) */
|
|
14
|
-
export const renderPage:
|
|
14
|
+
export const renderPage:
|
|
15
|
+
| ((page: import('@inertiajs/core').Page) => Promise<{ head: string[]; body: string }>)
|
|
16
|
+
| null
|
|
15
17
|
/** React's static markup of a JSX document (`react-dom/server`'s `renderToStaticMarkup`) */
|
|
16
18
|
export function renderMarkup(element: import('react').ReactElement): string | Promise<string>
|
|
17
19
|
}
|
package/dist/vite/hmr.js
CHANGED
|
@@ -99,7 +99,8 @@ function namedExports(program) {
|
|
|
99
99
|
for (const d of declaration?.declarations ?? [])
|
|
100
100
|
if (d.id.type === 'Identifier')
|
|
101
101
|
names.push(d.id.name);
|
|
102
|
-
for (const s of node.specifiers ??
|
|
102
|
+
for (const s of node.specifiers ??
|
|
103
|
+
[])
|
|
103
104
|
names.push(s.exported.name ?? s.exported.value);
|
|
104
105
|
}
|
|
105
106
|
return names.filter((n) => n !== 'default');
|
package/dist/vite/index.js
CHANGED
|
@@ -98,7 +98,9 @@ export default function fondu(options) {
|
|
|
98
98
|
];
|
|
99
99
|
}
|
|
100
100
|
async function buildPlugin(options) {
|
|
101
|
-
const { default: build } = options.target === 'bun'
|
|
101
|
+
const { default: build } = options.target === 'bun'
|
|
102
|
+
? await import('@hono/vite-build/bun')
|
|
103
|
+
: await import('@hono/vite-build/node');
|
|
102
104
|
// the app's dependencies stay in node_modules (as with any Node server: native modules cannot be bundled)
|
|
103
105
|
const pkg = JSON.parse(readFileSync(resolve('package.json'), 'utf8'));
|
|
104
106
|
const external = Object.keys(pkg.dependencies ?? {}).filter((d) => d !== 'fondu');
|
|
@@ -196,7 +198,9 @@ function loadFailure() {
|
|
|
196
198
|
failed = true;
|
|
197
199
|
if (!visit)
|
|
198
200
|
return next(error);
|
|
199
|
-
res.writeHead(409, {
|
|
201
|
+
res.writeHead(409, {
|
|
202
|
+
'X-Inertia-Location': req.originalUrl ?? req.url,
|
|
203
|
+
});
|
|
200
204
|
res.end();
|
|
201
205
|
});
|
|
202
206
|
},
|
|
@@ -251,7 +255,10 @@ function core(options) {
|
|
|
251
255
|
? { build: { ...client, outDir: 'dist/client', emptyOutDir: true } }
|
|
252
256
|
: {
|
|
253
257
|
ssr: { noExternal: ['fondu'] },
|
|
254
|
-
build: {
|
|
258
|
+
build: {
|
|
259
|
+
copyPublicDir: false,
|
|
260
|
+
rollupOptions: { output: { chunkFileNames: 'server/[name]-[hash].js' } },
|
|
261
|
+
},
|
|
255
262
|
};
|
|
256
263
|
},
|
|
257
264
|
// dev: the dependencies are found before the first request (Vite scans index.html by default; an app has none),
|
|
@@ -310,7 +317,8 @@ function core(options) {
|
|
|
310
317
|
walk(m);
|
|
311
318
|
};
|
|
312
319
|
walk(env.moduleGraph.getModuleById(resolve(config.root, ENTRY)));
|
|
313
|
-
const parts = await Promise.all(css.map(async (url) => (await env.transformRequest(url + (url.includes('?') ? '&' : '?') + 'direct'))
|
|
320
|
+
const parts = await Promise.all(css.map(async (url) => (await env.transformRequest(url + (url.includes('?') ? '&' : '?') + 'direct'))
|
|
321
|
+
?.code ?? ''));
|
|
314
322
|
res.setHeader('Content-Type', 'text/css');
|
|
315
323
|
res.end(parts.join('\n'));
|
|
316
324
|
}
|
|
@@ -373,7 +381,9 @@ function core(options) {
|
|
|
373
381
|
/** `*.server.ts`, or the app's `routes/` and `server.ts` (the root's: a path above it or in a package may have them) */
|
|
374
382
|
const isServerOnly = (id, root) => {
|
|
375
383
|
const file = id.split('?')[0];
|
|
376
|
-
return (/\.server\.[cm]?[jt]sx?$/.test(file) ||
|
|
384
|
+
return (/\.server\.[cm]?[jt]sx?$/.test(file) ||
|
|
385
|
+
file.startsWith(`${root}/app/routes/`) ||
|
|
386
|
+
file === `${root}/app/server.ts`);
|
|
377
387
|
};
|
|
378
388
|
const nonceAttr = `\${nonce ? \` nonce="\${nonce}"\` : ''}`;
|
|
379
389
|
function devAssets(base) {
|
package/dist/vite/layouts.js
CHANGED
|
@@ -33,7 +33,9 @@ export function layouts() {
|
|
|
33
33
|
if (!found)
|
|
34
34
|
return;
|
|
35
35
|
// added at the end (imports are hoisted) and within a line: the lines stay where they were (`map: null`)
|
|
36
|
-
const imports = chain
|
|
36
|
+
const imports = chain
|
|
37
|
+
.map((f, i) => `import __fondu_layout_${i} from ${JSON.stringify(f)};`)
|
|
38
|
+
.join('\n');
|
|
37
39
|
const set = `${found.name}.layout ??= [${chain.map((_, i) => `__fondu_layout_${i}`).join(', ')}];`;
|
|
38
40
|
return { code: `${found.code}\n${imports}\n${set}\n`, map: null };
|
|
39
41
|
},
|
package/dist/vite/prerender.js
CHANGED
|
@@ -19,7 +19,7 @@ export function ownScripts(html, policy) {
|
|
|
19
19
|
const nonce = policy?.match(/'nonce-([^']+)'/)?.[1];
|
|
20
20
|
if (!nonce)
|
|
21
21
|
return { html, hashes: [] };
|
|
22
|
-
const own = html.replace(/(<(?:script|link)\b[^>]*?)\snonce="([^"]*)"/gi, (tag, before, value) => value === nonce ? tag : before);
|
|
22
|
+
const own = html.replace(/(<(?:script|link)\b[^>]*?)\snonce="([^"]*)"/gi, (tag, before, value) => (value === nonce ? tag : before));
|
|
23
23
|
const hashes = new Set();
|
|
24
24
|
for (const [, attrs, code] of own.matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script>/gi))
|
|
25
25
|
if (attrs.includes(` nonce="${nonce}"`) && !/\ssrc=/i.test(attrs))
|
|
@@ -81,7 +81,10 @@ export async function prerender(start, clientDir, logger, headersFile) {
|
|
|
81
81
|
const errors = [];
|
|
82
82
|
for (const path of paths) {
|
|
83
83
|
const doc = await get(path, { Accept: 'text/html' });
|
|
84
|
-
const visit = await get(path, {
|
|
84
|
+
const visit = await get(path, {
|
|
85
|
+
'X-Inertia': 'true',
|
|
86
|
+
...(version && { 'X-Inertia-Version': version }),
|
|
87
|
+
});
|
|
85
88
|
if (doc.status !== 200 || visit.status !== 200) {
|
|
86
89
|
const to = doc.headers.get('location');
|
|
87
90
|
errors.push(`${path}: ${doc.status}${to ? ` → ${to}` : ''} (a prerendered page must answer 200 to anyone)`);
|
|
@@ -96,7 +99,10 @@ export async function prerender(start, clientDir, logger, headersFile) {
|
|
|
96
99
|
const { html, hashes } = ownScripts(await doc.text(), doc.headers.get('content-security-policy') ?? undefined);
|
|
97
100
|
const headers = Object.fromEntries([...doc.headers]
|
|
98
101
|
.filter(([name]) => SECURITY_HEADERS.test(name))
|
|
99
|
-
.map(([name, value]) => [
|
|
102
|
+
.map(([name, value]) => [
|
|
103
|
+
name,
|
|
104
|
+
/^content-security-policy/i.test(name) ? staticPolicy(value, hashes) : value,
|
|
105
|
+
]));
|
|
100
106
|
if (headersFile && security && JSON.stringify(headers) !== JSON.stringify(security)) {
|
|
101
107
|
errors.push(`${path}: its security headers differ from ${paths[0]}'s; one set is written for all`);
|
|
102
108
|
continue;
|