@tempots/server 0.0.1
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/LICENSE.txt +202 -0
- package/README.md +163 -0
- package/index.cjs +1 -0
- package/index.d.ts +206 -0
- package/index.js +139 -0
- package/package.json +41 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# @tempots/server
|
|
2
|
+
|
|
3
|
+
Server-side rendering utilities for Tempo applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tempots/server
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @tempots/server
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- **Streaming SSR**: Stream HTML to the client for faster Time To First Byte (TTFB)
|
|
16
|
+
- **String rendering**: Generate complete HTML strings for simpler use cases
|
|
17
|
+
- **Static markup**: Generate HTML without hydration markers for emails, PDFs, etc.
|
|
18
|
+
- **Full Tempo compatibility**: Works with all Tempo renderables, signals, and providers
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Quick Start with `createRenderer()`
|
|
23
|
+
|
|
24
|
+
The simplest way to set up your server-side rendering entry point:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
// entry-server.ts
|
|
28
|
+
import { createRenderer } from '@tempots/server'
|
|
29
|
+
import { App } from './app'
|
|
30
|
+
|
|
31
|
+
export const { render, renderStream } = createRenderer(App, {
|
|
32
|
+
getData: (url) => ({
|
|
33
|
+
timestamp: new Date().toISOString(),
|
|
34
|
+
path: new URL(url, 'https://example.com').pathname,
|
|
35
|
+
}),
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
export { App }
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
This creates standard `render()` and `renderStream()` functions ready for use with your Express/Vite server.
|
|
42
|
+
|
|
43
|
+
### Streaming (Recommended for large pages)
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { renderToStream } from '@tempots/server'
|
|
47
|
+
import { html } from '@tempots/dom'
|
|
48
|
+
import express from 'express'
|
|
49
|
+
|
|
50
|
+
const App = () => html.div(
|
|
51
|
+
html.h1('Hello, World!'),
|
|
52
|
+
html.p('This is streamed from the server.')
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
const app = express()
|
|
56
|
+
|
|
57
|
+
app.get('/', (req, res) => {
|
|
58
|
+
res.setHeader('Content-Type', 'text/html')
|
|
59
|
+
const stream = renderToStream(App(), {
|
|
60
|
+
url: req.url,
|
|
61
|
+
onShellReady: () => {
|
|
62
|
+
// Shell is ready, headers can be sent
|
|
63
|
+
},
|
|
64
|
+
onAllReady: () => {
|
|
65
|
+
// All content has been rendered
|
|
66
|
+
},
|
|
67
|
+
onError: (error) => {
|
|
68
|
+
console.error('Render error:', error)
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
stream.pipe(res)
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### String Rendering
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { renderToString } from '@tempots/server'
|
|
79
|
+
import { html } from '@tempots/dom'
|
|
80
|
+
|
|
81
|
+
const App = () => html.div(
|
|
82
|
+
html.h1('Hello, World!'),
|
|
83
|
+
html.p('This is rendered on the server.')
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
const htmlString = await renderToString(App())
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Static Markup (No hydration)
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { renderToStaticMarkup } from '@tempots/server'
|
|
93
|
+
import { html } from '@tempots/dom'
|
|
94
|
+
|
|
95
|
+
const EmailTemplate = () => html.div(
|
|
96
|
+
html.h1('Welcome!'),
|
|
97
|
+
html.p('Thank you for signing up.')
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
const markup = await renderToStaticMarkup(EmailTemplate())
|
|
101
|
+
// Use for emails, PDFs, etc.
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## API
|
|
105
|
+
|
|
106
|
+
### `createRenderer(App, options?)`
|
|
107
|
+
|
|
108
|
+
High-level function to create standard render functions for SSR entry points.
|
|
109
|
+
|
|
110
|
+
**Parameters:**
|
|
111
|
+
- `App: (options: O) => Renderable` - The app component factory
|
|
112
|
+
|
|
113
|
+
**Options:**
|
|
114
|
+
- `getData?: (url: string) => O | Promise<O>` - Function to get initial data for each request
|
|
115
|
+
- `hydrate?: boolean` - Generate hydration placeholders (default: `true`)
|
|
116
|
+
- `selector?: string` - Root element selector (default: `"body"`)
|
|
117
|
+
- `providers?: Providers` - Providers to inject during rendering
|
|
118
|
+
|
|
119
|
+
**Returns:** `{ render, renderStream }`
|
|
120
|
+
- `render(url: string): Promise<string>` - Renders to HTML string
|
|
121
|
+
- `renderStream(url: string): Readable` - Renders to stream
|
|
122
|
+
|
|
123
|
+
**Example:**
|
|
124
|
+
```typescript
|
|
125
|
+
export const { render, renderStream } = createRenderer(App, {
|
|
126
|
+
getData: (url) => ({ timestamp: Date.now() }),
|
|
127
|
+
hydrate: true,
|
|
128
|
+
})
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### `renderToStream(renderable, options?)`
|
|
132
|
+
|
|
133
|
+
Renders a Renderable to a Node.js Readable stream.
|
|
134
|
+
|
|
135
|
+
**Options:**
|
|
136
|
+
- `url?: string` - Initial URL for routing (default: 'https://example.com')
|
|
137
|
+
- `selector?: string` - Root element selector (default: 'body')
|
|
138
|
+
- `generatePlaceholders?: boolean` - Include hydration markers (default: false)
|
|
139
|
+
- `providers?: Providers` - Providers to inject during rendering
|
|
140
|
+
- `onShellReady?: () => void` - Called when shell content is ready
|
|
141
|
+
- `onAllReady?: () => void` - Called when all content is rendered
|
|
142
|
+
- `onError?: (error: Error) => void` - Called on render errors
|
|
143
|
+
|
|
144
|
+
### `renderToString(renderable, options?)`
|
|
145
|
+
|
|
146
|
+
Renders a Renderable to an HTML string.
|
|
147
|
+
|
|
148
|
+
**Options:**
|
|
149
|
+
- `url?: string` - Initial URL for routing
|
|
150
|
+
- `selector?: string` - Root element selector
|
|
151
|
+
- `generatePlaceholders?: boolean` - Include hydration markers
|
|
152
|
+
- `providers?: Providers` - Providers to inject during rendering
|
|
153
|
+
- `onError?: (error: Error) => void` - Called on render errors
|
|
154
|
+
|
|
155
|
+
### `renderToStaticMarkup(renderable, options?)`
|
|
156
|
+
|
|
157
|
+
Renders a Renderable to static HTML without hydration markers.
|
|
158
|
+
|
|
159
|
+
Same options as `renderToString` except `generatePlaceholders` is forced to `false`.
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
Apache-2.0
|
package/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("stream"),h=require("@tempots/dom");function g(e,t={}){const{url:n="https://example.com",selector:r="body",generatePlaceholders:s=!1,providers:u={},onShellReady:f,onAllReady:c,onError:o}=t,a={generatePlaceholders:s};let l=null,i=null;try{l=h.runHeadless(()=>e,{startUrl:n,selector:r,providers:u})}catch(d){i=d instanceof Error?d:new Error(String(d))}return new P.Readable({async read(){try{if(i)throw i;if(!l)throw new Error("Render result is null");const{root:d,clear:m}=l;f?.();for await(const S of b(d,a))this.push(S);c?.(),this.push(null),m(!1)}catch(d){const m=d instanceof Error?d:new Error(String(d));o?.(m),this.destroy(m),l?.clear(!1)}}})}async function p(e,t={}){const{url:n="https://example.com",selector:r="body",generatePlaceholders:s=!1,providers:u={},onError:f}=t;try{const{root:c,clear:o}=h.runHeadless(()=>e,{startUrl:n,selector:r,providers:u}),a=H(c,s);return o(!1),a}catch(c){const o=c instanceof Error?c:new Error(String(c));throw f?.(o),o}}async function T(e,t={}){return p(e,{...t,generatePlaceholders:!1})}async function*b(e,t){yield*e.contentToHTMLStream(t);const n=e.getPortals().filter(r=>r!==e);for(const r of n)yield*r.contentToHTMLStream(t)}function H(e,t){const n=[];n.push(e.contentToHTML(t));const r=e.getPortals().filter(s=>s!==e);for(const s of r)n.push(s.contentToHTML(t));return n.join("")}function w(e,t={}){const{hydrate:n=!0,getData:r,selector:s="body",providers:u={}}=t;async function f(o){const a=r?await r(o):{};return p(e(a),{url:o,selector:s,providers:u,generatePlaceholders:n})}function c(o){if(r){const a=new P.Readable({read(){}});return Promise.resolve(r(o)).then(l=>{const i=g(e(l),{url:o,selector:s,providers:u,generatePlaceholders:n});i.on("data",y=>a.push(y)),i.on("end",()=>a.push(null)),i.on("error",y=>a.destroy(y))}).catch(l=>{a.destroy(l instanceof Error?l:new Error(String(l)))}),a}return g(e({}),{url:o,selector:s,providers:u,generatePlaceholders:n})}return{render:f,renderStream:c}}Object.defineProperty(exports,"HeadlessContext",{enumerable:!0,get:()=>h.HeadlessContext});Object.defineProperty(exports,"HeadlessPortal",{enumerable:!0,get:()=>h.HeadlessPortal});Object.defineProperty(exports,"runHeadless",{enumerable:!0,get:()=>h.runHeadless});exports.createRenderer=w;exports.renderToStaticMarkup=T;exports.renderToStream=g;exports.renderToString=p;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { Readable } from 'stream';
|
|
2
|
+
import { Value } from '@tempots/core';
|
|
3
|
+
import { Renderable, Providers, HeadlessContext, HeadlessPortal, runHeadless, StreamOptions } from '@tempots/dom';
|
|
4
|
+
/**
|
|
5
|
+
* Options for server-side rendering.
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export interface RenderOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Streaming lifecycle callbacks.
|
|
11
|
+
*/
|
|
12
|
+
onShellReady?: () => void;
|
|
13
|
+
onAllReady?: () => void;
|
|
14
|
+
onError?: (error: Error) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Initial URL for routing (defaults to 'https://example.com').
|
|
17
|
+
*/
|
|
18
|
+
url?: string;
|
|
19
|
+
/**
|
|
20
|
+
* The selector used to find the root element (defaults to 'body').
|
|
21
|
+
*/
|
|
22
|
+
selector?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Whether to generate hydration placeholder attributes.
|
|
25
|
+
*/
|
|
26
|
+
generatePlaceholders?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Providers to inject during rendering.
|
|
29
|
+
*/
|
|
30
|
+
providers?: Providers;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Result of a headless render operation.
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export interface HeadlessRenderResult {
|
|
37
|
+
/**
|
|
38
|
+
* The root portal containing the rendered content.
|
|
39
|
+
*/
|
|
40
|
+
root: HeadlessPortal;
|
|
41
|
+
/**
|
|
42
|
+
* Function to clear/dispose the rendered content.
|
|
43
|
+
*/
|
|
44
|
+
clear: (removeTree?: boolean) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Current URL signal (useful for routing).
|
|
47
|
+
*/
|
|
48
|
+
currentURL: ReturnType<typeof Value.toSignal>["deriveProp"];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Renders a Renderable to a Node.js Readable stream.
|
|
52
|
+
*
|
|
53
|
+
* This function enables streaming server-side rendering, allowing the server
|
|
54
|
+
* to start sending HTML to the client before the entire page is rendered.
|
|
55
|
+
* This improves Time To First Byte (TTFB) for large pages.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* import { renderToStream } from '@tempots/server'
|
|
60
|
+
* import { html } from '@tempots/dom'
|
|
61
|
+
*
|
|
62
|
+
* const App = () => html.div(
|
|
63
|
+
* html.h1('Hello, World!'),
|
|
64
|
+
* html.p('This is streamed from the server.')
|
|
65
|
+
* )
|
|
66
|
+
*
|
|
67
|
+
* // Express example
|
|
68
|
+
* app.get('/', (req, res) => {
|
|
69
|
+
* const stream = renderToStream(App(), { url: req.url })
|
|
70
|
+
* stream.pipe(res)
|
|
71
|
+
* })
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @param renderable - The Renderable to render.
|
|
75
|
+
* @param options - Rendering options.
|
|
76
|
+
* @returns A Node.js Readable stream of HTML chunks.
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
export declare function renderToStream(renderable: Renderable, options?: RenderOptions): Readable;
|
|
80
|
+
/**
|
|
81
|
+
* Renders a Renderable to an HTML string.
|
|
82
|
+
*
|
|
83
|
+
* This is a simpler alternative to `renderToStream` when streaming is not needed.
|
|
84
|
+
* It waits for the entire render to complete before returning the HTML string.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* import { renderToString } from '@tempots/server'
|
|
89
|
+
* import { html } from '@tempots/dom'
|
|
90
|
+
*
|
|
91
|
+
* const App = () => html.div(
|
|
92
|
+
* html.h1('Hello, World!'),
|
|
93
|
+
* html.p('This is rendered on the server.')
|
|
94
|
+
* )
|
|
95
|
+
*
|
|
96
|
+
* const html = await renderToString(App())
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @param renderable - The Renderable to render.
|
|
100
|
+
* @param options - Rendering options.
|
|
101
|
+
* @returns A Promise that resolves to the HTML string.
|
|
102
|
+
* @public
|
|
103
|
+
*/
|
|
104
|
+
export declare function renderToString(renderable: Renderable, options?: Omit<RenderOptions, "onShellReady" | "onAllReady">): Promise<string>;
|
|
105
|
+
/**
|
|
106
|
+
* Renders a Renderable to static HTML markup without hydration markers.
|
|
107
|
+
*
|
|
108
|
+
* Use this when you don't need client-side hydration, such as for
|
|
109
|
+
* generating static HTML pages, emails, or PDFs.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* import { renderToStaticMarkup } from '@tempots/server'
|
|
114
|
+
* import { html } from '@tempots/dom'
|
|
115
|
+
*
|
|
116
|
+
* const EmailTemplate = () => html.div(
|
|
117
|
+
* html.h1('Welcome!'),
|
|
118
|
+
* html.p('Thank you for signing up.')
|
|
119
|
+
* )
|
|
120
|
+
*
|
|
121
|
+
* const markup = await renderToStaticMarkup(EmailTemplate())
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @param renderable - The Renderable to render.
|
|
125
|
+
* @param options - Rendering options (generatePlaceholders is forced to false).
|
|
126
|
+
* @returns A Promise that resolves to the static HTML string.
|
|
127
|
+
* @public
|
|
128
|
+
*/
|
|
129
|
+
export declare function renderToStaticMarkup(renderable: Renderable, options?: Omit<RenderOptions, "onShellReady" | "onAllReady" | "generatePlaceholders">): Promise<string>;
|
|
130
|
+
/**
|
|
131
|
+
* Options for createRenderer.
|
|
132
|
+
* @public
|
|
133
|
+
*/
|
|
134
|
+
export interface RendererOptions<O extends Record<string, unknown>> {
|
|
135
|
+
/**
|
|
136
|
+
* Generate hydration placeholders in the output.
|
|
137
|
+
* @default true
|
|
138
|
+
*/
|
|
139
|
+
hydrate?: boolean;
|
|
140
|
+
/**
|
|
141
|
+
* Function to get initial data for each request.
|
|
142
|
+
* The returned object will be passed to the App component.
|
|
143
|
+
*/
|
|
144
|
+
getData?: (url: string) => O | Promise<O>;
|
|
145
|
+
/**
|
|
146
|
+
* The selector used to find the root element.
|
|
147
|
+
* @default "body"
|
|
148
|
+
*/
|
|
149
|
+
selector?: string;
|
|
150
|
+
/**
|
|
151
|
+
* Providers to inject during rendering.
|
|
152
|
+
*/
|
|
153
|
+
providers?: Providers;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Result of createRenderer containing render functions.
|
|
157
|
+
* @public
|
|
158
|
+
*/
|
|
159
|
+
export interface Renderer {
|
|
160
|
+
/**
|
|
161
|
+
* Renders the app to an HTML string.
|
|
162
|
+
*/
|
|
163
|
+
render: (url: string) => Promise<string>;
|
|
164
|
+
/**
|
|
165
|
+
* Renders the app to a Node.js Readable stream.
|
|
166
|
+
*/
|
|
167
|
+
renderStream: (url: string) => Readable;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Creates standard render functions for SSR entry points.
|
|
171
|
+
*
|
|
172
|
+
* This is a high-level convenience function that creates the standard
|
|
173
|
+
* `render()` and `renderStream()` exports for your entry-server.ts file.
|
|
174
|
+
*
|
|
175
|
+
* For more control, use the lower-level `renderToString()` and `renderToStream()` functions directly.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* // entry-server.ts
|
|
180
|
+
* import { createRenderer } from '@tempots/server'
|
|
181
|
+
* import { App } from './app'
|
|
182
|
+
*
|
|
183
|
+
* export const { render, renderStream } = createRenderer(App, {
|
|
184
|
+
* getData: (url) => ({
|
|
185
|
+
* timestamp: new Date().toISOString(),
|
|
186
|
+
* path: new URL(url, 'https://example.com').pathname
|
|
187
|
+
* })
|
|
188
|
+
* })
|
|
189
|
+
*
|
|
190
|
+
* export { App }
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* // Without getData (for simple apps)
|
|
196
|
+
* export const { render, renderStream } = createRenderer(App)
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
* @param App - The app component factory function.
|
|
200
|
+
* @param options - Renderer options.
|
|
201
|
+
* @returns An object with `render` and `renderStream` functions.
|
|
202
|
+
* @public
|
|
203
|
+
*/
|
|
204
|
+
export declare function createRenderer<O extends Record<string, unknown>>(App: (options: O) => Renderable, options?: RendererOptions<O>): Renderer;
|
|
205
|
+
export type { Renderable, Providers, StreamOptions };
|
|
206
|
+
export { runHeadless, HeadlessContext, HeadlessPortal };
|
package/index.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Readable as p } from "stream";
|
|
2
|
+
import { runHeadless as g } from "@tempots/dom";
|
|
3
|
+
import { HeadlessContext as b, HeadlessPortal as v, runHeadless as k } from "@tempots/dom";
|
|
4
|
+
function y(r, t = {}) {
|
|
5
|
+
const {
|
|
6
|
+
url: n = "https://example.com",
|
|
7
|
+
selector: e = "body",
|
|
8
|
+
generatePlaceholders: s = !1,
|
|
9
|
+
providers: i = {},
|
|
10
|
+
onShellReady: f,
|
|
11
|
+
onAllReady: l,
|
|
12
|
+
onError: o
|
|
13
|
+
} = t, a = { generatePlaceholders: s };
|
|
14
|
+
let c = null, u = null;
|
|
15
|
+
try {
|
|
16
|
+
c = g(() => r, {
|
|
17
|
+
startUrl: n,
|
|
18
|
+
selector: e,
|
|
19
|
+
providers: i
|
|
20
|
+
});
|
|
21
|
+
} catch (d) {
|
|
22
|
+
u = d instanceof Error ? d : new Error(String(d));
|
|
23
|
+
}
|
|
24
|
+
return new p({
|
|
25
|
+
async read() {
|
|
26
|
+
try {
|
|
27
|
+
if (u)
|
|
28
|
+
throw u;
|
|
29
|
+
if (!c)
|
|
30
|
+
throw new Error("Render result is null");
|
|
31
|
+
const { root: d, clear: m } = c;
|
|
32
|
+
f?.();
|
|
33
|
+
for await (const S of T(d, a))
|
|
34
|
+
this.push(S);
|
|
35
|
+
l?.(), this.push(null), m(!1);
|
|
36
|
+
} catch (d) {
|
|
37
|
+
const m = d instanceof Error ? d : new Error(String(d));
|
|
38
|
+
o?.(m), this.destroy(m), c?.clear(!1);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async function P(r, t = {}) {
|
|
44
|
+
const {
|
|
45
|
+
url: n = "https://example.com",
|
|
46
|
+
selector: e = "body",
|
|
47
|
+
generatePlaceholders: s = !1,
|
|
48
|
+
providers: i = {},
|
|
49
|
+
onError: f
|
|
50
|
+
} = t;
|
|
51
|
+
try {
|
|
52
|
+
const { root: l, clear: o } = g(() => r, {
|
|
53
|
+
startUrl: n,
|
|
54
|
+
selector: e,
|
|
55
|
+
providers: i
|
|
56
|
+
}), a = w(l, s);
|
|
57
|
+
return o(!1), a;
|
|
58
|
+
} catch (l) {
|
|
59
|
+
const o = l instanceof Error ? l : new Error(String(l));
|
|
60
|
+
throw f?.(o), o;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function M(r, t = {}) {
|
|
64
|
+
return P(r, {
|
|
65
|
+
...t,
|
|
66
|
+
generatePlaceholders: !1
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
async function* T(r, t) {
|
|
70
|
+
yield* r.contentToHTMLStream(t);
|
|
71
|
+
const n = r.getPortals().filter((e) => e !== r);
|
|
72
|
+
for (const e of n)
|
|
73
|
+
yield* e.contentToHTMLStream(t);
|
|
74
|
+
}
|
|
75
|
+
function w(r, t) {
|
|
76
|
+
const n = [];
|
|
77
|
+
n.push(r.contentToHTML(t));
|
|
78
|
+
const e = r.getPortals().filter((s) => s !== r);
|
|
79
|
+
for (const s of e)
|
|
80
|
+
n.push(s.contentToHTML(t));
|
|
81
|
+
return n.join("");
|
|
82
|
+
}
|
|
83
|
+
function R(r, t = {}) {
|
|
84
|
+
const {
|
|
85
|
+
hydrate: n = !0,
|
|
86
|
+
getData: e,
|
|
87
|
+
selector: s = "body",
|
|
88
|
+
providers: i = {}
|
|
89
|
+
} = t;
|
|
90
|
+
async function f(o) {
|
|
91
|
+
const a = e ? await e(o) : {};
|
|
92
|
+
return P(r(a), {
|
|
93
|
+
url: o,
|
|
94
|
+
selector: s,
|
|
95
|
+
providers: i,
|
|
96
|
+
generatePlaceholders: n
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function l(o) {
|
|
100
|
+
if (e) {
|
|
101
|
+
const a = new p({
|
|
102
|
+
read() {
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return Promise.resolve(e(o)).then((c) => {
|
|
106
|
+
const u = y(r(c), {
|
|
107
|
+
url: o,
|
|
108
|
+
selector: s,
|
|
109
|
+
providers: i,
|
|
110
|
+
generatePlaceholders: n
|
|
111
|
+
});
|
|
112
|
+
u.on("data", (h) => a.push(h)), u.on("end", () => a.push(null)), u.on("error", (h) => a.destroy(h));
|
|
113
|
+
}).catch((c) => {
|
|
114
|
+
a.destroy(
|
|
115
|
+
c instanceof Error ? c : new Error(String(c))
|
|
116
|
+
);
|
|
117
|
+
}), a;
|
|
118
|
+
}
|
|
119
|
+
return y(r({}), {
|
|
120
|
+
url: o,
|
|
121
|
+
selector: s,
|
|
122
|
+
providers: i,
|
|
123
|
+
generatePlaceholders: n
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
render: f,
|
|
128
|
+
renderStream: l
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
export {
|
|
132
|
+
b as HeadlessContext,
|
|
133
|
+
v as HeadlessPortal,
|
|
134
|
+
R as createRenderer,
|
|
135
|
+
M as renderToStaticMarkup,
|
|
136
|
+
y as renderToStream,
|
|
137
|
+
P as renderToString,
|
|
138
|
+
k as runHeadless
|
|
139
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tempots/server",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./index.cjs",
|
|
6
|
+
"module": "./index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./index.js",
|
|
11
|
+
"require": "./index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"description": "Server-side rendering utilities for Tempo applications",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"tempo",
|
|
18
|
+
"tempots",
|
|
19
|
+
"ssr",
|
|
20
|
+
"server",
|
|
21
|
+
"streaming",
|
|
22
|
+
"rendering"
|
|
23
|
+
],
|
|
24
|
+
"homepage": "https://github.com/fponticelli/tempots",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/fponticelli/tempots/issues"
|
|
27
|
+
},
|
|
28
|
+
"author": {
|
|
29
|
+
"name": "Franco Ponticelli",
|
|
30
|
+
"email": "franco.ponticelli@gmail.com",
|
|
31
|
+
"url": "https://github.com/fponticelli"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/fponticelli/tempots.git"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@tempots/core": "^2.1.0",
|
|
39
|
+
"@tempots/dom": "^36.0.0"
|
|
40
|
+
}
|
|
41
|
+
}
|