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.
@@ -1,13 +1,17 @@
1
1
  import { Component } from 'rasti';
2
2
  import App from './App.ts';
3
3
  {{#if ROUTER}}
4
- import type { AppProps } from './App.ts';
5
- import { getTitleForUrl } from './router-setup.ts';
4
+ import { route, getTitleForUrl, loadPages } from './router-setup.ts';
6
5
  {{#endif}}
7
6
  {{#if CSSFUN}}
8
7
  import { theme } from './theme.js';
9
8
  import { StyleSheet } from 'cssfun';
10
9
  {{#endif}}
10
+ {{#if ROUTER}}
11
+
12
+ // Every page is in memory before the first render.
13
+ await loadPages();
14
+ {{#endif}}
11
15
 
12
16
  /** The pieces `app.js` injects into the `index.html` template. */
13
17
  export interface RenderResult {
@@ -24,7 +28,7 @@ export interface RenderResult {
24
28
  * @param options - Per-request options.
25
29
  * @returns JSON safe to embed into an inline script.
26
30
  */
27
- function serializeOptions(options: AppProps): string {
31
+ function serializeOptions(options: { url: string }): string {
28
32
  return JSON.stringify(options).replace(
29
33
  /[<>&\u2028\u2029]/g,
30
34
  char => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`
@@ -36,16 +40,20 @@ function serializeOptions(options: AppProps): string {
36
40
  * Server-side render function.
37
41
  * @param {{#if ROUTER}}url{{#else}}_url{{#endif}} - Request URL
38
42
  */
39
- export function render({{#if ROUTER}}url{{#else}}_url{{#endif}}: string): RenderResult {
43
+ export {{#if ROUTER}}async {{#endif}}function render({{#if ROUTER}}url{{#else}}_url{{#endif}}: string): {{#if ROUTER}}Promise<RenderResult>{{#else}}RenderResult{{#endif}} {
44
+ {{#if ROUTER}}
45
+ // Rendering is synchronous, so it waits for routing to set the page.
46
+ const { model, router } = await route(url);
47
+ {{#endif}}
40
48
  // Hydration matches elements by uid, so every request numbers them from the start.
41
49
  Component.resetUid();
42
50
  {{#if ROUTER}}
43
- const options: AppProps = { url };
44
- const html = new App(options).toString();
51
+ const html = new App({ model, router }).toString();
52
+ const options = { url };
45
53
  // The client hydrates with the same options.
46
54
  const script = `<script>window.__APP_OPTIONS__=${serializeOptions(options)}</script>`;
47
55
  {{#if CSSFUN}}
48
- // <style> tags for every style sheet the app modules registered on import, theme included.
56
+ // <style> tags for every style sheet the app modules registered on import, theme and every page included.
49
57
  return { html, head : StyleSheet.toString() + script, title : getTitleForUrl(url), bodyClassName : theme.classes.root };
50
58
  {{#else}}
51
59
  return { html, head : script, title : getTitleForUrl(url) };