@inertiajs/svelte 3.0.0-beta.2 → 3.0.0-beta.3

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.
@@ -35,6 +35,10 @@
35
35
  let page = $state({ ...initialPage, flash: initialPage.flash ?? {} })
36
36
  let renderProps = $derived.by<RenderProps>(() => resolveRenderProps(component, page, key))
37
37
 
38
+ // Synchronous initialization so the global page store is populated during SSR
39
+ // ($effect.pre does not run during Svelte 5 SSR)
40
+ setPage(page)
41
+
38
42
  // Reactively update the global page state when local page state changes
39
43
  $effect.pre(() => {
40
44
  setPage(page)
@@ -1,4 +1,4 @@
1
- import { type CreateInertiaAppOptions, type CreateInertiaAppOptionsForCSR, type InertiaAppSSRResponse, type Page, type PageProps } from '@inertiajs/core';
1
+ import { type CreateInertiaAppOptions, type CreateInertiaAppOptionsForCSR, type InertiaAppSSRResponse, type Page, type PageProps, type SharedPageProps } from '@inertiajs/core';
2
2
  import App, { type InertiaAppProps } from './components/App.svelte';
3
3
  import type { ComponentResolver, SvelteInertiaAppConfig } from './types';
4
4
  type SvelteRenderResult = {
@@ -10,14 +10,22 @@ type SetupOptions<SharedProps extends PageProps> = {
10
10
  App: typeof App;
11
11
  props: InertiaAppProps<SharedProps>;
12
12
  };
13
- type InertiaAppOptionsForCSR<SharedProps extends PageProps> = CreateInertiaAppOptionsForCSR<SharedProps, ComponentResolver, SetupOptions<SharedProps>, SvelteRenderResult | void, SvelteInertiaAppConfig>;
13
+ type InertiaAppOptionsForCSR<SharedProps extends PageProps> = CreateInertiaAppOptionsForCSR<SharedProps, ComponentResolver, SetupOptions<SharedProps>, SvelteRenderResult | void, SvelteInertiaAppConfig> & {
14
+ withApp?: (context: Map<any, any>, options: {
15
+ ssr: boolean;
16
+ }) => void;
17
+ };
14
18
  type InertiaAppOptionsAuto<SharedProps extends PageProps> = CreateInertiaAppOptions<ComponentResolver, SetupOptions<SharedProps>, SvelteRenderResult | void, SvelteInertiaAppConfig> & {
15
19
  page?: Page<SharedProps>;
20
+ withApp?: (context: Map<any, any>, options: {
21
+ ssr: boolean;
22
+ }) => void;
16
23
  };
17
24
  type SvelteServerRender = (component: typeof App, options: {
18
25
  props: InertiaAppProps<PageProps>;
26
+ context?: Map<any, any>;
19
27
  }) => SvelteRenderResult;
20
28
  type RenderFunction<SharedProps extends PageProps> = (page: Page<SharedProps>, render: SvelteServerRender) => Promise<InertiaAppSSRResponse>;
21
- export default function createInertiaApp<SharedProps extends PageProps = PageProps>(options: InertiaAppOptionsForCSR<SharedProps>): Promise<InertiaAppSSRResponse | void>;
22
- export default function createInertiaApp<SharedProps extends PageProps = PageProps>(options?: InertiaAppOptionsAuto<SharedProps>): Promise<void | RenderFunction<SharedProps>>;
29
+ export default function createInertiaApp<SharedProps extends PageProps = PageProps & SharedPageProps>(options: InertiaAppOptionsForCSR<SharedProps>): Promise<InertiaAppSSRResponse | void>;
30
+ export default function createInertiaApp<SharedProps extends PageProps = PageProps & SharedPageProps>(options?: InertiaAppOptionsAuto<SharedProps>): Promise<void | RenderFunction<SharedProps>>;
23
31
  export {};
@@ -2,7 +2,7 @@ import { buildSSRBody, getInitialPageFromDOM, http as httpModule, router, setupP
2
2
  import { hydrate, mount } from 'svelte';
3
3
  import App, {} from './components/App.svelte';
4
4
  import { config } from './index';
5
- export default async function createInertiaApp({ id = 'app', resolve, setup, progress = {}, page, defaults = {}, http, layout, } = {}) {
5
+ export default async function createInertiaApp({ id = 'app', resolve, setup, progress = {}, page, defaults = {}, http, layout, withApp, } = {}) {
6
6
  config.replace(defaults);
7
7
  if (http) {
8
8
  httpModule.setClient(http);
@@ -27,7 +27,11 @@ export default async function createInertiaApp({ id = 'app', resolve, setup, pro
27
27
  svelteApp = result;
28
28
  }
29
29
  else {
30
- svelteApp = render(App, { props });
30
+ const context = new Map();
31
+ if (withApp) {
32
+ withApp(context, { ssr: true });
33
+ }
34
+ svelteApp = render(App, { props, context });
31
35
  }
32
36
  const body = buildSSRBody(id, page, svelteApp.body);
33
37
  return {
@@ -60,11 +64,17 @@ export default async function createInertiaApp({ id = 'app', resolve, setup, pro
60
64
  if (setup) {
61
65
  await setup({ el: target, App, props });
62
66
  }
63
- else if (target.hasAttribute('data-server-rendered')) {
64
- hydrate(App, { target, props });
65
- }
66
67
  else {
67
- mount(App, { target, props });
68
+ const context = new Map();
69
+ if (withApp) {
70
+ withApp(context, { ssr: false });
71
+ }
72
+ if (target.hasAttribute('data-server-rendered')) {
73
+ hydrate(App, { target, props, context });
74
+ }
75
+ else {
76
+ mount(App, { target, props, context });
77
+ }
68
78
  }
69
79
  if (progress) {
70
80
  setupProgress(progress);
@@ -1,6 +1,5 @@
1
- import { type Readable } from 'svelte/store';
2
1
  export declare function setLayoutProps(props: Record<string, unknown>): void;
3
2
  export declare function setLayoutPropsFor(name: string, props: Record<string, unknown>): void;
4
3
  export declare function resetLayoutProps(): void;
5
4
  export declare const LAYOUT_CONTEXT_KEY: unique symbol;
6
- export declare function useLayoutProps<T extends Record<string, unknown>>(defaults: T): Readable<T>;
5
+ export declare function useLayoutProps<T extends Record<string, unknown>>(defaults: T): T;
@@ -1,7 +1,15 @@
1
1
  import { createLayoutPropsStore, mergeLayoutProps } from '@inertiajs/core';
2
2
  import { getContext } from 'svelte';
3
- import { readable } from 'svelte/store';
4
3
  const store = createLayoutPropsStore();
4
+ const storeState = $state({
5
+ shared: {},
6
+ named: {},
7
+ });
8
+ store.subscribe(() => {
9
+ const snapshot = store.get();
10
+ storeState.shared = snapshot.shared;
11
+ storeState.named = snapshot.named;
12
+ });
5
13
  export function setLayoutProps(props) {
6
14
  store.set(props);
7
15
  }
@@ -17,9 +25,12 @@ export function useLayoutProps(defaults) {
17
25
  const resolve = () => {
18
26
  const staticProps = context?.staticProps ?? {};
19
27
  const name = context?.name;
20
- const { shared, named } = store.get();
21
- const dynamicProps = name ? { ...shared, ...named[name] } : shared;
28
+ const dynamicProps = name ? { ...storeState.shared, ...(storeState.named[name] ?? {}) } : storeState.shared;
22
29
  return mergeLayoutProps(defaults, staticProps, dynamicProps);
23
30
  };
24
- return readable(resolve(), (set) => store.subscribe(() => set(resolve())));
31
+ const state = $state(resolve());
32
+ $effect.pre(() => {
33
+ Object.assign(state, resolve());
34
+ });
35
+ return state;
25
36
  }
package/dist/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { type Page } from '@inertiajs/core';
1
+ import { type Page, type SharedPageProps } from '@inertiajs/core';
2
2
  import type { Component } from 'svelte';
3
3
  import type { RenderFunction, RenderProps } from './components/Render.svelte';
4
- export type ComponentResolver = (name: string, page?: Page) => ResolvedComponent | Promise<ResolvedComponent>;
4
+ export type ComponentResolver = (name: string, page?: Page<SharedPageProps>) => ResolvedComponent | Promise<ResolvedComponent>;
5
5
  export type LayoutResolver = (h: RenderFunction, page: RenderProps) => RenderProps;
6
6
  export type LayoutTuple = [Component, Record<string, unknown>?];
7
7
  export type LayoutObject = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inertiajs/svelte",
3
- "version": "3.0.0-beta.2",
3
+ "version": "3.0.0-beta.3",
4
4
  "license": "MIT",
5
5
  "description": "The Svelte adapter for Inertia.js",
6
6
  "contributors": [
@@ -56,7 +56,7 @@
56
56
  "@types/lodash-es": "^4.17.12",
57
57
  "laravel-precognition": "2.0.0-beta.3",
58
58
  "lodash-es": "^4.17.23",
59
- "@inertiajs/core": "3.0.0-beta.2"
59
+ "@inertiajs/core": "3.0.0-beta.3"
60
60
  },
61
61
  "scripts": {
62
62
  "build": "pnpm package && svelte-check --tsconfig ./tsconfig.json && publint",
@@ -389,8 +389,8 @@ const layout = useLayoutProps({
389
389
  let { children } = $props()
390
390
  </script>
391
391
 
392
- <header>{$layout.title}</header>
393
- {#if $layout.showSidebar}
392
+ <header>{layout.title}</header>
393
+ {#if layout.showSidebar}
394
394
  <aside>Sidebar</aside>
395
395
  {/if}
396
396
  <main>