@sveltejs/kit 1.0.0-next.575 → 1.0.0-next.577

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/kit",
3
- "version": "1.0.0-next.575",
3
+ "version": "1.0.0-next.577",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -129,6 +129,8 @@ const options = object(
129
129
  checkOrigin: boolean(true)
130
130
  }),
131
131
 
132
+ embedded: boolean(false),
133
+
132
134
  // TODO: remove this for the 1.0 release
133
135
  endpointExtensions: error(
134
136
  (keypath) => `${keypath} has been renamed to config.kit.moduleExtensions`
@@ -66,6 +66,7 @@ export class Server {
66
66
  check_origin: ${s(config.kit.csrf.checkOrigin)},
67
67
  },
68
68
  dev: false,
69
+ embedded: ${config.kit.embedded},
69
70
  handle_error: (error, event) => {
70
71
  return this.options.hooks.handleError({
71
72
  error,
@@ -147,7 +147,8 @@ export function get_default_build_config({ config, input, ssr, outDir }) {
147
147
  __SVELTEKIT_APP_VERSION_FILE__: JSON.stringify(`${config.kit.appDir}/version.json`),
148
148
  __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: JSON.stringify(config.kit.version.pollInterval),
149
149
  __SVELTEKIT_BROWSER__: ssr ? 'false' : 'true',
150
- __SVELTEKIT_DEV__: 'false'
150
+ __SVELTEKIT_DEV__: 'false',
151
+ __SVELTEKIT_EMBEDDED__: config.kit.embedded ? 'true' : 'false'
151
152
  },
152
153
  publicDir: ssr ? false : config.kit.files.assets,
153
154
  resolve: {
@@ -449,6 +449,7 @@ export async function dev(vite, vite_config, svelte_config) {
449
449
  check_origin: svelte_config.kit.csrf.checkOrigin
450
450
  },
451
451
  dev: true,
452
+ embedded: svelte_config.kit.embedded,
452
453
  handle_error: async (error, event) => {
453
454
  const error_object = await hooks.handleError({
454
455
  error: new Proxy(error, {
@@ -59,9 +59,25 @@ const enforced_config = {
59
59
  root: true
60
60
  };
61
61
 
62
- /** @return {import('vite').Plugin[]} */
63
- export function sveltekit() {
64
- return [...svelte(), ...kit()];
62
+ /** @return {Promise<import('vite').Plugin[]>} */
63
+ export async function sveltekit() {
64
+ const svelte_config = await load_config();
65
+
66
+ /** @type {import('@sveltejs/vite-plugin-svelte').Options} */
67
+ const vite_plugin_svelte_options = {
68
+ configFile: false,
69
+ extensions: svelte_config.extensions,
70
+ preprocess: svelte_config.preprocess,
71
+ onwarn: svelte_config.onwarn,
72
+ compilerOptions: {
73
+ // @ts-expect-error SvelteKit requires hydratable true by default
74
+ hydratable: true,
75
+ ...svelte_config.compilerOptions
76
+ },
77
+ ...svelte_config.vitePlugin
78
+ };
79
+
80
+ return [...svelte(vite_plugin_svelte_options), ...kit({ svelte_config })];
65
81
  }
66
82
 
67
83
  /**
@@ -74,12 +90,10 @@ export function sveltekit() {
74
90
  * - https://rollupjs.org/guide/en/#build-hooks
75
91
  * - https://rollupjs.org/guide/en/#output-generation-hooks
76
92
  *
93
+ * @param {{ svelte_config: import('types').ValidatedConfig }} options
77
94
  * @return {import('vite').Plugin[]}
78
95
  */
79
- function kit() {
80
- /** @type {import('types').ValidatedConfig} */
81
- let svelte_config;
82
-
96
+ function kit({ svelte_config }) {
83
97
  /** @type {import('vite').ResolvedConfig} */
84
98
  let vite_config;
85
99
 
@@ -193,7 +207,6 @@ function kit() {
193
207
  */
194
208
  async config(config, config_env) {
195
209
  vite_config_env = config_env;
196
- svelte_config = await load_config();
197
210
 
198
211
  env = get_env(svelte_config.kit.env, vite_config_env.mode);
199
212
 
@@ -249,7 +262,8 @@ function kit() {
249
262
  define: {
250
263
  __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: '0',
251
264
  __SVELTEKIT_BROWSER__: config_env.ssrBuild ? 'false' : 'true',
252
- __SVELTEKIT_DEV__: 'true'
265
+ __SVELTEKIT_DEV__: 'true',
266
+ __SVELTEKIT_EMBEDDED__: svelte_config.kit.embedded ? 'true' : 'false'
253
267
  },
254
268
  publicDir: svelte_config.kit.files.assets,
255
269
  resolve: {
@@ -89,6 +89,7 @@ function check_for_removed_attributes() {
89
89
  * @returns {import('./types').Client}
90
90
  */
91
91
  export function create_client({ target, base }) {
92
+ const container = __SVELTEKIT_EMBEDDED__ ? target : document.documentElement;
92
93
  /** @type {Array<((url: URL) => boolean)>} */
93
94
  const invalidated = [];
94
95
 
@@ -1199,7 +1200,7 @@ export function create_client({ target, base }) {
1199
1200
  /** @type {NodeJS.Timeout} */
1200
1201
  let mousemove_timeout;
1201
1202
 
1202
- target.addEventListener('mousemove', (event) => {
1203
+ container.addEventListener('mousemove', (event) => {
1203
1204
  const target = /** @type {Element} */ (event.target);
1204
1205
 
1205
1206
  clearTimeout(mousemove_timeout);
@@ -1213,8 +1214,8 @@ export function create_client({ target, base }) {
1213
1214
  preload(/** @type {Element} */ (event.composedPath()[0]), 1);
1214
1215
  }
1215
1216
 
1216
- target.addEventListener('mousedown', tap);
1217
- target.addEventListener('touchstart', tap, { passive: true });
1217
+ container.addEventListener('mousedown', tap);
1218
+ container.addEventListener('touchstart', tap, { passive: true });
1218
1219
 
1219
1220
  const observer = new IntersectionObserver(
1220
1221
  (entries) => {
@@ -1233,7 +1234,7 @@ export function create_client({ target, base }) {
1233
1234
  * @param {number} priority
1234
1235
  */
1235
1236
  function preload(element, priority) {
1236
- const a = find_anchor(element, target);
1237
+ const a = find_anchor(element, container);
1237
1238
  if (!a) return;
1238
1239
 
1239
1240
  const { url, external } = get_link_info(a, base);
@@ -1253,7 +1254,7 @@ export function create_client({ target, base }) {
1253
1254
  function after_navigate() {
1254
1255
  observer.disconnect();
1255
1256
 
1256
- for (const a of target.querySelectorAll('a')) {
1257
+ for (const a of container.querySelectorAll('a')) {
1257
1258
  const { url, external } = get_link_info(a, base);
1258
1259
  if (external) continue;
1259
1260
 
@@ -1457,14 +1458,14 @@ export function create_client({ target, base }) {
1457
1458
  }
1458
1459
 
1459
1460
  /** @param {MouseEvent} event */
1460
- target.addEventListener('click', (event) => {
1461
+ container.addEventListener('click', (event) => {
1461
1462
  // Adapted from https://github.com/visionmedia/page.js
1462
1463
  // MIT license https://github.com/visionmedia/page.js#license
1463
1464
  if (event.button || event.which !== 1) return;
1464
1465
  if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
1465
1466
  if (event.defaultPrevented) return;
1466
1467
 
1467
- const a = find_anchor(/** @type {Element} */ (event.composedPath()[0]), target);
1468
+ const a = find_anchor(/** @type {Element} */ (event.composedPath()[0]), container);
1468
1469
  if (!a) return;
1469
1470
 
1470
1471
  const { url, external, has } = get_link_info(a, base);
@@ -1534,7 +1535,7 @@ export function create_client({ target, base }) {
1534
1535
  });
1535
1536
  });
1536
1537
 
1537
- target.addEventListener('submit', (event) => {
1538
+ container.addEventListener('submit', (event) => {
1538
1539
  if (event.defaultPrevented) return;
1539
1540
 
1540
1541
  const form = /** @type {HTMLFormElement} */ (
@@ -1639,11 +1640,25 @@ export function create_client({ target, base }) {
1639
1640
  });
1640
1641
  },
1641
1642
 
1642
- _hydrate: async ({ status, error, node_ids, params, route, data: server_data_nodes, form }) => {
1643
+ _hydrate: async ({
1644
+ status = 200,
1645
+ error,
1646
+ node_ids,
1647
+ params,
1648
+ route,
1649
+ data: server_data_nodes,
1650
+ form
1651
+ }) => {
1643
1652
  hydrated = true;
1644
1653
 
1645
1654
  const url = new URL(location.href);
1646
1655
 
1656
+ if (!__SVELTEKIT_EMBEDDED__) {
1657
+ // See https://github.com/sveltejs/kit/pull/4935#issuecomment-1328093358 for one motivation
1658
+ // of determining the params on the client side.
1659
+ ({ params = {}, route = { id: null } } = get_navigation_intent(url, false) || {});
1660
+ }
1661
+
1647
1662
  /** @type {import('./types').NavigationFinished | undefined} */
1648
1663
  let result;
1649
1664
 
@@ -266,24 +266,41 @@ export async function render_response({
266
266
  }
267
267
 
268
268
  if (page_config.csr) {
269
+ const opts = [
270
+ `env: ${s(options.public_env)}`,
271
+ `paths: ${s(options.paths)}`,
272
+ `target: document.querySelector('[data-sveltekit-hydrate="${target}"]').parentNode`,
273
+ `version: ${s(options.version)}`
274
+ ];
275
+
276
+ if (page_config.ssr) {
277
+ const hydrate = [
278
+ `node_ids: [${branch.map(({ node }) => node.index).join(', ')}]`,
279
+ `data: ${serialized.data}`,
280
+ `form: ${serialized.form}`
281
+ ];
282
+
283
+ if (status !== 200) {
284
+ hydrate.push(`status: ${status}`);
285
+ }
286
+
287
+ if (error) {
288
+ hydrate.push(`error: ${devalue.uneval(error)}`);
289
+ }
290
+
291
+ if (options.embedded) {
292
+ hydrate.push(`params: ${devalue.uneval(event.params)}`, `route: ${s(event.route)}`);
293
+ }
294
+
295
+ opts.push(`hydrate: {\n\t\t\t\t\t${hydrate.join(',\n\t\t\t\t\t')}\n\t\t\t\t}`);
296
+ }
297
+
269
298
  // prettier-ignore
270
299
  const init_app = `
271
300
  import { start } from ${s(prefixed(entry.file))};
272
301
 
273
302
  start({
274
- env: ${s(options.public_env)},
275
- hydrate: ${page_config.ssr ? `{
276
- status: ${status},
277
- error: ${devalue.uneval(error)},
278
- node_ids: [${branch.map(({ node }) => node.index).join(', ')}],
279
- params: ${devalue.uneval(event.params)},
280
- route: ${s(event.route)},
281
- data: ${serialized.data},
282
- form: ${serialized.form}
283
- }` : 'null'},
284
- paths: ${s(options.paths)},
285
- target: document.querySelector('[data-sveltekit-hydrate="${target}"]').parentNode,
286
- version: ${s(options.version)}
303
+ ${opts.join(',\n\t\t\t\t')}
287
304
  });
288
305
  `;
289
306
 
@@ -433,5 +433,5 @@ declare module '@sveltejs/kit/vite' {
433
433
  /**
434
434
  * Returns the SvelteKit Vite plugins.
435
435
  */
436
- export function sveltekit(): Plugin[];
436
+ export function sveltekit(): Promise<Plugin[]>;
437
437
  }
package/types/index.d.ts CHANGED
@@ -325,6 +325,11 @@ export interface KitConfig {
325
325
  */
326
326
  checkOrigin?: boolean;
327
327
  };
328
+ /**
329
+ * Whether or not the app is embedded inside a larger app. If `true`, SvelteKit will add its event listeners related to navigation etc on the parent of `%sveltekit.body%` instead of `window`, and will pass `params` from the server rather than inferring them from `location.pathname`.
330
+ * @default false
331
+ */
332
+ embedded?: boolean;
328
333
  /**
329
334
  * Environment variable configuration
330
335
  */
@@ -299,6 +299,7 @@ export interface SSROptions {
299
299
  check_origin: boolean;
300
300
  };
301
301
  dev: boolean;
302
+ embedded: boolean;
302
303
  handle_error(error: Error & { frame?: string }, event: RequestEvent): MaybePromise<App.Error>;
303
304
  hooks: ServerHooks;
304
305
  manifest: SSRManifest;
@@ -387,4 +388,5 @@ declare global {
387
388
  const __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: number;
388
389
  const __SVELTEKIT_BROWSER__: boolean;
389
390
  const __SVELTEKIT_DEV__: boolean;
391
+ const __SVELTEKIT_EMBEDDED__: boolean;
390
392
  }