create-rasti 0.1.0-alpha.2 → 0.1.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.
@@ -3,8 +3,7 @@ import { css } from 'cssfun';
3
3
  import Header from './components/Header.js';
4
4
  import Home from './components/Home.js';
5
5
  {{#if ROUTER}}
6
- import About from './components/About.js';
7
- import { createAppRouter } from './router-setup.js';
6
+ import { About } from './router-setup.js';
8
7
  {{#endif}}
9
8
 
10
9
  const { classes } = css({
@@ -39,46 +38,38 @@ const { classes } = css({
39
38
  /**
40
39
  * @typedef {Object} AppState
41
40
  * @property {number} count
42
- {{#if ROUTER}}
43
- * @property {import('./lib/router.js').Location | null} location
44
- {{#endif}}
45
41
  */
46
42
  {{#if ROUTER}}
47
43
 
48
44
  /**
49
- * Options `App` is created with. Rasti keeps the ones it doesn't reserve as props.
45
+ * Options `App` is created with, along with the `model` from `route`. Rasti keeps the ones it doesn't reserve as props.
50
46
  * @typedef {Object} AppProps
51
- * @property {string} [url] - URL to route on creation (the request URL on the server).
47
+ * @property {import('./lib/router.js').Router} router - Router that updates the model, bound to links and history once hydrated.
52
48
  */
53
49
  {{#endif}}
54
50
 
55
51
  /**
56
52
  * Root application component with the global styles. Owns the state and passes values and callbacks down as props.
57
53
  {{#if ROUTER}}
58
- * @extends {Component<AppProps, AppState>}
54
+ * Renders the page at `model.location`, which `route` in `router-setup.js` sets.
55
+ * @extends {Component<AppProps, AppState, import('./router-setup.js').RouteState>}
59
56
  {{#else}}
60
57
  * @extends {Component<{}, AppState>}
61
58
  {{#endif}}
62
59
  */
63
60
  class App extends Component {
64
- {{#if ROUTER}}
65
- /** @param {AppProps} [options] */
66
- onCreate(options = {}) {
67
- this.state = new Model({ location : null, count : 0 });
68
- this.router = createAppRouter(this.state);
69
- this.router.navigate(options.url ?? '/', { addToHistory : false });
61
+ onCreate() {
62
+ this.state = new Model({ count : 0 });
70
63
  }
64
+ {{#if ROUTER}}
71
65
 
72
66
  onHydrate() {
67
+ const { router } = this.props;
73
68
  this.destroyQueue.push(
74
- this.router.delegateNavigation(this.el),
75
- this.router.bindHistory()
69
+ router.delegateNavigation(this.el),
70
+ router.bindHistory()
76
71
  );
77
72
  }
78
- {{#else}}
79
- onCreate() {
80
- this.state = new Model({ count : 0 });
81
- }
82
73
  {{#endif}}
83
74
 
84
75
  /** Created once per instance, so `Home` doesn't re-render for a new callback on every render. */
@@ -87,13 +78,18 @@ class App extends Component {
87
78
  };
88
79
 
89
80
  template() {
81
+ {{#if ROUTER}}
82
+ // `location` is null for a URL no route matches: no page renders.
83
+ const { location } = this.model;
84
+
85
+ {{#endif}}
90
86
  return this.html`
91
87
  <div class="${classes.root}">
92
88
  <${Header} />
93
89
  {{#if ROUTER}}
94
- ${this.state.location?.test('/') ?
90
+ ${location?.test('/') ?
95
91
  this.html`<${Home} count="${this.state.count}" handleIncrement=${this.handleIncrement} />` :
96
- this.html`<${About} />`}
92
+ location && this.html`<${About.Component} />`}
97
93
  {{#else}}
98
94
  <${Home} count="${this.state.count}" handleIncrement=${this.handleIncrement} />
99
95
  {{#endif}}
@@ -3,9 +3,9 @@ import { css } from 'cssfun';
3
3
  import Header from './components/Header.ts';
4
4
  import Home from './components/Home.ts';
5
5
  {{#if ROUTER}}
6
- import About from './components/About.ts';
7
- import { createAppRouter } from './router-setup.ts';
8
- import type { Location, Router } from './lib/router.js';
6
+ import { About } from './router-setup.ts';
7
+ import type { RouteModel } from './router-setup.ts';
8
+ import type { Router } from './lib/router.js';
9
9
  {{#endif}}
10
10
 
11
11
  const { classes } = css({
@@ -39,9 +39,6 @@ const { classes } = css({
39
39
 
40
40
  interface AppAttributes {
41
41
  count: number;
42
- {{#if ROUTER}}
43
- location: Location | null;
44
- {{#endif}}
45
42
  }
46
43
 
47
44
  /** Application state. Mutating it re-renders `App`. */
@@ -49,36 +46,31 @@ class AppState extends Model<AppAttributes> {}
49
46
  interface AppState extends AppAttributes {} // exposes the attributes on the model
50
47
 
51
48
  {{#if ROUTER}}
52
- /** Options `App` is created with. Rasti keeps the ones it doesn't reserve as props. */
49
+ /** Options `App` is created with, along with the `model` from `route`. Rasti keeps the ones it doesn't reserve as props. */
53
50
  export interface AppProps {
54
- /** URL to route on creation (the request URL on the server). */
55
- url?: string;
51
+ /** Router that updates the model, bound to links and history once hydrated. */
52
+ router: Router;
56
53
  }
57
54
 
58
55
  {{#endif}}
59
56
  /** Root application component with the global styles. Owns the state and passes values and callbacks down as props. */
60
57
  {{#if ROUTER}}
61
- class App extends Component<AppProps, AppState> {
62
- /** Set in `onCreate`, which runs in the base constructor: `declare` keeps a field initializer from resetting it. */
63
- declare router: Router;
64
-
65
- onCreate(options: AppProps = {}) {
66
- this.state = new AppState({ location : null, count : 0 });
67
- this.router = createAppRouter(this.state);
68
- this.router.navigate(options.url ?? '/', { addToHistory : false });
58
+ class App extends Component<AppProps, AppState, RouteModel> {
59
+ {{#else}}
60
+ class App extends Component<{}, AppState> {
61
+ {{#endif}}
62
+ onCreate() {
63
+ this.state = new AppState({ count : 0 });
69
64
  }
65
+ {{#if ROUTER}}
70
66
 
71
67
  onHydrate() {
68
+ const { router } = this.props;
72
69
  this.destroyQueue.push(
73
- this.router.delegateNavigation(this.el),
74
- this.router.bindHistory()
70
+ router.delegateNavigation(this.el),
71
+ router.bindHistory()
75
72
  );
76
73
  }
77
- {{#else}}
78
- class App extends Component<{}, AppState> {
79
- onCreate() {
80
- this.state = new AppState({ count : 0 });
81
- }
82
74
  {{#endif}}
83
75
 
84
76
  /** Created once per instance, so `Home` doesn't re-render for a new callback on every render. */
@@ -88,14 +80,18 @@ class App extends Component<{}, AppState> {
88
80
 
89
81
  template() {
90
82
  const state = this.state!;
83
+ {{#if ROUTER}}
84
+ // `location` is null for a URL no route matches: no page renders.
85
+ const { location } = this.model!;
86
+ {{#endif}}
91
87
 
92
88
  return this.html`
93
89
  <div class="${classes.root}">
94
90
  <${Header} />
95
91
  {{#if ROUTER}}
96
- ${state.location?.test('/') ?
92
+ ${location?.test('/') ?
97
93
  this.html`<${Home} count="${state.count}" handleIncrement=${this.handleIncrement} />` :
98
- this.html`<${About} />`}
94
+ location && this.html`<${About.Component} />`}
99
95
  {{#else}}
100
96
  <${Home} count="${state.count}" handleIncrement=${this.handleIncrement} />
101
97
  {{#endif}}
@@ -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 './components/About.js';
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. Rasti keeps the ones it doesn't reserve as props.
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 {string} [url] - URL to route on creation (the request URL on the server).
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
- * @extends {Component<AppProps, AppState>}
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
- {{#if ROUTER}}
35
- /** @param {AppProps} [options] */
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
- this.router.delegateNavigation(this.el),
45
- this.router.bindHistory()
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="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">
62
58
  <${Header} />
63
59
  {{#if ROUTER}}
64
- ${this.state.location?.test('/') ?
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}}
@@ -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 './components/About.ts';
6
- import { createAppRouter } from './router-setup.ts';
7
- import type { Location, Router } from './lib/router.js';
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. Rasti keeps the ones it doesn't reserve as props. */
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
- /** URL to route on creation (the request URL on the server). */
25
- url?: string;
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
- /** Set in `onCreate`, which runs in the base constructor: `declare` keeps a field initializer from resetting it. */
33
- declare router: Router;
34
-
35
- onCreate(options: AppProps = {}) {
36
- this.state = new AppState({ location : null, count : 0 });
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
- this.router.delegateNavigation(this.el),
44
- this.router.bindHistory()
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
- ${state.location?.test('/') ?
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}}
@@ -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 './components/About.js';
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. Rasti keeps the ones it doesn't reserve as props.
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 {string} [url] - URL to route on creation (the request URL on the server).
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
- * @extends {Component<AppProps, AppState>}
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
- {{#if ROUTER}}
35
- /** @param {AppProps} [options] */
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
- this.router.delegateNavigation(this.el),
45
- this.router.bindHistory()
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
- ${this.state.location?.test('/') ?
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}}
@@ -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 './components/About.ts';
6
- import { createAppRouter } from './router-setup.ts';
7
- import type { Location, Router } from './lib/router.js';
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. Rasti keeps the ones it doesn't reserve as props. */
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
- /** URL to route on creation (the request URL on the server). */
25
- url?: string;
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
- /** Set in `onCreate`, which runs in the base constructor: `declare` keeps a field initializer from resetting it. */
33
- declare router: Router;
34
-
35
- onCreate(options: AppProps = {}) {
36
- this.state = new AppState({ location : null, count : 0 });
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
- this.router.delegateNavigation(this.el),
44
- this.router.bindHistory()
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
- ${state.location?.test('/') ?
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
- * Create router bound to model (sets model.location on navigate).
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 {{ location: (import('./lib/router.js').Location|null) }} model - Model with location
47
- * @returns {import('./lib/router.js').Router} Router with navigate, delegateNavigation, bindHistory, createUrl
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 createAppRouter(model) {
50
- /** @param {import('./lib/router.js').Location} location */
51
- const action = (location) => {
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
- return createRouter([
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
  }