fondu 0.13.0 → 0.14.0
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 +1 -1
- package/dist/client/app.d.ts +36 -8
- package/dist/client/app.js +39 -16
- package/dist/client/form.d.ts +3 -1
- package/dist/client/form.js +10 -4
- package/dist/client/navigation.d.ts +4 -1
- package/dist/client/navigation.js +171 -27
- package/dist/client.d.ts +1 -1
- package/dist/client.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/paths.d.ts +7 -2
- package/dist/paths.js +13 -6
- package/dist/server/app.d.ts +7 -3
- package/dist/server/app.js +37 -23
- package/dist/server/document.d.ts +10 -2
- package/dist/server/document.js +69 -18
- package/dist/server/errors.js +1 -0
- package/dist/server/http.d.ts +2 -0
- package/dist/server/http.js +3 -0
- package/dist/server/prerender.js +7 -52
- package/dist/server/protocol.d.ts +16 -2
- package/dist/server/protocol.js +95 -15
- package/dist/server/routes.d.ts +3 -1
- package/dist/server/routes.js +6 -2
- package/dist/server/static.d.ts +14 -0
- package/dist/server/static.js +37 -0
- package/dist/virtual.d.ts +5 -1
- package/dist/vite/index.js +30 -21
- package/dist/vite/prerender.d.ts +11 -7
- package/dist/vite/prerender.js +13 -10
- package/dist/vite/strip.d.ts +1 -1
- package/dist/vite/strip.js +1 -1
- package/dist/vite/typegen.js +1 -1
- package/package.json +1 -1
package/dist/server/routes.d.ts
CHANGED
|
@@ -10,9 +10,11 @@ export interface RouteModule {
|
|
|
10
10
|
params?: Params;
|
|
11
11
|
/** a `_middleware.ts`: the middleware; a page: its component */
|
|
12
12
|
default?: Handlers;
|
|
13
|
+
/** a page's route file (`.tsx`): GET, POST, POST `?name` */
|
|
13
14
|
loader?: Handlers;
|
|
14
15
|
action?: Handlers;
|
|
15
16
|
actions?: Record<string, Handlers>;
|
|
17
|
+
/** a `.ts` route file: the HTTP methods */
|
|
16
18
|
GET?: Handlers;
|
|
17
19
|
POST?: Handlers;
|
|
18
20
|
PUT?: Handlers;
|
|
@@ -34,6 +36,6 @@ export interface Route {
|
|
|
34
36
|
* by segment, static before a param with listed values, before any param, before a catch-all. Throws, at startup,
|
|
35
37
|
* on what would answer the wrong way: two files for the same URLs (one would never answer, or answer without the
|
|
36
38
|
* other's middleware), `params` that no route takes or that aren't single segments, an error page other than the
|
|
37
|
-
* app's.
|
|
39
|
+
* app's, a `.jsx` file that would be a route, a layout or the error page (route files are TypeScript).
|
|
38
40
|
*/
|
|
39
41
|
export declare function routeTable(modules: Record<string, RouteModule>, append: boolean): Route[];
|
package/dist/server/routes.js
CHANGED
|
@@ -6,11 +6,15 @@ const SEGMENT = /^[\w.~-]+$/;
|
|
|
6
6
|
* by segment, static before a param with listed values, before any param, before a catch-all. Throws, at startup,
|
|
7
7
|
* on what would answer the wrong way: two files for the same URLs (one would never answer, or answer without the
|
|
8
8
|
* other's middleware), `params` that no route takes or that aren't single segments, an error page other than the
|
|
9
|
-
* app's.
|
|
9
|
+
* app's, a `.jsx` file that would be a route, a layout or the error page (route files are TypeScript).
|
|
10
10
|
*/
|
|
11
11
|
export function routeTable(modules, append) {
|
|
12
12
|
const files = Object.keys(modules);
|
|
13
|
-
const
|
|
13
|
+
const jsx = files.find((f) => f.endsWith('.jsx') &&
|
|
14
|
+
(isRouteFile(f.replace(/\.jsx$/, '.tsx')) || /(?:^|\/)_(?:layout|error)\.jsx$/.test(f)));
|
|
15
|
+
if (jsx)
|
|
16
|
+
throw new Error(`fondu: routes/${jsx}: route files are TypeScript; rename it to ${jsx.replace(/\.jsx$/, '.tsx')}`);
|
|
17
|
+
const nested = files.find((f) => f.endsWith('/_error.tsx'));
|
|
14
18
|
if (nested)
|
|
15
19
|
throw new Error(`fondu: routes/${nested} is never shown: the error page is routes/_error.tsx, one for the app`);
|
|
16
20
|
const middlewareFiles = files.filter((f) => f.endsWith('/_middleware.ts'));
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MiddlewareHandler } from 'hono';
|
|
2
|
+
/** Hono's `serveStatic` of the runtime (`hono/bun`, `@hono/node-server/serve-static`), given by the built server's entry */
|
|
3
|
+
export type ServeStatic = (options: {
|
|
4
|
+
root: string;
|
|
5
|
+
rewriteRequestPath?: (path: string) => string;
|
|
6
|
+
}) => MiddlewareHandler;
|
|
7
|
+
/**
|
|
8
|
+
* Node / Bun: the prerendered pages as files, before the app: a document request (`Accept: text/html`, no `X-Fondu`)
|
|
9
|
+
* for `/docs/intro` is `docs/intro.html`, a page object (`/_fondu/pages/…`) its `.json`, each with the headers the
|
|
10
|
+
* build decided (`headers`: `dist/server/prerender.json`). No file, or no page request: the next handler (the app).
|
|
11
|
+
* A URL ending in `index` is never a document file (it would be its directory's). A path that still has an escaped
|
|
12
|
+
* reserved character (`%2F`…) after decoding is not served from a file: serveStatic refuses it, and the app answers.
|
|
13
|
+
*/
|
|
14
|
+
export declare function staticPages(serveStatic: ServeStatic, clientRoot: string, headers: Record<string, string>): MiddlewareHandler;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { endsInIndex, htmlFile, PAGE_DATA } from '../paths.js';
|
|
2
|
+
import { isPageRequest } from './http.js';
|
|
3
|
+
/** the headers on the response a file was served with (the runtimes differ in when `onFound` could set them) */
|
|
4
|
+
const withHeaders = (res, headers) => {
|
|
5
|
+
for (const [name, value] of Object.entries(headers))
|
|
6
|
+
res.headers.set(name, value);
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Node / Bun: the prerendered pages as files, before the app: a document request (`Accept: text/html`, no `X-Fondu`)
|
|
10
|
+
* for `/docs/intro` is `docs/intro.html`, a page object (`/_fondu/pages/…`) its `.json`, each with the headers the
|
|
11
|
+
* build decided (`headers`: `dist/server/prerender.json`). No file, or no page request: the next handler (the app).
|
|
12
|
+
* A URL ending in `index` is never a document file (it would be its directory's). A path that still has an escaped
|
|
13
|
+
* reserved character (`%2F`…) after decoding is not served from a file: serveStatic refuses it, and the app answers.
|
|
14
|
+
*/
|
|
15
|
+
export function staticPages(serveStatic, clientRoot, headers) {
|
|
16
|
+
const pageObject = serveStatic({ root: clientRoot });
|
|
17
|
+
const document = serveStatic({ root: clientRoot, rewriteRequestPath: htmlFile });
|
|
18
|
+
const serve = (middleware, own) => async (c, next) => {
|
|
19
|
+
const res = await middleware(c, next);
|
|
20
|
+
if (res instanceof Response)
|
|
21
|
+
withHeaders(res, { ...headers, ...own });
|
|
22
|
+
return res;
|
|
23
|
+
};
|
|
24
|
+
const pages = serve(pageObject, { 'Cache-Control': 'private, no-cache', 'X-Fondu': 'true' });
|
|
25
|
+
const documents = serve(document, { 'Cache-Control': 'private, no-cache' });
|
|
26
|
+
return (c, next) => {
|
|
27
|
+
if ((c.req.method !== 'GET' && c.req.method !== 'HEAD') || c.req.path.includes('\0'))
|
|
28
|
+
return next();
|
|
29
|
+
if (c.req.path.startsWith(`${PAGE_DATA}/`))
|
|
30
|
+
return pages(c, next);
|
|
31
|
+
if (isPageRequest(c) ||
|
|
32
|
+
!(c.req.header('accept') ?? '').includes('text/html') ||
|
|
33
|
+
endsInIndex(c.req.path))
|
|
34
|
+
return next();
|
|
35
|
+
return documents(c, next);
|
|
36
|
+
};
|
|
37
|
+
}
|
package/dist/virtual.d.ts
CHANGED
|
@@ -10,7 +10,11 @@ declare module 'virtual:fondu/assets' {
|
|
|
10
10
|
declare module 'virtual:fondu/ssr' {
|
|
11
11
|
/** the server render of a page (null: `fondu({ ssr: false })`) */
|
|
12
12
|
export const renderPage:
|
|
13
|
-
| ((
|
|
13
|
+
| ((
|
|
14
|
+
page: import('./client/navigation.js').Page,
|
|
15
|
+
options: { nonce?: string; onError?: (error: unknown) => void },
|
|
16
|
+
after?: import('react').ReactElement,
|
|
17
|
+
) => Promise<import('./client/app.js').Rendered>)
|
|
14
18
|
| null
|
|
15
19
|
/** React's static markup of a JSX document (`react-dom/server`'s `renderToStaticMarkup`) */
|
|
16
20
|
export function renderMarkup(element: import('react').ReactElement): string | Promise<string>
|
package/dist/vite/index.js
CHANGED
|
@@ -2,10 +2,10 @@ import { createHash } from 'node:crypto';
|
|
|
2
2
|
import { appendFileSync, existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
3
|
import { builtinModules, createRequire } from 'node:module';
|
|
4
4
|
import { dirname, join, relative, resolve, sep } from 'node:path';
|
|
5
|
-
import { pathToFileURL } from 'node:url';
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
6
6
|
import devServer, { defaultOptions as devServerDefaults } from '@hono/vite-dev-server';
|
|
7
7
|
import react, {} from '@vitejs/plugin-react';
|
|
8
|
-
import { parseAst, transformWithOxc, } from 'vite';
|
|
8
|
+
import { normalizePath, parseAst, transformWithOxc, } from 'vite';
|
|
9
9
|
import { isPageFile, layoutsOf, pageId } from '../paths.js';
|
|
10
10
|
import { hmr, PAGE_EXPORTS, PROPS_EVENT } from './hmr.js';
|
|
11
11
|
import { nodeServer, prerender, worker } from './prerender.js';
|
|
@@ -14,6 +14,7 @@ import { typegen, walk as walkFiles } from './typegen.js';
|
|
|
14
14
|
/** what fondu's server code imports from Hono (test/hono.test.ts keeps the list complete) */
|
|
15
15
|
export const HONO = [
|
|
16
16
|
'hono',
|
|
17
|
+
'hono/body-limit',
|
|
17
18
|
'hono/combine',
|
|
18
19
|
'hono/cookie',
|
|
19
20
|
'hono/csrf',
|
|
@@ -31,18 +32,20 @@ const V = {
|
|
|
31
32
|
ssr: 'virtual:fondu/ssr',
|
|
32
33
|
serverEntry: 'virtual:fondu/server-entry',
|
|
33
34
|
};
|
|
35
|
+
/** fondu's own `server/static.js`, imported by the built server's entry (by its path: it is not an export of the package) */
|
|
36
|
+
const staticModule = normalizePath(fileURLToPath(new URL('../server/static.js', import.meta.url)));
|
|
34
37
|
const resolved = (id) => '\0' + id;
|
|
35
38
|
/** the pages, for the app's entry: the route files of `app/routes/` that are React, but what isn't a route */
|
|
36
39
|
const PAGES_GLOB = [
|
|
37
|
-
'/app/routes/**/*.{tsx,
|
|
40
|
+
'/app/routes/**/*.{tsx,mdx}',
|
|
38
41
|
'!/app/routes/**/_*',
|
|
39
42
|
'!/app/routes/**/_*/**',
|
|
40
|
-
'!/app/routes/**/*.{test,spec}.
|
|
43
|
+
'!/app/routes/**/*.{test,spec}.tsx',
|
|
41
44
|
];
|
|
42
45
|
/** the error page (`_error.tsx`: a page, though its name says it is no URL) */
|
|
43
|
-
const ERROR_GLOB = '/app/routes/_error.
|
|
46
|
+
const ERROR_GLOB = '/app/routes/_error.tsx';
|
|
44
47
|
/** and the layouts (`_layout.tsx`): the client puts a page in those of its directories */
|
|
45
|
-
const LAYOUTS_GLOB = ['/app/routes/**/_layout.
|
|
48
|
+
const LAYOUTS_GLOB = ['/app/routes/**/_layout.tsx', '!/app/routes/**/_*/**'];
|
|
46
49
|
/**
|
|
47
50
|
* For the dependency scan: a page's route file as the browser gets it, its server code removed. The scan would find
|
|
48
51
|
* the server's dependencies otherwise, and bundle them for the browser.
|
|
@@ -51,13 +54,13 @@ function scanRoutes(root) {
|
|
|
51
54
|
return {
|
|
52
55
|
name: 'fondu:scan-routes',
|
|
53
56
|
load: {
|
|
54
|
-
filter: { id: /\/app\/routes\/.*\.
|
|
57
|
+
filter: { id: /\/app\/routes\/.*\.tsx$/ },
|
|
55
58
|
async handler(id) {
|
|
56
59
|
const file = relative(`${root()}/app/routes`, id).split(sep).join('/');
|
|
57
60
|
if (file.startsWith('..') || !isPageFile(file))
|
|
58
61
|
return;
|
|
59
62
|
const { code } = await transformWithOxc(readFileSync(id, 'utf8'), id, {
|
|
60
|
-
lang:
|
|
63
|
+
lang: 'tsx',
|
|
61
64
|
jsx: { runtime: 'automatic' },
|
|
62
65
|
});
|
|
63
66
|
return {
|
|
@@ -129,6 +132,8 @@ function dependencies(root = '') {
|
|
|
129
132
|
* server bundle wherever it runs from; hashed files never change), then the app. Node: @hono/node-server's serve()
|
|
130
133
|
* on `PORT` (else the option), saying where it listens, with a graceful shutdown on SIGTERM / SIGINT (10 s). Bun
|
|
131
134
|
* serves the default export itself and reads `PORT` (with the app's `websocket`, if it has one).
|
|
135
|
+
* With `prerender`, the pages the build wrote are served from their files first, with the headers it decided
|
|
136
|
+
* (`prerender.json`, next to this entry).
|
|
132
137
|
*/
|
|
133
138
|
function serverEntry(options) {
|
|
134
139
|
let root = process.cwd();
|
|
@@ -158,15 +163,17 @@ function serverEntry(options) {
|
|
|
158
163
|
`const clientRoot = fileURLToPath(new URL('../client', import.meta.url))`,
|
|
159
164
|
`const main = new Hono()`,
|
|
160
165
|
`main.use('/assets/*', async (c, next) => { await next(); if (c.res.ok) c.header('Cache-Control', 'public, max-age=31536000, immutable') })`,
|
|
161
|
-
|
|
162
|
-
//
|
|
166
|
+
// prerender: the pages the build wrote are files, with the headers it decided (prerender.json, written after
|
|
167
|
+
// this server was built: not there while the build prerenders, so the app renders them then)
|
|
163
168
|
...(options.prerender
|
|
164
169
|
? [
|
|
165
|
-
`import {
|
|
166
|
-
`import {
|
|
167
|
-
`
|
|
170
|
+
`import { existsSync, readFileSync } from 'node:fs'`,
|
|
171
|
+
`import { staticPages } from ${JSON.stringify(staticModule)}`,
|
|
172
|
+
`const prerendered = fileURLToPath(new URL('./prerender.json', import.meta.url))`,
|
|
173
|
+
`if (existsSync(prerendered)) main.use('*', staticPages(serveStatic, clientRoot, JSON.parse(readFileSync(prerendered, 'utf8')).headers))`,
|
|
168
174
|
]
|
|
169
175
|
: []),
|
|
176
|
+
...paths.map((p) => `main.use(${JSON.stringify(p)}, serveStatic({ root: clientRoot }))`),
|
|
170
177
|
`main.all('*', (c) => app.fetch(c.req.raw, c.env))`,
|
|
171
178
|
...(bun
|
|
172
179
|
? [
|
|
@@ -292,7 +299,9 @@ function core(options) {
|
|
|
292
299
|
const wrangler = resolve(config.root, env.config.build.outDir, 'wrangler.json');
|
|
293
300
|
if (options.prerender && existsSync(wrangler)) {
|
|
294
301
|
const clientDir = resolve(config.root, client.config.build.outDir);
|
|
295
|
-
await prerender(worker(config.root, wrangler), clientDir, config.logger,
|
|
302
|
+
await prerender(worker(config.root, wrangler), clientDir, config.logger, {
|
|
303
|
+
headersFile: true,
|
|
304
|
+
});
|
|
296
305
|
workerFirstForIndex(wrangler);
|
|
297
306
|
}
|
|
298
307
|
}
|
|
@@ -333,7 +342,7 @@ function core(options) {
|
|
|
333
342
|
await builder.build(builder.environments.client);
|
|
334
343
|
await builder.build(builder.environments.ssr);
|
|
335
344
|
if (options.prerender)
|
|
336
|
-
await prerender(nodeServer(config.root, target), resolve(config.root, 'dist/client'), config.logger,
|
|
345
|
+
await prerender(nodeServer(config.root, target), resolve(config.root, 'dist/client'), config.logger, { serverDir: resolve(config.root, 'dist/server') });
|
|
337
346
|
},
|
|
338
347
|
},
|
|
339
348
|
};
|
|
@@ -342,7 +351,7 @@ function core(options) {
|
|
|
342
351
|
// so the first page is not reloaded once they are optimized. The scan reads JavaScript: an MDX page is compiled
|
|
343
352
|
// for it by the app's @mdx-js/rollup; the browser's, a page's route file without its server code
|
|
344
353
|
configEnvironment(name, env) {
|
|
345
|
-
const pages = ['app/routes/**/*.{tsx,
|
|
354
|
+
const pages = ['app/routes/**/*.{tsx,mdx}'];
|
|
346
355
|
const mdx = { extensions: ['.mdx'], rolldownOptions: { plugins: [scanMdx(() => config)] } };
|
|
347
356
|
if (name === 'client')
|
|
348
357
|
return {
|
|
@@ -387,7 +396,7 @@ function core(options) {
|
|
|
387
396
|
const env = server.environments.client;
|
|
388
397
|
const css = [];
|
|
389
398
|
try {
|
|
390
|
-
const layouts = walkFiles(resolve(config.root, 'app/routes')).filter((f) => /(?:^|\/)_layout\.
|
|
399
|
+
const layouts = walkFiles(resolve(config.root, 'app/routes')).filter((f) => /(?:^|\/)_layout\.tsx$/.test(f) &&
|
|
391
400
|
!f
|
|
392
401
|
.split('/')
|
|
393
402
|
.slice(0, -1)
|
|
@@ -462,9 +471,9 @@ function core(options) {
|
|
|
462
471
|
? `export const renderPage = null\nexport const renderMarkup = async (el) => (await import('react-dom/server')).renderToStaticMarkup(el)`
|
|
463
472
|
: [
|
|
464
473
|
`import '${V.entry}'`,
|
|
465
|
-
`import {
|
|
474
|
+
`import { renderToReadableStream, renderToStaticMarkup } from 'react-dom/server'`,
|
|
466
475
|
`import { renderPage as render } from 'fondu/client'`,
|
|
467
|
-
`export const renderPage = (page) => render(page,
|
|
476
|
+
`export const renderPage = (page, options, after) => render(page, (el) => renderToReadableStream(el, options), after)`,
|
|
468
477
|
`export { renderToStaticMarkup as renderMarkup }`,
|
|
469
478
|
].join('\n');
|
|
470
479
|
if (id === resolved(V.assets))
|
|
@@ -548,11 +557,11 @@ function productionAssets(config, options) {
|
|
|
548
557
|
const pages = {};
|
|
549
558
|
for (const key of Object.keys(manifest)) {
|
|
550
559
|
const file = key.startsWith('app/routes/') ? key.slice('app/routes/'.length) : undefined;
|
|
551
|
-
if (!file || !(isPageFile(file) || /^_error\.
|
|
560
|
+
if (!file || !(isPageFile(file) || /^_error\.tsx$/.test(file)))
|
|
552
561
|
continue;
|
|
553
562
|
// the page's chunk, and its layouts' (loaded with it)
|
|
554
563
|
const around = layoutsOf(file)
|
|
555
|
-
.
|
|
564
|
+
.map((dir) => `app/routes/${dir ? `${dir}/` : ''}_layout.tsx`)
|
|
556
565
|
.filter((k) => k in manifest);
|
|
557
566
|
const all = [...around, key].map(tags);
|
|
558
567
|
pages[pageId(file)] = { css: all.flatMap((t) => t.css), js: all.flatMap((t) => t.js) };
|
package/dist/vite/prerender.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type { Logger } from 'vite';
|
|
2
2
|
/**
|
|
3
|
-
* A static document's own tags: those with the build's nonce (its Content Security Policy's). The
|
|
4
|
-
* document
|
|
5
|
-
*
|
|
6
|
-
* inline scripts.
|
|
3
|
+
* A static document's own tags: those with the build's nonce (its Content Security Policy's). The policy of a static
|
|
4
|
+
* document has no nonce: its inline scripts are allowed by their hashes instead, and only these tags may have had the
|
|
5
|
+
* nonce: a `nonce` the document has otherwise (in a page's markup) is dropped. Then the hashes of these inline scripts.
|
|
7
6
|
*/
|
|
8
7
|
export declare function ownScripts(html: string, policy: string | undefined): {
|
|
9
8
|
html: string;
|
|
@@ -28,10 +27,15 @@ interface Server {
|
|
|
28
27
|
/**
|
|
29
28
|
* After the build: the built server (the Worker in wrangler, or `node` / `bun dist/server/index.js`), started with
|
|
30
29
|
* `FONDU_PRERENDER` set, lists the pages to prerender (`/__fondu/prerender`); each is asked for as a document and as
|
|
31
|
-
* a navigation, and both are written to the client build
|
|
32
|
-
*
|
|
30
|
+
* a navigation, and both are written to the client build, with the security headers the build decided for them: one
|
|
31
|
+
* set, in `_headers` (Cloudflare serves the files before the Worker) or in `prerender.json` next to the built server
|
|
32
|
+
* (Node and Bun: its entry serves the files before the app).
|
|
33
33
|
*/
|
|
34
|
-
export declare function prerender(start: () => Promise<Server>, clientDir: string, logger: Logger,
|
|
34
|
+
export declare function prerender(start: () => Promise<Server>, clientDir: string, logger: Logger, where: {
|
|
35
|
+
headersFile: true;
|
|
36
|
+
} | {
|
|
37
|
+
serverDir: string;
|
|
38
|
+
}): Promise<undefined>;
|
|
35
39
|
/** Cloudflare: the built Worker, in the app's own wrangler (fondu does not depend on it) */
|
|
36
40
|
export declare const worker: (root: string, workerConfig: string) => () => Promise<Server>;
|
|
37
41
|
/** Node / Bun: `node dist/server/index.js` (or `bun`), on a free port, until it answers */
|
package/dist/vite/prerender.js
CHANGED
|
@@ -7,12 +7,11 @@ import { dirname, join, resolve } from 'node:path';
|
|
|
7
7
|
import { pathToFileURL } from 'node:url';
|
|
8
8
|
import { PAGE_DATA, staticFiles } from '../paths.js';
|
|
9
9
|
/** the response headers a static document keeps: the security headers (hono/secure-headers' and the like) */
|
|
10
|
-
const SECURITY_HEADERS = /^(content-security-policy(-report-only)?|strict-transport-security|referrer-policy|permissions-policy|x-content-type-options|x-frame-options|x-xss-protection|x-dns-prefetch-control|x-download-options|x-permitted-cross-domain-policies|origin-agent-cluster|cross-origin-(opener|embedder|resource)-policy)$/i;
|
|
10
|
+
const SECURITY_HEADERS = /^(content-security-policy(-report-only)?|strict-transport-security|referrer-policy|permissions-policy|reporting-endpoints|report-to|x-content-type-options|x-frame-options|x-xss-protection|x-dns-prefetch-control|x-download-options|x-permitted-cross-domain-policies|origin-agent-cluster|cross-origin-(opener|embedder|resource)-policy)$/i;
|
|
11
11
|
/**
|
|
12
|
-
* A static document's own tags: those with the build's nonce (its Content Security Policy's). The
|
|
13
|
-
* document
|
|
14
|
-
*
|
|
15
|
-
* inline scripts.
|
|
12
|
+
* A static document's own tags: those with the build's nonce (its Content Security Policy's). The policy of a static
|
|
13
|
+
* document has no nonce: its inline scripts are allowed by their hashes instead, and only these tags may have had the
|
|
14
|
+
* nonce: a `nonce` the document has otherwise (in a page's markup) is dropped. Then the hashes of these inline scripts.
|
|
16
15
|
*/
|
|
17
16
|
export function ownScripts(html, policy) {
|
|
18
17
|
const nonce = policy?.match(/'nonce-([^']+)'/)?.[1];
|
|
@@ -79,10 +78,11 @@ export function withHeaderRules(file, headers) {
|
|
|
79
78
|
/**
|
|
80
79
|
* After the build: the built server (the Worker in wrangler, or `node` / `bun dist/server/index.js`), started with
|
|
81
80
|
* `FONDU_PRERENDER` set, lists the pages to prerender (`/__fondu/prerender`); each is asked for as a document and as
|
|
82
|
-
* a navigation, and both are written to the client build
|
|
83
|
-
*
|
|
81
|
+
* a navigation, and both are written to the client build, with the security headers the build decided for them: one
|
|
82
|
+
* set, in `_headers` (Cloudflare serves the files before the Worker) or in `prerender.json` next to the built server
|
|
83
|
+
* (Node and Bun: its entry serves the files before the app).
|
|
84
84
|
*/
|
|
85
|
-
export async function prerender(start, clientDir, logger,
|
|
85
|
+
export async function prerender(start, clientDir, logger, where) {
|
|
86
86
|
const fail = (lines) => {
|
|
87
87
|
for (const line of lines)
|
|
88
88
|
logger.error(`fondu: ${line}`);
|
|
@@ -124,7 +124,7 @@ export async function prerender(start, clientDir, logger, headersFile) {
|
|
|
124
124
|
name,
|
|
125
125
|
/^content-security-policy/i.test(name) ? staticPolicy(value, hashes) : value,
|
|
126
126
|
]));
|
|
127
|
-
if (
|
|
127
|
+
if (security && JSON.stringify(headers) !== JSON.stringify(security)) {
|
|
128
128
|
errors.push(`${path}: its security headers differ from ${paths[0]}'s; one set is written for all`);
|
|
129
129
|
continue;
|
|
130
130
|
}
|
|
@@ -136,7 +136,10 @@ export async function prerender(start, clientDir, logger, headersFile) {
|
|
|
136
136
|
}
|
|
137
137
|
if (errors.length)
|
|
138
138
|
return fail(errors);
|
|
139
|
-
if (
|
|
139
|
+
if ('serverDir' in where) {
|
|
140
|
+
write(where.serverDir, 'prerender.json', JSON.stringify({ headers: security ?? {} }));
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
140
143
|
const file = join(clientDir, '_headers');
|
|
141
144
|
const own = existsSync(file) ? readFileSync(file, 'utf8') : '';
|
|
142
145
|
writeFileSync(file, withHeaderRules(own, security ?? {}));
|
package/dist/vite/strip.d.ts
CHANGED
|
@@ -14,6 +14,6 @@ type Node = {
|
|
|
14
14
|
export declare function stripServerCode(code: string, program: {
|
|
15
15
|
body: Node[];
|
|
16
16
|
}, file: string): string;
|
|
17
|
-
/** The browser's copy of every page's route file (`.tsx
|
|
17
|
+
/** The browser's copy of every page's route file (`.tsx` in `app/routes/`): its server code removed */
|
|
18
18
|
export declare function stripRoutes(): Plugin;
|
|
19
19
|
export {};
|
package/dist/vite/strip.js
CHANGED
|
@@ -272,7 +272,7 @@ function importOf(code, node, kept) {
|
|
|
272
272
|
].filter(Boolean);
|
|
273
273
|
return `import ${clauses.join(', ')} from ${source};`;
|
|
274
274
|
}
|
|
275
|
-
/** The browser's copy of every page's route file (`.tsx
|
|
275
|
+
/** The browser's copy of every page's route file (`.tsx` in `app/routes/`): its server code removed */
|
|
276
276
|
export function stripRoutes() {
|
|
277
277
|
let root = process.cwd();
|
|
278
278
|
return {
|
package/dist/vite/typegen.js
CHANGED
|
@@ -25,7 +25,7 @@ function generateTypes(root) {
|
|
|
25
25
|
declare module 'fondu' {
|
|
26
26
|
interface Register {
|
|
27
27
|
routes: {
|
|
28
|
-
${routes.flatMap((f) => patternsOf(f).map((p) => line(p, `./routes/${f.replace(/\.
|
|
28
|
+
${routes.flatMap((f) => patternsOf(f).map((p) => line(p, `./routes/${f.replace(/\.tsx?$/, '')}`))).join('\n')}
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
}
|