@inertiajs/svelte 2.0.0-beta.1 → 2.0.0-beta.2

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,25 +1,48 @@
1
- <script>import Render, { h } from "./Render.svelte";
2
- import store, {} from "../store";
3
- $: props = resolveProps($store);
4
- function resolveProps({ component, page, key = null }) {
5
- const child = h(component.default, page.props, [], key);
6
- const layout = component.layout;
7
- return layout ? resolveLayout(layout, child, page.props, key) : child;
1
+ <script context="module">import {} from "@inertiajs/core";
2
+ </script>
3
+
4
+ <script>import { router } from "@inertiajs/core";
5
+ import Render, { h } from "./Render.svelte";
6
+ import { setPage } from "../page";
7
+ export let initialComponent;
8
+ export let initialPage;
9
+ export let resolveComponent;
10
+ let component = initialComponent;
11
+ let key = null;
12
+ let page = initialPage;
13
+ let renderProps = resolveRenderProps(component, page, key);
14
+ setPage(page);
15
+ const isServer = typeof window === "undefined";
16
+ if (!isServer) {
17
+ router.init({
18
+ initialPage,
19
+ resolveComponent,
20
+ swapComponent: async (args) => {
21
+ component = args.component;
22
+ page = args.page;
23
+ key = args.preserveState ? key : Date.now();
24
+ renderProps = resolveRenderProps(component, page, key);
25
+ setPage(page);
26
+ }
27
+ });
28
+ }
29
+ function resolveRenderProps(component2, page2, key2 = null) {
30
+ const child = h(component2.default, page2.props, [], key2);
31
+ const layout = component2.layout;
32
+ return layout ? resolveLayout(layout, child, page2.props, key2) : child;
8
33
  }
9
- function resolveLayout(layout, child, pageProps, key) {
34
+ function resolveLayout(layout, child, pageProps, key2) {
10
35
  if (isLayoutFunction(layout)) {
11
36
  return layout(h, child);
12
37
  }
13
38
  if (Array.isArray(layout)) {
14
- return layout.slice().reverse().reduce((currentRender, layoutComponent) => h(layoutComponent, pageProps, [currentRender], key), child);
39
+ return layout.slice().reverse().reduce((currentRender, layoutComponent) => h(layoutComponent, pageProps, [currentRender], key2), child);
15
40
  }
16
- return h(layout, pageProps, child ? [child] : [], key);
41
+ return h(layout, pageProps, child ? [child] : [], key2);
17
42
  }
18
43
  function isLayoutFunction(layout) {
19
44
  return typeof layout === "function" && layout.length === 2 && typeof layout.prototype === "undefined";
20
45
  }
21
46
  </script>
22
47
 
23
- {#if props}
24
- <Render {...props} />
25
- {/if}
48
+ <Render {...renderProps} />
@@ -1,6 +1,17 @@
1
1
  import { SvelteComponent } from "svelte";
2
+ import type { ComponentResolver, ResolvedComponent } from '../types';
3
+ import { type Page } from '@inertiajs/core';
4
+ export interface InertiaAppProps {
5
+ initialComponent: ResolvedComponent;
6
+ initialPage: Page;
7
+ resolveComponent: ComponentResolver;
8
+ }
2
9
  declare const __propDef: {
3
- props: Record<string, never>;
10
+ props: {
11
+ initialComponent: InertiaAppProps["initialComponent"];
12
+ initialPage: InertiaAppProps["initialPage"];
13
+ resolveComponent: InertiaAppProps["resolveComponent"];
14
+ };
4
15
  events: {
5
16
  [evt: string]: CustomEvent<any>;
6
17
  };
@@ -1,12 +1,22 @@
1
1
  <script>import { page } from "../index";
2
+ import { onDestroy } from "svelte";
2
3
  export let data;
3
4
  const keys = Array.isArray(data) ? data : [data];
5
+ let loaded = false;
6
+ const unsubscribe = page.subscribe(({ props }) => {
7
+ window.queueMicrotask(() => {
8
+ loaded = keys.every((key) => typeof props[key] !== "undefined");
9
+ });
10
+ });
11
+ onDestroy(() => {
12
+ unsubscribe();
13
+ });
4
14
  if (!$$slots.fallback) {
5
15
  throw new Error('`<Deferred>` requires a `<svelte:fragment slot="fallback">` slot');
6
16
  }
7
17
  </script>
8
18
 
9
- {#if keys.every((key) => $page.props[key] !== undefined)}
19
+ {#if loaded}
10
20
  <slot />
11
21
  {:else}
12
22
  <slot name="fallback" />
@@ -28,13 +28,13 @@ declare const __propDef: {
28
28
  mouseout: MouseEvent;
29
29
  mouseover: MouseEvent;
30
30
  mouseup: MouseEvent;
31
- 'cancel-token': FocusEvent | UIEvent | Event | MouseEvent | ProgressEvent<EventTarget> | AnimationEvent | InputEvent | CompositionEvent | ClipboardEvent | DragEvent | ErrorEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | SubmitEvent | TouchEvent | TransitionEvent | WheelEvent;
32
- before: FocusEvent | UIEvent | Event | MouseEvent | ProgressEvent<EventTarget> | AnimationEvent | InputEvent | CompositionEvent | ClipboardEvent | DragEvent | ErrorEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | SubmitEvent | TouchEvent | TransitionEvent | WheelEvent;
33
- start: FocusEvent | UIEvent | Event | MouseEvent | ProgressEvent<EventTarget> | AnimationEvent | InputEvent | CompositionEvent | ClipboardEvent | DragEvent | ErrorEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | SubmitEvent | TouchEvent | TransitionEvent | WheelEvent;
31
+ 'cancel-token': Event | FocusEvent | UIEvent | MouseEvent | ProgressEvent<EventTarget> | AnimationEvent | InputEvent | CompositionEvent | ClipboardEvent | DragEvent | ErrorEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | SubmitEvent | TouchEvent | TransitionEvent | WheelEvent;
32
+ before: Event | FocusEvent | UIEvent | MouseEvent | ProgressEvent<EventTarget> | AnimationEvent | InputEvent | CompositionEvent | ClipboardEvent | DragEvent | ErrorEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | SubmitEvent | TouchEvent | TransitionEvent | WheelEvent;
33
+ start: Event | FocusEvent | UIEvent | MouseEvent | ProgressEvent<EventTarget> | AnimationEvent | InputEvent | CompositionEvent | ClipboardEvent | DragEvent | ErrorEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | SubmitEvent | TouchEvent | TransitionEvent | WheelEvent;
34
34
  progress: ProgressEvent<EventTarget>;
35
- finish: FocusEvent | UIEvent | Event | MouseEvent | ProgressEvent<EventTarget> | AnimationEvent | InputEvent | CompositionEvent | ClipboardEvent | DragEvent | ErrorEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | SubmitEvent | TouchEvent | TransitionEvent | WheelEvent;
35
+ finish: Event | FocusEvent | UIEvent | MouseEvent | ProgressEvent<EventTarget> | AnimationEvent | InputEvent | CompositionEvent | ClipboardEvent | DragEvent | ErrorEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | SubmitEvent | TouchEvent | TransitionEvent | WheelEvent;
36
36
  cancel: Event;
37
- success: FocusEvent | UIEvent | Event | MouseEvent | ProgressEvent<EventTarget> | AnimationEvent | InputEvent | CompositionEvent | ClipboardEvent | DragEvent | ErrorEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | SubmitEvent | TouchEvent | TransitionEvent | WheelEvent;
37
+ success: Event | FocusEvent | UIEvent | MouseEvent | ProgressEvent<EventTarget> | AnimationEvent | InputEvent | CompositionEvent | ClipboardEvent | DragEvent | ErrorEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | SubmitEvent | TouchEvent | TransitionEvent | WheelEvent;
38
38
  error: ErrorEvent;
39
39
  } & {
40
40
  [evt: string]: CustomEvent<any>;
@@ -6,7 +6,7 @@ export type RenderProps = {
6
6
  props?: PageProps;
7
7
  children?: RenderProps[];
8
8
  key?: number | null;
9
- } | null;
9
+ };
10
10
  export type RenderFunction = {
11
11
  (component: ComponentType, props?: PageProps, children?: RenderProps[], key?: number | null): RenderProps;
12
12
  (component: ComponentType, children?: RenderProps[], key?: number | null): RenderProps;
@@ -3,7 +3,7 @@ import { onDestroy, onMount } from "svelte";
3
3
  export let data = "";
4
4
  export let params = {};
5
5
  export let buffer = 0;
6
- export let elementTag = "div";
6
+ export let as = "div";
7
7
  export let always = false;
8
8
  let loaded = false;
9
9
  let fetching = false;
@@ -62,7 +62,7 @@ function getReloadParams() {
62
62
  </script>
63
63
 
64
64
  {#if always || !loaded}
65
- <svelte:element this={elementTag} bind:this={el} />
65
+ <svelte:element this={as} bind:this={el} />
66
66
  {/if}
67
67
 
68
68
  {#if loaded}
@@ -5,7 +5,7 @@ declare const __propDef: {
5
5
  data?: string | string[];
6
6
  params?: ReloadOptions;
7
7
  buffer?: number;
8
- elementTag?: keyof HTMLElementTagNameMap;
8
+ as?: keyof HTMLElementTagNameMap;
9
9
  always?: boolean;
10
10
  };
11
11
  events: {
@@ -1,18 +1,25 @@
1
1
  import { type InertiaAppResponse, type Page } from '@inertiajs/core';
2
2
  import type { ComponentType } from 'svelte';
3
- import App from './components/App.svelte';
3
+ import App, { type InertiaAppProps } from './components/App.svelte';
4
4
  import type { ComponentResolver } from './types';
5
+ type SvelteRenderResult = {
6
+ html: string;
7
+ head: string;
8
+ css?: {
9
+ code: string;
10
+ };
11
+ };
12
+ type AppComponent = ComponentType<App> & {
13
+ render: (props: InertiaAppProps) => SvelteRenderResult;
14
+ };
5
15
  interface CreateInertiaAppProps {
6
16
  id?: string;
7
17
  resolve: ComponentResolver;
8
- setup?: (props: {
9
- el: HTMLElement;
10
- App: ComponentType<App>;
11
- props: {
12
- initialPage: Page;
13
- resolveComponent: ComponentResolver;
14
- };
15
- }) => void | App;
18
+ setup: (props: {
19
+ el: HTMLElement | null;
20
+ App: AppComponent;
21
+ props: InertiaAppProps;
22
+ }) => void | App | SvelteRenderResult;
16
23
  progress?: false | {
17
24
  delay?: number;
18
25
  color?: string;
@@ -1,70 +1,29 @@
1
1
  import { router, setupProgress } from '@inertiajs/core';
2
2
  import escape from 'html-escape';
3
- import { version as SVELTE_VERSION } from 'svelte/package.json';
4
- import App from './components/App.svelte';
5
- import store from './store';
3
+ import App, {} from './components/App.svelte';
6
4
  export default async function createInertiaApp({ id = 'app', resolve, setup, progress = {}, page, }) {
7
5
  const isServer = typeof window === 'undefined';
8
6
  const el = isServer ? null : document.getElementById(id);
9
- const initialPage = page || JSON.parse(el?.dataset?.page || '{}');
7
+ const initialPage = page || JSON.parse(el?.dataset.page || '{}');
10
8
  const resolveComponent = (name) => Promise.resolve(resolve(name));
11
- await Promise.all([resolveComponent(initialPage.component), router.decryptHistory().catch(() => { })]).then(([initialComponent]) => {
12
- store.set({
13
- component: initialComponent,
14
- page: initialPage,
15
- key: null,
16
- });
9
+ const [initialComponent] = await Promise.all([
10
+ resolveComponent(initialPage.component),
11
+ router.decryptHistory().catch(() => { }),
12
+ ]);
13
+ const props = { initialPage, initialComponent, resolveComponent };
14
+ const svelteApp = setup({
15
+ el,
16
+ App: App,
17
+ props
17
18
  });
18
19
  if (isServer) {
19
- const isSvelte5 = SVELTE_VERSION.startsWith('5');
20
- const { html, head, css } = await (async () => {
21
- if (isSvelte5) {
22
- const { render } = await dynamicImport('svelte/server');
23
- if (typeof render === 'function') {
24
- return render(App);
25
- }
26
- }
27
- return App.render();
28
- })();
20
+ const { html, head, css } = svelteApp;
29
21
  return {
30
22
  body: `<div data-server-rendered="true" id="${id}" data-page="${escape(JSON.stringify(initialPage))}">${html}</div>`,
31
23
  head: [head, css ? `<style data-vite-css>${css.code}</style>` : ''],
32
24
  };
33
25
  }
34
- if (!el) {
35
- throw new Error(`Element with ID "${id}" not found.`);
36
- }
37
- router.init({
38
- initialPage,
39
- resolveComponent,
40
- swapComponent: async ({ component, page, preserveState }) => {
41
- store.update((current) => ({
42
- component: component,
43
- page,
44
- key: preserveState ? current.key : Date.now(),
45
- }));
46
- },
47
- });
48
26
  if (progress) {
49
27
  setupProgress(progress);
50
28
  }
51
- setup({
52
- el,
53
- App,
54
- props: {
55
- initialPage,
56
- resolveComponent,
57
- },
58
- });
59
- }
60
- // Loads the module dynamically during execution instead of at
61
- // build time. The `@vite-ignore` flag prevents Vite from
62
- // analyzing or pre-bundling this import.
63
- async function dynamicImport(module) {
64
- try {
65
- return await import(/* @vite-ignore */ module);
66
- }
67
- catch {
68
- return null;
69
- }
70
29
  }
package/dist/link.js CHANGED
@@ -2,7 +2,6 @@ import { mergeDataIntoQueryString, router, shouldIntercept, } from '@inertiajs/c
2
2
  function link(node, initialParams = {}) {
3
3
  let inFlightCount = 0;
4
4
  let hoverTimeout;
5
- // Variables initialized and controlled by the "update" function
6
5
  let prefetchModes = [];
7
6
  let cacheForValue;
8
7
  let method;
@@ -36,7 +35,6 @@ function link(node, initialParams = {}) {
36
35
  },
37
36
  click: (event) => {
38
37
  if (shouldIntercept(event)) {
39
- // Let the mouseup event handle the visit
40
38
  event.preventDefault();
41
39
  }
42
40
  },
@@ -53,15 +51,11 @@ function link(node, initialParams = {}) {
53
51
  })();
54
52
  cacheForValue = (() => {
55
53
  if (cacheFor !== 0) {
56
- // If they've provided a value, respect it
57
54
  return cacheFor;
58
55
  }
59
56
  if (prefetchModes.length === 1 && prefetchModes[0] === 'click') {
60
- // If they've only provided a prefetch mode of 'click',
61
- // we should only prefetch for the next request but not keep it around
62
57
  return 0;
63
58
  }
64
- // Otherwise, default to 30 seconds
65
59
  return 30_000;
66
60
  })();
67
61
  method = (params.method?.toLowerCase() || 'get');
@@ -147,7 +141,6 @@ function link(node, initialParams = {}) {
147
141
  removeEventListeners();
148
142
  }
149
143
  update(initialParams);
150
- // TODO: Confirm is this needs to rerun when "prefetchModes" changes
151
144
  if (prefetchModes.includes('mount')) {
152
145
  prefetch();
153
146
  }
package/dist/page.d.ts CHANGED
@@ -1,2 +1,12 @@
1
- declare const page: import("svelte/store").Readable<import("@inertiajs/core").Page<import("@inertiajs/core").PageProps>>;
2
- export default page;
1
+ import { type Page } from '@inertiajs/core';
2
+ export declare const setPage: {
3
+ (this: void, value: Page<import("@inertiajs/core").PageProps>): void;
4
+ (this: void, value: Page<import("@inertiajs/core").PageProps>): void;
5
+ };
6
+ declare const _default: {
7
+ subscribe: {
8
+ (this: void, run: import("svelte/store").Subscriber<Page<import("@inertiajs/core").PageProps>>, invalidate?: import("svelte/store").Invalidator<Page<import("@inertiajs/core").PageProps>> | undefined): import("svelte/store").Unsubscriber;
9
+ (this: void, run: import("svelte/store").Subscriber<Page<import("@inertiajs/core").PageProps>>, invalidate?: import("svelte/store").Invalidator<Page<import("@inertiajs/core").PageProps>> | undefined): import("svelte/store").Unsubscriber;
10
+ };
11
+ };
12
+ export default _default;
package/dist/page.js CHANGED
@@ -1,4 +1,5 @@
1
- import { derived } from 'svelte/store';
2
- import store from './store';
3
- const page = derived(store, ($store) => $store.page);
4
- export default page;
1
+ import {} from '@inertiajs/core';
2
+ import { writable } from 'svelte/store';
3
+ const { set, subscribe } = writable();
4
+ export const setPage = set;
5
+ export default { subscribe };
@@ -5,8 +5,8 @@ export default function usePrefetch(options = {}) {
5
5
  const isPrefetched = writable(false);
6
6
  const isPrefetching = writable(false);
7
7
  const lastUpdatedAt = writable(null);
8
- const cached = router.getCached(window.location.pathname, options);
9
- const inFlight = router.getPrefetching(window.location.pathname, options);
8
+ const cached = typeof window === 'undefined' ? null : router.getCached(window.location.pathname, options);
9
+ const inFlight = typeof window === 'undefined' ? null : router.getPrefetching(window.location.pathname, options);
10
10
  isPrefetched.set(cached !== null);
11
11
  isPrefetching.set(inFlight !== null);
12
12
  lastUpdatedAt.set(cached?.staleTimestamp || null);
@@ -26,8 +26,12 @@ export default function usePrefetch(options = {}) {
26
26
  });
27
27
  });
28
28
  onDestroy(() => {
29
- removePrefetchedListener();
30
- removePrefetchingListener();
29
+ if (removePrefetchedListener) {
30
+ removePrefetchedListener();
31
+ }
32
+ if (removePrefetchingListener) {
33
+ removePrefetchingListener();
34
+ }
31
35
  });
32
36
  return {
33
37
  isPrefetched: readonly(isPrefetched),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inertiajs/svelte",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.2",
4
4
  "license": "MIT",
5
5
  "description": "The Svelte adapter for Inertia.js",
6
6
  "contributors": [
@@ -43,7 +43,7 @@
43
43
  "svelte": "^4.0.0 || ^5.0.0 || ^5.0.0-next.244"
44
44
  },
45
45
  "dependencies": {
46
- "@inertiajs/core": "2.0.0-beta.1",
46
+ "@inertiajs/core": "2.0.0-beta.2",
47
47
  "html-escape": "^2.0.0",
48
48
  "lodash": "^4.5.0"
49
49
  },
package/dist/store.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import type { Page } from '@inertiajs/core';
2
- import type { ResolvedComponent } from './types';
3
- export interface InertiaStore {
4
- component: ResolvedComponent;
5
- page: Page;
6
- key: number | null;
7
- }
8
- declare const store: import("svelte/store").Writable<InertiaStore>;
9
- export default store;
package/dist/store.js DELETED
@@ -1,7 +0,0 @@
1
- import { writable } from 'svelte/store';
2
- const store = writable({
3
- component: null,
4
- page: null,
5
- key: null,
6
- });
7
- export default store;