@sveltejs/kit 1.0.0-next.230 → 1.0.0-next.234

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,7 +1,7 @@
1
1
  import fs__default from 'fs';
2
2
  import path__default from 'path';
3
3
  import { s } from './misc.js';
4
- import { m as mkdirp, a as runtime, f as posixify } from '../cli.js';
4
+ import { m as mkdirp, b as runtime, f as posixify } from '../cli.js';
5
5
 
6
6
  /**
7
7
  * Takes zero or more objects and returns a new object that has all the values
@@ -1,6 +1,6 @@
1
1
  import fs__default from 'fs';
2
2
  import path__default from 'path';
3
- import { p as print_config_conflicts, S as SVELTE_KIT, d as copy_assets, f as posixify, e as get_aliases, r as resolve_entry, a as runtime, l as load_template, m as mkdirp, h as rimraf } from '../cli.js';
3
+ import { p as print_config_conflicts, a as SVELTE_KIT, d as copy_assets, f as posixify, e as get_aliases, r as resolve_entry, b as runtime, l as load_template, m as mkdirp, h as rimraf } from '../cli.js';
4
4
  import { d as deep_merge, a as create_app, c as create_manifest_data } from './index2.js';
5
5
  import { g as generate_manifest } from './index4.js';
6
6
  import vite from 'vite';
@@ -268,7 +268,7 @@ set_paths(${s(config.kit.paths)});
268
268
  // named imports without triggering Rollup's missing import detection
269
269
  const get_hooks = hooks => ({
270
270
  getSession: hooks.getSession || (() => ({})),
271
- handle: hooks.handle || (({ request, resolve }) => resolve(request)),
271
+ handle: hooks.handle || (({ event, resolve }) => resolve(event)),
272
272
  handleError: hooks.handleError || (({ error }) => console.error(error.stack)),
273
273
  externalFetch: hooks.externalFetch || fetch
274
274
  });
@@ -293,8 +293,17 @@ export class App {
293
293
  dev: false,
294
294
  floc: ${config.kit.floc},
295
295
  get_stack: error => String(error), // for security
296
- handle_error: (error, request) => {
297
- hooks.handleError({ error, request });
296
+ handle_error: (error, event) => {
297
+ hooks.handleError({
298
+ error,
299
+ event,
300
+
301
+ // TODO remove for 1.0
302
+ // @ts-expect-error
303
+ get request() {
304
+ throw new Error('request in handleError has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details');
305
+ }
306
+ });
298
307
  error.stack = this.options.get_stack(error);
299
308
  },
300
309
  hooks,
@@ -317,25 +326,11 @@ export class App {
317
326
  render(request, {
318
327
  prerender
319
328
  } = {}) {
320
- // TODO remove this for 1.0
321
- if (Object.keys(request).sort().join() !== 'headers,method,rawBody,url') {
322
- throw new Error('Adapters should call app.render({ url, method, headers, rawBody })');
329
+ if (!(request instanceof Request)) {
330
+ throw new Error('The first argument to app.render must be a Request object. See https://github.com/sveltejs/kit/pull/3384 for details');
323
331
  }
324
332
 
325
- const host = ${
326
- config.kit.host
327
- ? s(config.kit.host)
328
- : `request.headers[${s(config.kit.headers.host || 'host')}]`
329
- };
330
- const protocol = ${
331
- config.kit.protocol
332
- ? s(config.kit.protocol)
333
- : config.kit.headers.protocol
334
- ? `request.headers[${s(config.kit.headers.protocol)}] || default_protocol`
335
- : 'default_protocol'
336
- };
337
-
338
- return respond({ ...request, url: new URL(request.url, protocol + '://' + host) }, this.options, { prerender });
333
+ return respond(request, this.options, { prerender });
339
334
  }
340
335
  }
341
336
  `;
@@ -1,9 +1,9 @@
1
- import { m as mkdirp, S as SVELTE_KIT, h as rimraf, i as copy, $, j as logger } from '../cli.js';
1
+ import { m as mkdirp, a as SVELTE_KIT, h as rimraf, i as copy, $, j as logger } from '../cli.js';
2
2
  import { readFileSync, writeFileSync } from 'fs';
3
3
  import { resolve, join, dirname } from 'path';
4
4
  import { pathToFileURL, URL } from 'url';
5
5
  import { __fetch_polyfill } from '../install-fetch.js';
6
- import { g as get_single_valued_header, r as resolve$1, i as is_root_relative } from './url.js';
6
+ import { r as resolve$1, i as is_root_relative } from './url.js';
7
7
  import { g as generate_manifest } from './index4.js';
8
8
  import 'sade';
9
9
  import 'child_process';
@@ -412,28 +412,19 @@ async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
412
412
  * @param {string?} referrer
413
413
  */
414
414
  async function visit(path, decoded_path, referrer) {
415
- /** @type {Map<string, import('types/hooks').ServerResponse>} */
415
+ /** @type {Map<string, Response>} */
416
416
  const dependencies = new Map();
417
417
 
418
- const rendered = await app.render(
419
- {
420
- url: `${config.kit.protocol || 'http'}://${config.kit.host || 'prerender'}${path}`,
421
- method: 'GET',
422
- headers: {},
423
- rawBody: null
424
- },
425
- {
426
- prerender: {
427
- all,
428
- dependencies
429
- }
418
+ const rendered = await app.render(new Request(`http://sveltekit-prerender${path}`), {
419
+ prerender: {
420
+ all,
421
+ dependencies
430
422
  }
431
- );
423
+ });
432
424
 
433
425
  if (rendered) {
434
426
  const response_type = Math.floor(rendered.status / 100);
435
- const headers = rendered.headers;
436
- const type = headers && headers['content-type'];
427
+ const type = rendered.headers.get('content-type');
437
428
  const is_html = response_type === REDIRECT || type === 'text/html';
438
429
 
439
430
  const parts = decoded_path.split('/');
@@ -444,7 +435,7 @@ async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
444
435
  const file = `${out}${parts.join('/')}`;
445
436
 
446
437
  if (response_type === REDIRECT) {
447
- const location = get_single_valued_header(headers, 'location');
438
+ const location = rendered.headers.get('location');
448
439
 
449
440
  if (location) {
450
441
  mkdirp(dirname(file));
@@ -463,20 +454,22 @@ async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
463
454
  return;
464
455
  }
465
456
 
457
+ const text = await rendered.text();
458
+
466
459
  if (rendered.status === 200) {
467
460
  mkdirp(dirname(file));
468
461
 
469
462
  log.info(`${rendered.status} ${decoded_path}`);
470
- writeFileSync(file, rendered.body || '');
463
+ writeFileSync(file, text);
471
464
  paths.push(normalize(decoded_path));
472
465
  } else if (response_type !== OK) {
473
466
  error({ status: rendered.status, path, referrer, referenceType: 'linked' });
474
467
  }
475
468
 
476
- dependencies.forEach((result, dependency_path) => {
469
+ for (const [dependency_path, result] of dependencies) {
477
470
  const response_type = Math.floor(result.status / 100);
478
471
 
479
- const is_html = result.headers['content-type'] === 'text/html';
472
+ const is_html = result.headers.get('content-type') === 'text/html';
480
473
 
481
474
  const parts = dependency_path.split('/');
482
475
  if (is_html && parts[parts.length - 1] !== 'index.html') {
@@ -487,7 +480,7 @@ async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
487
480
  mkdirp(dirname(file));
488
481
 
489
482
  if (result.body) {
490
- writeFileSync(file, result.body);
483
+ writeFileSync(file, await result.text());
491
484
  paths.push(dependency_path);
492
485
  }
493
486
 
@@ -501,17 +494,23 @@ async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
501
494
  referenceType: 'fetched'
502
495
  });
503
496
  }
504
- });
497
+ }
505
498
 
506
499
  if (is_html && config.kit.prerender.crawl) {
507
- for (const href of crawl(/** @type {string} */ (rendered.body))) {
508
- if (href.startsWith('data:')) continue;
500
+ for (const href of crawl(text)) {
501
+ if (href.startsWith('data:') || href.startsWith('#')) continue;
509
502
 
510
503
  const resolved = resolve$1(path, href);
511
504
  if (!is_root_relative(resolved)) continue;
512
505
 
513
506
  const parsed = new URL(resolved, 'http://localhost');
514
- const pathname = decodeURI(parsed.pathname).replace(config.kit.paths.base, '');
507
+
508
+ let pathname = decodeURI(parsed.pathname);
509
+
510
+ if (config.kit.paths.base) {
511
+ if (!pathname.startsWith(config.kit.paths.base)) continue;
512
+ pathname = pathname.slice(config.kit.paths.base.length) || '/';
513
+ }
515
514
 
516
515
  const file = pathname.slice(1);
517
516
  if (files.has(file)) continue;
@@ -539,25 +538,17 @@ async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
539
538
  }
540
539
 
541
540
  if (fallback) {
542
- const rendered = await app.render(
543
- {
544
- url: `${config.kit.protocol || 'http'}://${config.kit.host || 'prerender'}/[fallback]`,
545
- method: 'GET',
546
- headers: {},
547
- rawBody: null
548
- },
549
- {
550
- prerender: {
551
- fallback,
552
- all: false,
553
- dependencies: new Map()
554
- }
541
+ const rendered = await app.render(new Request('http://sveltekit-prerender/[fallback]'), {
542
+ prerender: {
543
+ fallback,
544
+ all: false,
545
+ dependencies: new Map()
555
546
  }
556
- );
547
+ });
557
548
 
558
549
  const file = join(out, fallback);
559
550
  mkdirp(dirname(file));
560
- writeFileSync(file, rendered.body || '');
551
+ writeFileSync(file, await rendered.text());
561
552
  }
562
553
 
563
554
  return {