@sveltejs/kit 1.0.0-next.494 → 1.0.0-next.496

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.494",
3
+ "version": "1.0.0-next.496",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -54,6 +54,7 @@
54
54
  "!src/core/**/test",
55
55
  "types",
56
56
  "svelte-kit.js",
57
+ "postinstall.js",
57
58
  "scripts/special-types"
58
59
  ],
59
60
  "exports": {
@@ -89,6 +90,6 @@
89
90
  "test:integration": "pnpm run -r --workspace-concurrency 1 --filter=\"./test/**\" test",
90
91
  "test:unit": "uvu src \"(spec\\.js|test[\\\\/]index\\.js)\"",
91
92
  "types": "node scripts/extract-types.js",
92
- "postinstall": "node svelte-kit.js sync"
93
+ "postinstall": "node postinstall.js"
93
94
  }
94
95
  }
package/postinstall.js ADDED
@@ -0,0 +1,37 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import glob from 'tiny-glob/sync.js';
4
+ import { load_config } from './src/core/config/index.js';
5
+ import * as sync from './src/core/sync/sync.js';
6
+
7
+ const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
8
+
9
+ const directories = [];
10
+
11
+ const cwd = process.env.INIT_CWD ?? process.cwd();
12
+
13
+ if (pkg.workspaces) {
14
+ for (const directory of pkg.workspaces) {
15
+ directories.push(...glob(directory, { cwd }).map((dir) => path.resolve(cwd, dir)));
16
+ }
17
+ } else {
18
+ directories.push(cwd);
19
+ }
20
+
21
+ for (const cwd of directories) {
22
+ process.chdir(cwd);
23
+
24
+ if (!fs.existsSync('package.json')) continue;
25
+ if (!fs.existsSync('svelte.config.js')) continue;
26
+
27
+ const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
28
+ if (!pkg.dependencies?.['@sveltejs/kit'] && !pkg.devDependencies?.['@sveltejs/kit']) continue;
29
+
30
+ try {
31
+ const config = await load_config();
32
+ await sync.all(config, 'development');
33
+ } catch (e) {
34
+ console.log('Error while trying to sync SvelteKit config');
35
+ console.log(e);
36
+ }
37
+ }
package/src/cli.js CHANGED
@@ -41,10 +41,6 @@ prog
41
41
  return;
42
42
  }
43
43
 
44
- if (event === 'postinstall' && process.env.INIT_CWD) {
45
- process.chdir(process.env.INIT_CWD);
46
- }
47
-
48
44
  if (!fs.existsSync('svelte.config.js')) {
49
45
  console.warn(`Missing ${path.resolve('svelte.config.js')} — skipping`);
50
46
  return;
@@ -28,7 +28,13 @@ export function enhance(form, submit = () => {}) {
28
28
  await invalidateAll();
29
29
  }
30
30
 
31
- if (location.origin + location.pathname === action.origin + action.pathname) {
31
+ // For success/invalid results, only apply action if it belongs to the
32
+ // current page, otherwise `form` will be updated erroneously
33
+ if (
34
+ location.origin + location.pathname === action.origin + action.pathname ||
35
+ result.type === 'redirect' ||
36
+ result.type === 'error'
37
+ ) {
32
38
  applyAction(result);
33
39
  }
34
40
  };
@@ -814,40 +814,22 @@ export function create_client({ target, base, trailing_slash }) {
814
814
  error = handle_error(err, { params, url, routeId: route.id });
815
815
  }
816
816
 
817
- while (i--) {
818
- if (errors[i]) {
819
- /** @type {import('./types').BranchNode | undefined} */
820
- let error_loaded;
821
-
822
- let j = i;
823
- while (!branch[j]) j -= 1;
824
- try {
825
- error_loaded = {
826
- node: await /** @type {import('types').CSRPageNodeLoader } */ (errors[i])(),
827
- loader: /** @type {import('types').CSRPageNodeLoader } */ (errors[i]),
828
- data: {},
829
- server: null,
830
- shared: null
831
- };
832
-
833
- return await get_navigation_result_from_branch({
834
- url,
835
- params,
836
- branch: branch.slice(0, j + 1).concat(error_loaded),
837
- status,
838
- error,
839
- route
840
- });
841
- } catch (e) {
842
- continue;
843
- }
844
- }
817
+ const error_load = await load_nearest_error_page(i, branch, errors);
818
+ if (error_load) {
819
+ return await get_navigation_result_from_branch({
820
+ url,
821
+ params,
822
+ branch: branch.slice(0, error_load.idx).concat(error_load.node),
823
+ status,
824
+ error,
825
+ route
826
+ });
827
+ } else {
828
+ // if we get here, it's because the root `load` function failed,
829
+ // and we need to fall back to the server
830
+ await native_navigation(url);
831
+ return;
845
832
  }
846
-
847
- // if we get here, it's because the root `load` function failed,
848
- // and we need to fall back to the server
849
- await native_navigation(url);
850
- return;
851
833
  }
852
834
  } else {
853
835
  // push an empty slot so we can rewind past gaps to the
@@ -868,6 +850,35 @@ export function create_client({ target, base, trailing_slash }) {
868
850
  });
869
851
  }
870
852
 
853
+ /**
854
+ * @param {number} i Start index to backtrack from
855
+ * @param {Array<import('./types').BranchNode | undefined>} branch Branch to backtrack
856
+ * @param {Array<import('types').CSRPageNodeLoader | undefined>} errors All error pages for this branch
857
+ * @returns {Promise<{idx: number; node: import('./types').BranchNode} | undefined>}
858
+ */
859
+ async function load_nearest_error_page(i, branch, errors) {
860
+ while (i--) {
861
+ if (errors[i]) {
862
+ let j = i;
863
+ while (!branch[j]) j -= 1;
864
+ try {
865
+ return {
866
+ idx: j + 1,
867
+ node: {
868
+ node: await /** @type {import('types').CSRPageNodeLoader } */ (errors[i])(),
869
+ loader: /** @type {import('types').CSRPageNodeLoader } */ (errors[i]),
870
+ data: {},
871
+ server: null,
872
+ shared: null
873
+ }
874
+ };
875
+ } catch (e) {
876
+ continue;
877
+ }
878
+ }
879
+ }
880
+ }
881
+
871
882
  /**
872
883
  * @param {{
873
884
  * status: number;
@@ -1160,44 +1171,26 @@ export function create_client({ target, base, trailing_slash }) {
1160
1171
  const { branch, route } = current;
1161
1172
  if (!route) return;
1162
1173
 
1163
- let i = current.branch.length;
1164
-
1165
- while (i--) {
1166
- if (route.errors[i]) {
1167
- /** @type {import('./types').BranchNode | undefined} */
1168
- let error_loaded;
1169
-
1170
- let j = i;
1171
- while (!branch[j]) j -= 1;
1172
- try {
1173
- error_loaded = {
1174
- node: await /** @type {import('types').CSRPageNodeLoader } */ (route.errors[i])(),
1175
- loader: /** @type {import('types').CSRPageNodeLoader } */ (route.errors[i]),
1176
- data: {},
1177
- server: null,
1178
- shared: null
1179
- };
1180
-
1181
- const navigation_result = await get_navigation_result_from_branch({
1182
- url,
1183
- params: current.params,
1184
- branch: branch.slice(0, j + 1).concat(error_loaded),
1185
- status: 500, // TODO might not be 500?
1186
- error: result.error,
1187
- route
1188
- });
1189
-
1190
- current = navigation_result.state;
1191
-
1192
- const post_update = pre_update();
1193
- root.$set(navigation_result.props);
1194
- post_update();
1195
-
1196
- return;
1197
- } catch (e) {
1198
- continue;
1199
- }
1200
- }
1174
+ const error_load = await load_nearest_error_page(
1175
+ current.branch.length,
1176
+ branch,
1177
+ route.errors
1178
+ );
1179
+ if (error_load) {
1180
+ const navigation_result = await get_navigation_result_from_branch({
1181
+ url,
1182
+ params: current.params,
1183
+ branch: branch.slice(0, error_load.idx).concat(error_load.node),
1184
+ status: 500, // TODO might not be 500?
1185
+ error: result.error,
1186
+ route
1187
+ });
1188
+
1189
+ current = navigation_result.state;
1190
+
1191
+ const post_update = pre_update();
1192
+ root.$set(navigation_result.props);
1193
+ post_update();
1201
1194
  }
1202
1195
  } else if (result.type === 'redirect') {
1203
1196
  goto(result.location, {}, []);
@@ -1206,10 +1199,8 @@ export function create_client({ target, base, trailing_slash }) {
1206
1199
  const props = { form: result.data };
1207
1200
 
1208
1201
  if (result.status !== page.status) {
1209
- props.page = {
1210
- ...page,
1211
- status: result.status
1212
- };
1202
+ page = { ...page, status: result.status };
1203
+ props.page = page;
1213
1204
  }
1214
1205
 
1215
1206
  const post_update = pre_update();