create-rasti 0.1.0-alpha.2 → 0.1.0-alpha.3
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/extras/micro-router/README.md +27 -5
- package/extras/micro-router/src/index.js +103 -28
- package/package.json +7 -7
- package/src/apply/router.js +2 -1
- package/src/versions.js +5 -5
- package/templates/AGENTS.md +6 -1
- package/templates/_base/App-cssfun.js +18 -22
- package/templates/_base/App-cssfun.ts +22 -26
- package/templates/_base/App-tailwind.js +18 -22
- package/templates/_base/App-tailwind.ts +22 -26
- package/templates/_base/App.js +18 -22
- package/templates/_base/App.ts +22 -26
- package/templates/_extras/router/router-setup.js +77 -9
- package/templates/_extras/router/router-setup.ts +74 -9
- package/templates/spa/src/main.js +8 -1
- package/templates/spa/src/main.ts +8 -1
- package/templates/ssr/app.js +3 -2
- package/templates/ssr/src/entry-client.js +9 -2
- package/templates/ssr/src/entry-client.ts +8 -4
- package/templates/ssr/src/entry-server.js +16 -5
- package/templates/ssr/src/entry-server.ts +15 -7
|
@@ -2,16 +2,13 @@ import { Component, Model } from 'rasti';
|
|
|
2
2
|
import Header from './components/Header.ts';
|
|
3
3
|
import Home from './components/Home.ts';
|
|
4
4
|
{{#if ROUTER}}
|
|
5
|
-
import About from './
|
|
6
|
-
import {
|
|
7
|
-
import type {
|
|
5
|
+
import { About } from './router-setup.ts';
|
|
6
|
+
import type { RouteModel } from './router-setup.ts';
|
|
7
|
+
import type { Router } from './lib/router.js';
|
|
8
8
|
{{#endif}}
|
|
9
9
|
|
|
10
10
|
interface AppAttributes {
|
|
11
11
|
count: number;
|
|
12
|
-
{{#if ROUTER}}
|
|
13
|
-
location: Location | null;
|
|
14
|
-
{{#endif}}
|
|
15
12
|
}
|
|
16
13
|
|
|
17
14
|
/** Application state. Mutating it re-renders `App`. */
|
|
@@ -19,36 +16,31 @@ class AppState extends Model<AppAttributes> {}
|
|
|
19
16
|
interface AppState extends AppAttributes {} // exposes the attributes on the model
|
|
20
17
|
|
|
21
18
|
{{#if ROUTER}}
|
|
22
|
-
/** Options `App` is created with
|
|
19
|
+
/** Options `App` is created with, along with the `model` from `route`. Rasti keeps the ones it doesn't reserve as props. */
|
|
23
20
|
export interface AppProps {
|
|
24
|
-
/**
|
|
25
|
-
|
|
21
|
+
/** Router that updates the model, bound to links and history once hydrated. */
|
|
22
|
+
router: Router;
|
|
26
23
|
}
|
|
27
24
|
|
|
28
25
|
{{#endif}}
|
|
29
26
|
/** Root application component. Owns the state and passes values and callbacks down as props. */
|
|
30
27
|
{{#if ROUTER}}
|
|
31
|
-
class App extends Component<AppProps, AppState> {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
onCreate(
|
|
36
|
-
this.state = new AppState({
|
|
37
|
-
this.router = createAppRouter(this.state);
|
|
38
|
-
this.router.navigate(options.url ?? '/', { addToHistory : false });
|
|
28
|
+
class App extends Component<AppProps, AppState, RouteModel> {
|
|
29
|
+
{{#else}}
|
|
30
|
+
class App extends Component<{}, AppState> {
|
|
31
|
+
{{#endif}}
|
|
32
|
+
onCreate() {
|
|
33
|
+
this.state = new AppState({ count : 0 });
|
|
39
34
|
}
|
|
35
|
+
{{#if ROUTER}}
|
|
40
36
|
|
|
41
37
|
onHydrate() {
|
|
38
|
+
const { router } = this.props;
|
|
42
39
|
this.destroyQueue.push(
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
router.delegateNavigation(this.el),
|
|
41
|
+
router.bindHistory()
|
|
45
42
|
);
|
|
46
43
|
}
|
|
47
|
-
{{#else}}
|
|
48
|
-
class App extends Component<{}, AppState> {
|
|
49
|
-
onCreate() {
|
|
50
|
-
this.state = new AppState({ count : 0 });
|
|
51
|
-
}
|
|
52
44
|
{{#endif}}
|
|
53
45
|
|
|
54
46
|
/** Created once per instance, so `Home` doesn't re-render for a new callback on every render. */
|
|
@@ -58,14 +50,18 @@ class App extends Component<{}, AppState> {
|
|
|
58
50
|
|
|
59
51
|
template() {
|
|
60
52
|
const state = this.state!;
|
|
53
|
+
{{#if ROUTER}}
|
|
54
|
+
// `location` is null for a URL no route matches: no page renders.
|
|
55
|
+
const { location } = this.model!;
|
|
56
|
+
{{#endif}}
|
|
61
57
|
|
|
62
58
|
return this.html`
|
|
63
59
|
<div class="font-sans flex min-h-dvh flex-col items-center gap-6 md:gap-10 max-w-[1000px] mx-auto p-5 md:p-12 box-border text-white">
|
|
64
60
|
<${Header} />
|
|
65
61
|
{{#if ROUTER}}
|
|
66
|
-
${
|
|
62
|
+
${location?.test('/') ?
|
|
67
63
|
this.html`<${Home} count="${state.count}" handleIncrement=${this.handleIncrement} />` :
|
|
68
|
-
this.html`<${About} />`}
|
|
64
|
+
location && this.html`<${About.Component} />`}
|
|
69
65
|
{{#else}}
|
|
70
66
|
<${Home} count="${state.count}" handleIncrement=${this.handleIncrement} />
|
|
71
67
|
{{#endif}}
|
package/templates/_base/App.js
CHANGED
|
@@ -2,53 +2,44 @@ import { Component, Model } from 'rasti';
|
|
|
2
2
|
import Header from './components/Header.js';
|
|
3
3
|
import Home from './components/Home.js';
|
|
4
4
|
{{#if ROUTER}}
|
|
5
|
-
import About from './
|
|
6
|
-
import { createAppRouter } from './router-setup.js';
|
|
5
|
+
import { About } from './router-setup.js';
|
|
7
6
|
{{#endif}}
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* @typedef {Object} AppState
|
|
11
10
|
* @property {number} count
|
|
12
|
-
{{#if ROUTER}}
|
|
13
|
-
* @property {import('./lib/router.js').Location | null} location
|
|
14
|
-
{{#endif}}
|
|
15
11
|
*/
|
|
16
12
|
{{#if ROUTER}}
|
|
17
13
|
|
|
18
14
|
/**
|
|
19
|
-
* Options `App` is created with
|
|
15
|
+
* Options `App` is created with, along with the `model` from `route`. Rasti keeps the ones it doesn't reserve as props.
|
|
20
16
|
* @typedef {Object} AppProps
|
|
21
|
-
* @property {
|
|
17
|
+
* @property {import('./lib/router.js').Router} router - Router that updates the model, bound to links and history once hydrated.
|
|
22
18
|
*/
|
|
23
19
|
{{#endif}}
|
|
24
20
|
|
|
25
21
|
/**
|
|
26
22
|
* Root application component. Owns the state and passes values and callbacks down as props.
|
|
27
23
|
{{#if ROUTER}}
|
|
28
|
-
*
|
|
24
|
+
* Renders the page at `model.location`, which `route` in `router-setup.js` sets.
|
|
25
|
+
* @extends {Component<AppProps, AppState, import('./router-setup.js').RouteState>}
|
|
29
26
|
{{#else}}
|
|
30
27
|
* @extends {Component<{}, AppState>}
|
|
31
28
|
{{#endif}}
|
|
32
29
|
*/
|
|
33
30
|
class App extends Component {
|
|
34
|
-
{
|
|
35
|
-
|
|
36
|
-
onCreate(options = {}) {
|
|
37
|
-
this.state = new Model({ location : null, count : 0 });
|
|
38
|
-
this.router = createAppRouter(this.state);
|
|
39
|
-
this.router.navigate(options.url ?? '/', { addToHistory : false });
|
|
31
|
+
onCreate() {
|
|
32
|
+
this.state = new Model({ count : 0 });
|
|
40
33
|
}
|
|
34
|
+
{{#if ROUTER}}
|
|
41
35
|
|
|
42
36
|
onHydrate() {
|
|
37
|
+
const { router } = this.props;
|
|
43
38
|
this.destroyQueue.push(
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
router.delegateNavigation(this.el),
|
|
40
|
+
router.bindHistory()
|
|
46
41
|
);
|
|
47
42
|
}
|
|
48
|
-
{{#else}}
|
|
49
|
-
onCreate() {
|
|
50
|
-
this.state = new Model({ count : 0 });
|
|
51
|
-
}
|
|
52
43
|
{{#endif}}
|
|
53
44
|
|
|
54
45
|
/** Created once per instance, so `Home` doesn't re-render for a new callback on every render. */
|
|
@@ -57,13 +48,18 @@ class App extends Component {
|
|
|
57
48
|
};
|
|
58
49
|
|
|
59
50
|
template() {
|
|
51
|
+
{{#if ROUTER}}
|
|
52
|
+
// `location` is null for a URL no route matches: no page renders.
|
|
53
|
+
const { location } = this.model;
|
|
54
|
+
|
|
55
|
+
{{#endif}}
|
|
60
56
|
return this.html`
|
|
61
57
|
<div class="app">
|
|
62
58
|
<${Header} />
|
|
63
59
|
{{#if ROUTER}}
|
|
64
|
-
${
|
|
60
|
+
${location?.test('/') ?
|
|
65
61
|
this.html`<${Home} count="${this.state.count}" handleIncrement=${this.handleIncrement} />` :
|
|
66
|
-
this.html`<${About} />`}
|
|
62
|
+
location && this.html`<${About.Component} />`}
|
|
67
63
|
{{#else}}
|
|
68
64
|
<${Home} count="${this.state.count}" handleIncrement=${this.handleIncrement} />
|
|
69
65
|
{{#endif}}
|
package/templates/_base/App.ts
CHANGED
|
@@ -2,16 +2,13 @@ import { Component, Model } from 'rasti';
|
|
|
2
2
|
import Header from './components/Header.ts';
|
|
3
3
|
import Home from './components/Home.ts';
|
|
4
4
|
{{#if ROUTER}}
|
|
5
|
-
import About from './
|
|
6
|
-
import {
|
|
7
|
-
import type {
|
|
5
|
+
import { About } from './router-setup.ts';
|
|
6
|
+
import type { RouteModel } from './router-setup.ts';
|
|
7
|
+
import type { Router } from './lib/router.js';
|
|
8
8
|
{{#endif}}
|
|
9
9
|
|
|
10
10
|
interface AppAttributes {
|
|
11
11
|
count: number;
|
|
12
|
-
{{#if ROUTER}}
|
|
13
|
-
location: Location | null;
|
|
14
|
-
{{#endif}}
|
|
15
12
|
}
|
|
16
13
|
|
|
17
14
|
/** Application state. Mutating it re-renders `App`. */
|
|
@@ -19,36 +16,31 @@ class AppState extends Model<AppAttributes> {}
|
|
|
19
16
|
interface AppState extends AppAttributes {} // exposes the attributes on the model
|
|
20
17
|
|
|
21
18
|
{{#if ROUTER}}
|
|
22
|
-
/** Options `App` is created with
|
|
19
|
+
/** Options `App` is created with, along with the `model` from `route`. Rasti keeps the ones it doesn't reserve as props. */
|
|
23
20
|
export interface AppProps {
|
|
24
|
-
/**
|
|
25
|
-
|
|
21
|
+
/** Router that updates the model, bound to links and history once hydrated. */
|
|
22
|
+
router: Router;
|
|
26
23
|
}
|
|
27
24
|
|
|
28
25
|
{{#endif}}
|
|
29
26
|
/** Root application component. Owns the state and passes values and callbacks down as props. */
|
|
30
27
|
{{#if ROUTER}}
|
|
31
|
-
class App extends Component<AppProps, AppState> {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
onCreate(
|
|
36
|
-
this.state = new AppState({
|
|
37
|
-
this.router = createAppRouter(this.state);
|
|
38
|
-
this.router.navigate(options.url ?? '/', { addToHistory : false });
|
|
28
|
+
class App extends Component<AppProps, AppState, RouteModel> {
|
|
29
|
+
{{#else}}
|
|
30
|
+
class App extends Component<{}, AppState> {
|
|
31
|
+
{{#endif}}
|
|
32
|
+
onCreate() {
|
|
33
|
+
this.state = new AppState({ count : 0 });
|
|
39
34
|
}
|
|
35
|
+
{{#if ROUTER}}
|
|
40
36
|
|
|
41
37
|
onHydrate() {
|
|
38
|
+
const { router } = this.props;
|
|
42
39
|
this.destroyQueue.push(
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
router.delegateNavigation(this.el),
|
|
41
|
+
router.bindHistory()
|
|
45
42
|
);
|
|
46
43
|
}
|
|
47
|
-
{{#else}}
|
|
48
|
-
class App extends Component<{}, AppState> {
|
|
49
|
-
onCreate() {
|
|
50
|
-
this.state = new AppState({ count : 0 });
|
|
51
|
-
}
|
|
52
44
|
{{#endif}}
|
|
53
45
|
|
|
54
46
|
/** Created once per instance, so `Home` doesn't re-render for a new callback on every render. */
|
|
@@ -58,14 +50,18 @@ class App extends Component<{}, AppState> {
|
|
|
58
50
|
|
|
59
51
|
template() {
|
|
60
52
|
const state = this.state!;
|
|
53
|
+
{{#if ROUTER}}
|
|
54
|
+
// `location` is null for a URL no route matches: no page renders.
|
|
55
|
+
const { location } = this.model!;
|
|
56
|
+
{{#endif}}
|
|
61
57
|
|
|
62
58
|
return this.html`
|
|
63
59
|
<div class="app">
|
|
64
60
|
<${Header} />
|
|
65
61
|
{{#if ROUTER}}
|
|
66
|
-
${
|
|
62
|
+
${location?.test('/') ?
|
|
67
63
|
this.html`<${Home} count="${state.count}" handleIncrement=${this.handleIncrement} />` :
|
|
68
|
-
this.html`<${About} />`}
|
|
64
|
+
location && this.html`<${About.Component} />`}
|
|
69
65
|
{{#else}}
|
|
70
66
|
<${Home} count="${state.count}" handleIncrement=${this.handleIncrement} />
|
|
71
67
|
{{#endif}}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Model } from 'rasti';
|
|
1
2
|
import createRouter from './lib/router.js';
|
|
2
3
|
|
|
3
4
|
const DEFAULT_TITLE = '{{NAME}}';
|
|
@@ -40,21 +41,88 @@ export function getTitleForUrl(url) {
|
|
|
40
41
|
return getTitleForPath(path);
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
{{#if SSR}}
|
|
43
45
|
/**
|
|
44
|
-
*
|
|
46
|
+
* Every page `lazy` created, for `loadPages`.
|
|
47
|
+
* @type {Array<{ load : () => Promise<unknown> }>}
|
|
48
|
+
*/
|
|
49
|
+
const lazyPages = [];
|
|
50
|
+
|
|
51
|
+
{{#endif}}
|
|
52
|
+
/**
|
|
53
|
+
* A page kept in a chunk of its own. `Component` is `null` until `load` imports it.
|
|
54
|
+
* @template T
|
|
55
|
+
* @param {() => Promise<{ default : T }>} importModule - Imports the page module, e.g. `() => import('./components/About.js')`.
|
|
56
|
+
* @returns {{ Component : T|null, load : () => Promise<T> }}
|
|
57
|
+
*/
|
|
58
|
+
const lazy = (importModule) => {
|
|
59
|
+
/** @type {{ Component : T|null, load : () => Promise<T> }} */
|
|
60
|
+
const page = {
|
|
61
|
+
Component : null,
|
|
62
|
+
async load() {
|
|
63
|
+
page.Component ??= (await importModule()).default;
|
|
64
|
+
return page.Component;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
{{#if SSR}}
|
|
68
|
+
lazyPages.push(page);
|
|
69
|
+
{{#endif}}
|
|
70
|
+
return page;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* About page, loaded by its route on the first visit.
|
|
75
|
+
*/
|
|
76
|
+
export const About = lazy(() => import('./components/About.js'));
|
|
77
|
+
{{#if SSR}}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Load every page kept in a chunk of its own. The server awaits it before its first render, so
|
|
81
|
+
* no request waits for a chunk{{#if CSSFUN}}, and every page embeds the style sheets of all of them,
|
|
82
|
+
* whatever pages earlier requests loaded: the browser finds them in the page and attaches none{{#endif}}.
|
|
83
|
+
* @returns {Promise<void>}
|
|
84
|
+
*/
|
|
85
|
+
export async function loadPages() {
|
|
86
|
+
await Promise.all(lazyPages.map((page) => page.load()));
|
|
87
|
+
}
|
|
88
|
+
{{#endif}}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Model the router updates and `App` renders.
|
|
92
|
+
* @typedef {Object} RouteState
|
|
93
|
+
* @property {import('./lib/router.js').Location|null} location - Location of the page shown, `null` for a URL no route matches.
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Route `url` before mounting `App`: create the model it renders and the router that
|
|
98
|
+
* updates it, and navigate to `url`, which loads the chunk of its page. Rendering is
|
|
99
|
+
* synchronous, so await it before rendering on the server or hydrating in the browser.
|
|
45
100
|
* Updates document.title on client when route changes.
|
|
46
|
-
* @param {
|
|
47
|
-
* @returns {import('./lib/router.js').Router}
|
|
101
|
+
* @param {string} url - Path and query.
|
|
102
|
+
* @returns {Promise<{ model: RouteState, router: import('./lib/router.js').Router }>} The `model` and `router` options for `App`.
|
|
48
103
|
*/
|
|
49
|
-
export function
|
|
50
|
-
/** @
|
|
51
|
-
|
|
104
|
+
export async function route(url) {
|
|
105
|
+
const model = /** @type {RouteState} */ (new Model({ location : null }));
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Action that shows a page, once its chunk (if any) is loaded.
|
|
109
|
+
* @param {{ load : () => Promise<*> }} [page] - Lazy page the route renders.
|
|
110
|
+
* @returns {(location: import('./lib/router.js').Location, context: import('./lib/router.js').ActionContext) => Promise<void>}
|
|
111
|
+
*/
|
|
112
|
+
const show = (page) => async (location, { signal }) => {
|
|
113
|
+
await page?.load();
|
|
114
|
+
// A newer navigation took over while the chunk loaded.
|
|
115
|
+
if (signal.aborted) return;
|
|
52
116
|
model.location = location;
|
|
53
117
|
if (typeof document !== 'undefined') document.title = getTitleForPath(location.path);
|
|
54
118
|
};
|
|
55
119
|
|
|
56
|
-
|
|
57
|
-
{ path : '/', action },
|
|
58
|
-
{ path : '{{ROUTE_ABOUT}}', action }
|
|
120
|
+
const router = createRouter([
|
|
121
|
+
{ path : '/', action : show() },
|
|
122
|
+
{ path : '{{ROUTE_ABOUT}}', action : show(About) }
|
|
59
123
|
]);
|
|
124
|
+
|
|
125
|
+
await router.navigate(url, { addToHistory : false });
|
|
126
|
+
|
|
127
|
+
return { model, router };
|
|
60
128
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { Model } from 'rasti';
|
|
1
2
|
import createRouter from './lib/router.js';
|
|
2
|
-
import type { Location, Router } from './lib/router.js';
|
|
3
|
+
import type { ActionContext, Location, Router } from './lib/router.js';
|
|
3
4
|
|
|
4
5
|
const DEFAULT_TITLE = '{{NAME}}';
|
|
5
6
|
|
|
@@ -33,20 +34,84 @@ export function getTitleForUrl(url: string): string {
|
|
|
33
34
|
return getTitleForPath(pathname);
|
|
34
35
|
}
|
|
35
36
|
|
|
37
|
+
/** A page kept in a chunk of its own. `Component` is `null` until `load` imports it. */
|
|
38
|
+
interface LazyPage<T> {
|
|
39
|
+
Component: T | null;
|
|
40
|
+
load(): Promise<T>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
{{#if SSR}}
|
|
44
|
+
/** Every page `lazy` created, for `loadPages`. */
|
|
45
|
+
const lazyPages: LazyPage<unknown>[] = [];
|
|
46
|
+
|
|
47
|
+
{{#endif}}
|
|
48
|
+
/**
|
|
49
|
+
* Wraps the import of a page module.
|
|
50
|
+
* @param importModule - Imports the page module, e.g. `() => import('./components/About.ts')`.
|
|
51
|
+
*/
|
|
52
|
+
const lazy = <T>(importModule: () => Promise<{ default: T }>): LazyPage<T> => {
|
|
53
|
+
const page: LazyPage<T> = {
|
|
54
|
+
Component : null,
|
|
55
|
+
async load() {
|
|
56
|
+
page.Component ??= (await importModule()).default;
|
|
57
|
+
return page.Component;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
{{#if SSR}}
|
|
61
|
+
lazyPages.push(page);
|
|
62
|
+
{{#endif}}
|
|
63
|
+
return page;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** About page, loaded by its route on the first visit. */
|
|
67
|
+
export const About = lazy(() => import('./components/About.ts'));
|
|
68
|
+
{{#if SSR}}
|
|
69
|
+
|
|
36
70
|
/**
|
|
37
|
-
*
|
|
71
|
+
* Load every page kept in a chunk of its own. The server awaits it before its first render, so
|
|
72
|
+
* no request waits for a chunk{{#if CSSFUN}}, and every page embeds the style sheets of all of them,
|
|
73
|
+
* whatever pages earlier requests loaded: the browser finds them in the page and attaches none{{#endif}}.
|
|
74
|
+
*/
|
|
75
|
+
export async function loadPages(): Promise<void> {
|
|
76
|
+
await Promise.all(lazyPages.map((page) => page.load()));
|
|
77
|
+
}
|
|
78
|
+
{{#endif}}
|
|
79
|
+
|
|
80
|
+
interface RouteAttributes {
|
|
81
|
+
/** Location of the page shown, `null` for a URL no route matches. */
|
|
82
|
+
location: Location | null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Model the router updates and `App` renders. */
|
|
86
|
+
export class RouteModel extends Model<RouteAttributes> {}
|
|
87
|
+
export interface RouteModel extends RouteAttributes {} // exposes the attributes on the model
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Route `url` before mounting `App`: create the model it renders and the router that
|
|
91
|
+
* updates it, and navigate to `url`, which loads the chunk of its page. Rendering is
|
|
92
|
+
* synchronous, so await it before rendering on the server or hydrating in the browser.
|
|
38
93
|
* Updates document.title on client when route changes.
|
|
39
|
-
* @param
|
|
40
|
-
* @returns
|
|
94
|
+
* @param url - Path and query.
|
|
95
|
+
* @returns The `model` and `router` options for `App`.
|
|
41
96
|
*/
|
|
42
|
-
export function
|
|
43
|
-
const
|
|
97
|
+
export async function route(url: string): Promise<{ model: RouteModel, router: Router }> {
|
|
98
|
+
const model = new RouteModel({ location : null });
|
|
99
|
+
|
|
100
|
+
/** Action that shows a page, once its chunk (if any) is loaded. */
|
|
101
|
+
const show = (page?: LazyPage<unknown>) => async (location: Location, { signal }: ActionContext) => {
|
|
102
|
+
await page?.load();
|
|
103
|
+
// A newer navigation took over while the chunk loaded.
|
|
104
|
+
if (signal.aborted) return;
|
|
44
105
|
model.location = location;
|
|
45
106
|
if (typeof document !== 'undefined') document.title = getTitleForPath(location.path);
|
|
46
107
|
};
|
|
47
108
|
|
|
48
|
-
|
|
49
|
-
{ path : '/', action },
|
|
50
|
-
{ path : '{{ROUTE_ABOUT}}', action }
|
|
109
|
+
const router = createRouter([
|
|
110
|
+
{ path : '/', action : show() },
|
|
111
|
+
{ path : '{{ROUTE_ABOUT}}', action : show(About) }
|
|
51
112
|
]);
|
|
113
|
+
|
|
114
|
+
await router.navigate(url, { addToHistory : false });
|
|
115
|
+
|
|
116
|
+
return { model, router };
|
|
52
117
|
}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import App from './App.js';
|
|
2
|
+
{{#if ROUTER}}
|
|
3
|
+
import { route } from './router-setup.js';
|
|
4
|
+
{{#endif}}
|
|
2
5
|
{{#if CSSFUN}}
|
|
3
6
|
import { theme } from './theme.js';
|
|
4
7
|
|
|
@@ -8,7 +11,11 @@ import './style.css';
|
|
|
8
11
|
{{#endif}}
|
|
9
12
|
|
|
10
13
|
{{#if ROUTER}}
|
|
11
|
-
|
|
14
|
+
// Routing loads the chunk of the page, which `App` renders on mount.
|
|
15
|
+
route(window.location.pathname + window.location.search).then(
|
|
16
|
+
({ model, router }) => App.mount({ model, router }, document.querySelector('#app')),
|
|
17
|
+
(error) => console.error('Could not load the page', error)
|
|
18
|
+
);
|
|
12
19
|
{{#else}}
|
|
13
20
|
App.mount({}, document.querySelector('#app'));
|
|
14
21
|
{{#endif}}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import App from './App.ts';
|
|
2
|
+
{{#if ROUTER}}
|
|
3
|
+
import { route } from './router-setup.ts';
|
|
4
|
+
{{#endif}}
|
|
2
5
|
{{#if CSSFUN}}
|
|
3
6
|
import { theme } from './theme.js';
|
|
4
7
|
|
|
@@ -8,7 +11,11 @@ import './style.css';
|
|
|
8
11
|
{{#endif}}
|
|
9
12
|
|
|
10
13
|
{{#if ROUTER}}
|
|
11
|
-
|
|
14
|
+
// Routing loads the chunk of the page, which `App` renders on mount.
|
|
15
|
+
route(window.location.pathname + window.location.search).then(
|
|
16
|
+
({ model, router }) => App.mount({ model, router }, document.querySelector('#app')!),
|
|
17
|
+
(error) => console.error('Could not load the page', error)
|
|
18
|
+
);
|
|
12
19
|
{{#else}}
|
|
13
20
|
App.mount({}, document.querySelector('#app')!);
|
|
14
21
|
{{#endif}}
|
package/templates/ssr/app.js
CHANGED
|
@@ -41,8 +41,9 @@ app.use('*all', async (req, res) => {
|
|
|
41
41
|
render = (await vite.ssrLoadModule('/src/entry-server.{{SRC_EXT}}')).render;
|
|
42
42
|
} else {
|
|
43
43
|
template = templateHtml;
|
|
44
|
-
//
|
|
45
|
-
|
|
44
|
+
// Built by `vite build --ssr`. A path in a variable keeps `tsc` from type-checking the bundle.
|
|
45
|
+
const serverEntry = './dist/server/entry-server.js';
|
|
46
|
+
render = (await import(serverEntry)).render;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
const rendered = await render(url);
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import App from './App.js';
|
|
2
|
+
{{#if ROUTER}}
|
|
3
|
+
import { route } from './router-setup.js';
|
|
4
|
+
{{#endif}}
|
|
2
5
|
{{#if CSSFUN}}
|
|
3
6
|
import { theme } from './theme.js';
|
|
4
7
|
|
|
@@ -9,8 +12,12 @@ import './style.css';
|
|
|
9
12
|
|
|
10
13
|
// The third argument `true` hydrates the server-rendered HTML instead of rendering it again.
|
|
11
14
|
{{#if ROUTER}}
|
|
12
|
-
// The server writes the options it rendered with, so the client
|
|
13
|
-
|
|
15
|
+
// The server writes the options it rendered with, so the client routes the same URL. Routing
|
|
16
|
+
// loads the chunk of the page, which hydration needs to render the markup the server sent.
|
|
17
|
+
route(window.__APP_OPTIONS__.url).then(
|
|
18
|
+
({ model, router }) => App.mount({ model, router }, document.querySelector('#app'), true),
|
|
19
|
+
(error) => console.error('Could not load the page to hydrate it', error)
|
|
20
|
+
);
|
|
14
21
|
{{#else}}
|
|
15
22
|
App.mount({}, document.querySelector('#app'), true);
|
|
16
23
|
{{#endif}}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import App from './App.ts';
|
|
2
2
|
{{#if ROUTER}}
|
|
3
|
-
import
|
|
3
|
+
import { route } from './router-setup.ts';
|
|
4
4
|
{{#endif}}
|
|
5
5
|
{{#if CSSFUN}}
|
|
6
6
|
import { theme } from './theme.js';
|
|
@@ -14,15 +14,19 @@ import './style.css';
|
|
|
14
14
|
declare global {
|
|
15
15
|
interface Window {
|
|
16
16
|
/** The options the server rendered `App` with, written into the page by `entry-server`. */
|
|
17
|
-
__APP_OPTIONS__:
|
|
17
|
+
__APP_OPTIONS__: { url: string };
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
{{#endif}}
|
|
21
21
|
|
|
22
22
|
// The third argument `true` hydrates the server-rendered HTML instead of rendering it again.
|
|
23
23
|
{{#if ROUTER}}
|
|
24
|
-
// The server writes the options it rendered with, so the client
|
|
25
|
-
|
|
24
|
+
// The server writes the options it rendered with, so the client routes the same URL. Routing
|
|
25
|
+
// loads the chunk of the page, which hydration needs to render the markup the server sent.
|
|
26
|
+
route(window.__APP_OPTIONS__.url).then(
|
|
27
|
+
({ model, router }) => App.mount({ model, router }, document.querySelector('#app')!, true),
|
|
28
|
+
(error) => console.error('Could not load the page to hydrate it', error)
|
|
29
|
+
);
|
|
26
30
|
{{#else}}
|
|
27
31
|
App.mount({}, document.querySelector('#app')!, true);
|
|
28
32
|
{{#endif}}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Component } from 'rasti';
|
|
2
2
|
import App from './App.js';
|
|
3
3
|
{{#if ROUTER}}
|
|
4
|
-
import { getTitleForUrl } from './router-setup.js';
|
|
4
|
+
import { route, getTitleForUrl, loadPages } from './router-setup.js';
|
|
5
5
|
{{#endif}}
|
|
6
6
|
{{#if CSSFUN}}
|
|
7
7
|
import { theme } from './theme.js';
|
|
@@ -9,10 +9,13 @@ import { StyleSheet } from 'cssfun';
|
|
|
9
9
|
{{#endif}}
|
|
10
10
|
|
|
11
11
|
{{#if ROUTER}}
|
|
12
|
+
// Every page is in memory before the first render.
|
|
13
|
+
await loadPages();
|
|
14
|
+
|
|
12
15
|
/**
|
|
13
16
|
* Serializes the render options for an inline `<script>`, as unicode escapes for the
|
|
14
17
|
* characters that could close the tag (`</script>`) or break older JavaScript parsers.
|
|
15
|
-
* @param {
|
|
18
|
+
* @param {{ url: string }} options - Per-request options.
|
|
16
19
|
* @returns {string} JSON safe to embed into an inline script.
|
|
17
20
|
*/
|
|
18
21
|
function serializeOptions(options) {
|
|
@@ -27,18 +30,26 @@ function serializeOptions(options) {
|
|
|
27
30
|
* Server-side render function.
|
|
28
31
|
* Returns the pieces `app.js` injects into the `index.html` template.
|
|
29
32
|
* @param {string} {{#if ROUTER}}url{{#else}}_url{{#endif}} - Request URL
|
|
33
|
+
{{#if ROUTER}}
|
|
34
|
+
* @returns {Promise<{ html: string, head?: string, title?: string, bodyClassName?: string }>}
|
|
35
|
+
{{#else}}
|
|
30
36
|
* @returns {{ html: string, head?: string, title?: string, bodyClassName?: string }}
|
|
37
|
+
{{#endif}}
|
|
31
38
|
*/
|
|
32
|
-
export function render({{#if ROUTER}}url{{#else}}_url{{#endif}}) {
|
|
39
|
+
export {{#if ROUTER}}async {{#endif}}function render({{#if ROUTER}}url{{#else}}_url{{#endif}}) {
|
|
40
|
+
{{#if ROUTER}}
|
|
41
|
+
// Rendering is synchronous, so it waits for routing to set the page.
|
|
42
|
+
const { model, router } = await route(url);
|
|
43
|
+
{{#endif}}
|
|
33
44
|
// Hydration matches elements by uid, so every request numbers them from the start.
|
|
34
45
|
Component.resetUid();
|
|
35
46
|
{{#if ROUTER}}
|
|
47
|
+
const html = new App({ model, router }).toString();
|
|
36
48
|
const options = { url };
|
|
37
|
-
const html = new App(options).toString();
|
|
38
49
|
// The client hydrates with the same options.
|
|
39
50
|
const script = `<script>window.__APP_OPTIONS__=${serializeOptions(options)}</script>`;
|
|
40
51
|
{{#if CSSFUN}}
|
|
41
|
-
// <style> tags for every style sheet the app modules registered on import, theme included.
|
|
52
|
+
// <style> tags for every style sheet the app modules registered on import, theme and every page included.
|
|
42
53
|
return { html, head : StyleSheet.toString() + script, title : getTitleForUrl(url), bodyClassName : theme.classes.root };
|
|
43
54
|
{{#else}}
|
|
44
55
|
return { html, head : script, title : getTitleForUrl(url) };
|