-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathpage.ts
More file actions
396 lines (336 loc) · 9.69 KB
/
page.ts
File metadata and controls
396 lines (336 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import devalue from 'devalue';
import { createReadStream, existsSync } from 'fs';
import * as mime from 'mime';
import fetch, { Response } from 'node-fetch';
import { readable, writable } from 'svelte/store';
import { parse, resolve, URLSearchParams } from 'url';
import {
EndpointResponse,
Headers,
IncomingRequest,
PageContext,
PageManifest,
PageManifestPart,
PageResponse,
RenderOptions,
RouteParams
} from '../../types';
import { render } from './index';
const noop = () => {};
type FetchOpts = {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS';
headers?: Headers;
body?: any;
};
export default async function render_page(
request: IncomingRequest,
context: any,
options: RenderOptions,
status: number = 200,
error: Error | null = null
): Promise<
| {
status: number;
body: string;
headers: Headers;
dependencies: Record<string, EndpointResponse>;
}
| undefined
> {
let redirected: PageResponse;
let preload_error;
const page: PageManifest | undefined = options.manifest.pages.find((page) =>
page.pattern.test(request.path)
);
const baseUrl = ''; // TODO
const session = await (options.setup.getSession && options.setup.getSession(context));
const serialized_session = try_serialize(session, (err: Error) => {
throw new Error(`Failed to serialize session data: ${err.message}`);
});
try {
if (!page) {
const error: any = new Error(`Not found: ${request.path}`);
error.status = 404;
throw error;
}
const segments = request.path.split('/').filter(Boolean);
// TODO make this less confusing
const layout_segments = [segments[0]];
let l = 1;
page.parts.forEach((part, i) => {
layout_segments[l] = segments[i + 1];
if (!part) return;
l++;
});
const dependencies: Record<string, EndpointResponse> = {};
const preload_context = {
redirect: (status: number, location: string) => {
if (
redirected &&
(redirected.status !== status || redirected.headers.location !== location)
) {
throw new Error(`Conflicting redirects`);
}
location = location.replace(/^\//g, ''); // leading slash (only)
redirected = {
status,
headers: { location },
body: null,
dependencies: {}
};
},
error: (status: number, error: Error | string) => {
if (typeof error === 'string') {
error = new Error(error);
}
preload_error = { ...error, status };
},
fetch: async (url: string, opts: FetchOpts = {}) => {
const parsed = parse(url);
if (parsed.protocol) {
// external fetch
return fetch(parsed.href, opts);
}
// otherwise we're dealing with an internal fetch. TODO there's
// probably no advantage to using fetch here — we should replace
// `this.fetch` with `this.load` or whatever
const resolved = resolve(request.path, parsed.pathname!);
// edge case — fetching a static file
const candidates = [
`${options.static_dir}${resolved}`,
`${options.static_dir}${resolved}/index.html`
];
for (const file of candidates) {
if (existsSync(file)) {
return new Response(createReadStream(file), {
headers: {
'content-type': mime.getType(file)!
}
});
}
}
// TODO this doesn't take account of opts.body
const rendered = await render(
{
host: request.host,
method: opts.method || 'GET',
headers: opts.headers || {}, // TODO inject credentials...
path: resolved,
body: opts.body,
query: new URLSearchParams(parsed.query || '')
},
options
);
if (rendered) {
// TODO this is primarily for the benefit of the static case,
// but could it be used elsewhere?
dependencies[resolved] = rendered;
return new Response(rendered.body, {
status: rendered.status,
headers: rendered.headers
});
} else {
return new Response('Not found', {
status: 404
});
}
}
};
const match = page.pattern.exec(request.path)!;
// the last part has all parameters from any segment in the URL
const params = parts_to_params(match, page.parts[page.parts.length - 1] as PageManifestPart);
const preloaded: any[] = [];
let can_prerender = true;
const parts = await Promise.all(
[{ component: options.manifest.layout, params: [] }, ...page.parts].map(async (part, i) => {
if (!part) return null;
const mod = await options.load(part.component);
if (options.only_prerender && !mod.prerender) {
can_prerender = false;
return;
}
// these are only the parameters up to the current URL segment
const params = parts_to_params(match, part);
const props = mod.preload
? await mod.preload.call(
preload_context,
{
host: request.host,
path: request.path,
query: request.query,
params
},
session
)
: {};
preloaded[i] = props;
return { component: mod.default, props };
})
);
if (options.only_prerender && !can_prerender) return;
if (preload_error) throw preload_error;
if (redirected!) return redirected!;
const branches: Array<{
component: any; // TODO
props: any;
segment: string;
}> = [];
parts.forEach((part, i) => {
if (part) {
branches.push({
component: part.component,
props: preloaded[i],
segment: segments[i]
});
}
});
const pageContext: PageContext = {
host: request.host as string,
path: request.path,
query: search_params_to_map(request.query),
params,
error: error || undefined
};
const props: Record<string, any> = {
status,
error,
stores: {
page: readable(pageContext, noop),
preloading: readable(null, noop),
session: writable(session)
},
// TODO stores, status, segments, notify, CONTEXT_KEY
segments: layout_segments,
branches,
level0: {
props: preloaded[0]
},
level1: {
segment: segments[0],
props: {}
}
};
// TODO this is highly confusing. replace the leveln thing with an array of branches
l = 1;
for (let i = 1; i < parts.length; i += 1) {
const part = parts[i];
if (!part) continue;
props[`level${l++}`] = {
component: part.component,
props: preloaded[i] || {},
segment: segments[i]
};
}
const serialized_preloads = `[${preloaded
.map((data) =>
try_serialize(data, (err: Error) => {
const path = '/' + segments.join('/');
console.error(
`Failed to serialize preloaded data to transmit to the client at the ${path} route: ${err.message}`
);
console.warn(
'The client will re-render over the server-rendered page fresh instead of continuing where it left off. See https://sapper.svelte.dev/docs#Return_value for more information'
);
})
)
.join(',')}]`;
const rendered = options.root.default.render(props);
const deps = options.client.deps;
const js_deps = new Set(deps.__entry__ ? [...deps.__entry__.js] : []);
const css_deps = new Set(deps.__entry__ ? [...deps.__entry__.css] : []);
(page.parts.filter(Boolean) as PageManifestPart[]).forEach((part) => {
const page_deps = deps[part.component.name];
if (!page_deps) return; // we don't have this info during dev
page_deps.js.forEach((dep) => js_deps.add(dep));
page_deps.css.forEach((dep) => css_deps.add(dep));
});
const head = `${rendered.head}
${Array.from(js_deps)
.map((dep) => `<link rel="modulepreload" href="/_app/${dep}">`)
.join('\n\t\t\t')}
${Array.from(css_deps)
.map((dep) => `<link rel="stylesheet" href="/_app/${dep}">`)
.join('\n\t\t\t')}
${options.dev ? `<style>${rendered.css.code}</style>` : ''}
<script type="module">
import { start } from '/_app/${options.client.entry}';
start({
target: document.querySelector('#svelte') || document.body
});
</script>`.replace(/^\t{2}/gm, ''); // TODO add links
const body = `${rendered.html}
<script>
__SVELTE__ = {
baseUrl: "${baseUrl}",
status: ${status},
error: ${serialize_error(error)},
preloaded: ${serialized_preloads},
session: ${serialized_session}
};
</script>`.replace(/^\t{3}/gm, '');
const html = options.template.replace('%svelte.head%', head).replace('%svelte.body%', body);
return {
status: 200,
headers: {
'content-type': 'text/html'
},
body: html,
dependencies
};
} catch (thrown) {
console.error(thrown.stack);
if (!error) {
const status = thrown.status || 500;
return render_page(request, context, options, status, thrown);
} else {
// oh lawd now you've done it
return {
status: 500,
headers: {},
body: thrown.stack, // TODO probably not in prod?
dependencies: {}
};
}
}
}
function parts_to_params(match: RegExpMatchArray, part: PageManifestPart): RouteParams {
const params: RouteParams = {};
part.params.forEach((name, i) => {
const is_spread = /^\.{3}.+$/.test(name);
if (is_spread) {
params[name.slice(3)] = match[i + 1].split('/');
} else {
params[name] = match[i + 1];
}
});
return params;
}
function try_serialize(data: any, fail?: (err: Error) => void) {
try {
return devalue(data);
} catch (err) {
if (fail) fail(err);
return null;
}
}
// Ensure we return something truthy so the client will not re-render the page over the error
function serialize_error(error?: Error | null) {
if (!error) return null;
let serialized = try_serialize(error);
if (!serialized) {
const { name, message, stack } = error;
serialized = try_serialize({ name, message, stack });
}
if (!serialized) {
serialized = '{}';
}
return serialized;
}
function search_params_to_map(params: URLSearchParams) {
const map: Record<string, string | string[]> = {};
for (const key of params.keys()) {
const values = params.getAll(key);
map[key] = values.length > 1 ? values : values[0];
}
return map;
}