sv-router 0.3.0 → 0.4.0

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": "sv-router",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Modern Svelte Routing",
5
5
  "keywords": [
6
6
  "svelte",
@@ -16,6 +16,9 @@ export let params = $state({ value: {} });
16
16
 
17
17
  export let location = $state(updatedLocation());
18
18
 
19
+ let navigationIndex = 0;
20
+ let pendingNavigationIndex = 0;
21
+
19
22
  /** @type {{ name?: string }} */
20
23
  export const base = {
21
24
  name: undefined,
@@ -90,6 +93,10 @@ export async function onNavigate(path, options = {}) {
90
93
  if (!routes) {
91
94
  throw new Error('Router not initialized: `createRouter` was not called.');
92
95
  }
96
+
97
+ navigationIndex++;
98
+ const currentNavigationIndex = navigationIndex;
99
+
93
100
  let matchPath = path || globalThis.location.pathname;
94
101
  if (base.name && matchPath.startsWith(base.name)) {
95
102
  matchPath = matchPath.slice(base.name.length) || '/';
@@ -98,14 +105,22 @@ export async function onNavigate(path, options = {}) {
98
105
 
99
106
  for (const { beforeLoad } of hooks) {
100
107
  try {
108
+ pendingNavigationIndex = currentNavigationIndex;
101
109
  await beforeLoad?.();
102
110
  } catch {
103
111
  return;
104
112
  }
105
113
  }
106
114
 
107
- componentTree.value = await resolveRouteComponents(match ? [...layouts, match] : layouts);
108
- params.value = newParams || {};
115
+ const fromBeforeLoadHook = new Error().stack?.includes('beforeLoad');
116
+
117
+ const routeComponents = await resolveRouteComponents(match ? [...layouts, match] : layouts);
118
+ if (
119
+ navigationIndex !== currentNavigationIndex ||
120
+ (fromBeforeLoadHook && pendingNavigationIndex + 1 !== currentNavigationIndex)
121
+ ) {
122
+ return;
123
+ }
109
124
 
110
125
  if (path) {
111
126
  if (options.search) path += options.search;
@@ -115,6 +130,14 @@ export async function onNavigate(path, options = {}) {
115
130
  globalThis.history[historyMethod](options.state || {}, '', to);
116
131
  }
117
132
 
133
+ if (options.viewTransition && document.startViewTransition !== undefined) {
134
+ document.startViewTransition(() => {
135
+ componentTree.value = routeComponents;
136
+ });
137
+ } else {
138
+ componentTree.value = routeComponents;
139
+ }
140
+ params.value = newParams || {};
118
141
  syncSearchParams();
119
142
  Object.assign(location, updatedLocation());
120
143
 
@@ -139,13 +162,14 @@ export function onGlobalClick(event) {
139
162
  if (url.origin !== currentOrigin) return;
140
163
 
141
164
  event.preventDefault();
142
- const { replace, state, scrollToTop } = anchor.dataset;
165
+ const { replace, state, scrollToTop, viewTransition } = anchor.dataset;
143
166
  onNavigate(url.pathname, {
144
167
  replace: replace === '' || replace === 'true',
145
168
  search: url.search,
146
169
  state,
147
170
  hash: url.hash,
148
171
  scrollToTop: scrollToTop === 'false' ? false : /** @type ScrollBehavior */ (scrollToTop),
172
+ viewTransition: viewTransition === '' || viewTransition === 'true',
149
173
  });
150
174
  }
151
175
 
package/src/index.d.ts CHANGED
@@ -166,6 +166,7 @@ export type NavigateOptions =
166
166
  state?: string;
167
167
  hash?: string;
168
168
  scrollToTop?: ScrollBehavior | false;
169
+ viewTransition?: boolean;
169
170
  }
170
171
  | undefined;
171
172
 
@@ -7,9 +7,9 @@ import { writeRouterCode } from '../gen/write-router-code.js';
7
7
  * @returns {import('vite').Plugin}
8
8
  */
9
9
  export function router(options) {
10
- if (options?.allLazy) genConfig.allLazy = options.allLazy;
11
- if (options?.js) genConfig.routesInJs = options.js;
12
- if (options?.path) genConfig.routesPath = options.path;
10
+ genConfig.allLazy = options?.allLazy || false;
11
+ genConfig.routesInJs = options?.js || false;
12
+ genConfig.routesPath = options?.path || 'src/routes';
13
13
 
14
14
  return {
15
15
  name: 'sv-router',