@adonisjs/inertia 1.0.0-2 → 1.0.0-22

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.
Files changed (49) hide show
  1. package/build/app.css.stub +39 -0
  2. package/build/chunk-PJEYAPJB.js +254 -0
  3. package/build/{stubs/config.stub → config.stub} +10 -2
  4. package/build/index.d.ts +3 -2
  5. package/build/index.js +214 -36
  6. package/build/providers/inertia_provider.d.ts +14 -1
  7. package/build/providers/inertia_provider.js +29 -16
  8. package/build/react/app.tsx.stub +35 -0
  9. package/build/react/errors/not_found.tsx.stub +14 -0
  10. package/build/react/errors/server_error.tsx.stub +14 -0
  11. package/build/react/home.tsx.stub +21 -0
  12. package/build/react/root.edge.stub +22 -0
  13. package/build/react/ssr.tsx.stub +17 -0
  14. package/build/react/tsconfig.json.stub +15 -0
  15. package/build/solid/app.tsx.stub +35 -0
  16. package/build/solid/errors/not_found.tsx.stub +14 -0
  17. package/build/solid/errors/server_error.tsx.stub +14 -0
  18. package/build/solid/home.tsx.stub +18 -0
  19. package/build/solid/root.edge.stub +21 -0
  20. package/build/solid/ssr.tsx.stub +19 -0
  21. package/build/solid/tsconfig.json.stub +16 -0
  22. package/build/src/helpers.d.ts +12 -0
  23. package/build/src/helpers.js +14 -0
  24. package/build/src/inertia_middleware.d.ts +13 -9
  25. package/build/src/inertia_middleware.js +1 -1
  26. package/build/src/plugins/edge/plugin.js +85 -0
  27. package/build/src/plugins/{api_client.d.ts → japa/api_client.d.ts} +2 -1
  28. package/build/src/plugins/{api_client.js → japa/api_client.js} +1 -1
  29. package/build/src/plugins/vite.d.ts +26 -0
  30. package/build/src/plugins/vite.js +36 -0
  31. package/build/src/types.d.ts +61 -2
  32. package/build/svelte/app.ts.stub +30 -0
  33. package/build/svelte/errors/not_found.svelte.stub +10 -0
  34. package/build/svelte/errors/server_error.svelte.stub +14 -0
  35. package/build/svelte/home.svelte.stub +19 -0
  36. package/build/svelte/root.edge.stub +21 -0
  37. package/build/svelte/ssr.ts.stub +15 -0
  38. package/build/svelte/tsconfig.json.stub +14 -0
  39. package/build/vue/app.ts.stub +38 -0
  40. package/build/vue/errors/not_found.vue.stub +10 -0
  41. package/build/vue/errors/server_error.vue.stub +14 -0
  42. package/build/vue/home.vue.stub +21 -0
  43. package/build/vue/root.edge.stub +21 -0
  44. package/build/vue/ssr.ts.stub +22 -0
  45. package/build/vue/tsconfig.json.stub +16 -0
  46. package/package.json +52 -43
  47. package/build/chunk-GDULL3NT.js +0 -123
  48. package/build/src/plugins/edge.js +0 -41
  49. /package/build/src/plugins/{edge.d.ts → edge/plugin.d.ts} +0 -0
@@ -1,3 +1,4 @@
1
+ import { Serialize } from '@tuyau/utils/types';
1
2
  import { HttpContext } from '@adonisjs/core/http';
2
3
 
3
4
  /**
@@ -44,9 +45,13 @@ type AssetsVersion = string | number | undefined;
44
45
  interface InertiaConfig {
45
46
  /**
46
47
  * Path to the Edge view that will be used as the root view for Inertia responses.
47
- * @default root (resources/views/root.edge)
48
+ * @default root (resources/views/inertia_layout.edge)
48
49
  */
49
50
  rootView?: string;
51
+ /**
52
+ * Path to your client-side entrypoint file.
53
+ */
54
+ entrypoint?: string;
50
55
  /**
51
56
  * The version of your assets. Every client request will be checked against this version.
52
57
  * If the version is not the same, the client will do a full reload.
@@ -56,6 +61,27 @@ interface InertiaConfig {
56
61
  * Data that should be shared with all rendered pages
57
62
  */
58
63
  sharedData?: SharedData;
64
+ /**
65
+ * Options to configure SSR
66
+ */
67
+ ssr?: {
68
+ /**
69
+ * Enable or disable SSR
70
+ */
71
+ enabled: boolean;
72
+ /**
73
+ * List of components that should be rendered on the server
74
+ */
75
+ pages?: string[] | ((ctx: HttpContext, page: string) => MaybePromise<boolean>);
76
+ /**
77
+ * Path to the SSR entrypoint file
78
+ */
79
+ entrypoint?: string;
80
+ /**
81
+ * Path to the SSR bundled file that will be used in production
82
+ */
83
+ bundle?: string;
84
+ };
59
85
  }
60
86
  /**
61
87
  * Resolved inertia configuration
@@ -64,6 +90,39 @@ interface ResolvedConfig {
64
90
  rootView: string;
65
91
  versionCache: VersionCache;
66
92
  sharedData: SharedData;
93
+ entrypoint: string;
94
+ ssr: {
95
+ enabled: boolean;
96
+ entrypoint: string;
97
+ pages?: string[] | ((ctx: HttpContext, page: string) => MaybePromise<boolean>);
98
+ bundle: string;
99
+ };
100
+ }
101
+ interface PageObject<TPageProps extends PageProps = PageProps> {
102
+ component: string;
103
+ version: string | number;
104
+ props: TPageProps;
105
+ url: string;
106
+ ssrHead?: string;
107
+ ssrBody?: string;
67
108
  }
109
+ /**
110
+ * Helper for infering the page props from a Controller method that returns
111
+ * inertia.render
112
+ *
113
+ * ```ts
114
+ * // Your Adonis Controller
115
+ * class MyController {
116
+ * index() {
117
+ * return inertia.render('foo', { foo: 1 })
118
+ * }
119
+ * }
120
+ *
121
+ * // Your React component
122
+ * export default MyReactComponent(props: InferPageProps<Controller, 'index'>) {
123
+ * }
124
+ * ```
125
+ */
126
+ type InferPageProps<Controller, Method extends keyof Controller> = Controller[Method] extends (...args: any[]) => any ? Serialize<Exclude<Awaited<ReturnType<Controller[Method]>>, string>['props']> : never;
68
127
 
69
- export type { AssetsVersion, Data, InertiaConfig, MaybePromise, PageProps, ResolvedConfig, SharedData, SharedDatumFactory };
128
+ export type { AssetsVersion, Data, InertiaConfig, InferPageProps, MaybePromise, PageObject, PageProps, ResolvedConfig, SharedData, SharedDatumFactory };
@@ -0,0 +1,30 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/app/app.ts') })
3
+ }}}
4
+ import '../css/app.css';
5
+
6
+ import { createInertiaApp } from '@inertiajs/svelte'
7
+ import { resolvePageComponent } from '@adonisjs/inertia/helpers'
8
+
9
+ const appName = import.meta.env.VITE_APP_NAME || 'AdonisJS'
10
+
11
+ createInertiaApp({
12
+ progress: { color: '#5468FF' },
13
+
14
+ title: (title) => {{ '`${title} - ${appName}`' }},
15
+
16
+ resolve: (name) => {
17
+ return resolvePageComponent(
18
+ {{ '`../pages/${name}.svelte`' }},
19
+ import.meta.glob('../pages/**/*.svelte'),
20
+ )
21
+ },
22
+
23
+ setup({ el, App, props }) {
24
+ {{#if ssr}}
25
+ new App({ target: el, props, hydrate: true })
26
+ {{#else}}
27
+ new App({ target: el, props })
28
+ {{/if}}
29
+ },
30
+ })
@@ -0,0 +1,10 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/pages/errors/not_found.svelte') })
3
+ }}}
4
+ <div>
5
+ <div class="container">
6
+ <div class="title">Page not found</div>
7
+
8
+ <span>This page does not exist.</span>
9
+ </div>
10
+ </div>
@@ -0,0 +1,14 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/pages/errors/server_error.svelte') })
3
+ }}}
4
+ <script>
5
+ export let error
6
+ </script>
7
+
8
+ <div>
9
+ <div class="container">
10
+ <div class="title">Server Error</div>
11
+
12
+ <span>{error.message}</span>
13
+ </div>
14
+ </div>
@@ -0,0 +1,19 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/pages/home.svelte') })
3
+ }}}
4
+ <script>
5
+ export let version
6
+ </script>
7
+
8
+ <svelte:head>
9
+ <title>Homepage</title>
10
+ </svelte:head>
11
+
12
+ <div class="container">
13
+ <div class="title">AdonisJS \{ version \} x Inertia x Svelte</div>
14
+
15
+ <span>
16
+ Learn more about AdonisJS and Inertia.js by visiting the
17
+ <a href="https://docs.adonisjs.com/guides/inertia">AdonisJS documentation</a>.
18
+ </span>
19
+ </div>
@@ -0,0 +1,21 @@
1
+ {{{
2
+ exports({ to: app.makePath('resources/views/inertia_layout.edge') })
3
+ }}}
4
+ <!DOCTYPE html>
5
+ <html>
6
+
7
+ <head>
8
+ <meta charset="utf-8">
9
+ <meta name="viewport" content="width=device-width, initial-scale=1">
10
+
11
+ <title inertia>AdonisJS x Inertia x Svelte</title>
12
+
13
+ {{ "@vite(['inertia/app/app.ts', `inertia/pages/${page.component}.svelte`])" }}
14
+ @inertiaHead()
15
+ </head>
16
+
17
+ <body>
18
+ @inertia()
19
+ </body>
20
+
21
+ </html>
@@ -0,0 +1,15 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/app/ssr.ts') })
3
+ }}}
4
+
5
+ import { createInertiaApp } from '@inertiajs/svelte'
6
+
7
+ export default function render(page: any) {
8
+ return createInertiaApp({
9
+ page,
10
+ resolve: (name) => {
11
+ const pages = import.meta.glob('../pages/**/*.svelte', { eager: true })
12
+ {{ 'return pages[`../pages/${name}.svelte`]' }}
13
+ }
14
+ })
15
+ }
@@ -0,0 +1,14 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/tsconfig.json') })
3
+ }}}
4
+ {
5
+ "extends": "@adonisjs/tsconfig/tsconfig.client.json",
6
+ "compilerOptions": {
7
+ "baseUrl": ".",
8
+ "module": "ESNext",
9
+ "paths": {
10
+ "~/*": ["./*"],
11
+ },
12
+ },
13
+ "include": ["./**/*.ts", "./**/*.svelte"],
14
+ }
@@ -0,0 +1,38 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/app/app.ts') })
3
+ }}}
4
+ import '../css/app.css';
5
+
6
+ {{#if ssr}}
7
+ import { createSSRApp, h } from 'vue'
8
+ {{#else}}
9
+ import { createApp, h } from 'vue'
10
+ {{/if}}
11
+ import type { DefineComponent } from 'vue'
12
+ import { createInertiaApp } from '@inertiajs/vue3'
13
+ import { resolvePageComponent } from '@adonisjs/inertia/helpers'
14
+
15
+ const appName = import.meta.env.VITE_APP_NAME || 'AdonisJS'
16
+
17
+ createInertiaApp({
18
+ progress: { color: '#5468FF' },
19
+
20
+ title: (title) => {{ '`${title} - ${appName}`' }},
21
+
22
+ resolve: (name) => {
23
+ return resolvePageComponent(
24
+ {{ '`../pages/${name}.vue`' }},
25
+ import.meta.glob<DefineComponent>('../pages/**/*.vue'),
26
+ )
27
+ },
28
+
29
+ setup({ el, App, props, plugin }) {
30
+ {{#if ssr}}
31
+ createSSRApp({ render: () => h(App, props) })
32
+ {{#else}}
33
+ createApp({ render: () => h(App, props) })
34
+ {{/if}}
35
+ .use(plugin)
36
+ .mount(el)
37
+ },
38
+ })
@@ -0,0 +1,10 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/pages/errors/not_found.vue') })
3
+ }}}
4
+ <template>
5
+ <div class="container">
6
+ <div class="title">Page not found</div>
7
+
8
+ <span>This page does not exist.</span>
9
+ </div>
10
+ </template>
@@ -0,0 +1,14 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/pages/errors/server_error.vue') })
3
+ }}}
4
+ <script setup lang="ts">
5
+ defineProps<{ error: any }>();
6
+ </script>
7
+
8
+ <template>
9
+ <div class="container">
10
+ <div class="title">Server Error</div>
11
+
12
+ <span>\{\{ error.message \}\}</span>
13
+ </div>
14
+ </template>
@@ -0,0 +1,21 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/pages/home.vue') })
3
+ }}}
4
+ <script setup lang="ts">
5
+ import { Head } from '@inertiajs/vue3'
6
+
7
+ defineProps<{ version: number }>()
8
+ </script>
9
+
10
+ <template>
11
+ <Head title="Homepage" />
12
+
13
+ <div class="container">
14
+ <div class="title">AdonisJS \{\{ version \}\} x Inertia x Vue.js</div>
15
+
16
+ <span>
17
+ Learn more about AdonisJS and Inertia.js by visiting the
18
+ <a href="https://docs.adonisjs.com/guides/inertia">AdonisJS documentation</a>.
19
+ </span>
20
+ </div>
21
+ </template>
@@ -0,0 +1,21 @@
1
+ {{{
2
+ exports({ to: app.makePath('resources/views/inertia_layout.edge') })
3
+ }}}
4
+ <!DOCTYPE html>
5
+ <html>
6
+
7
+ <head>
8
+ <meta charset="utf-8">
9
+ <meta name="viewport" content="width=device-width, initial-scale=1">
10
+
11
+ <title inertia>AdonisJS x Inertia x VueJS</title>
12
+
13
+ {{ "@vite(['inertia/app/app.ts', `inertia/pages/${page.component}.vue`])" }}
14
+ @inertiaHead()
15
+ </head>
16
+
17
+ <body>
18
+ @inertia()
19
+ </body>
20
+
21
+ </html>
@@ -0,0 +1,22 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/app/ssr.ts') })
3
+ }}}
4
+
5
+ import { createInertiaApp } from '@inertiajs/vue3'
6
+ import { renderToString } from '@vue/server-renderer'
7
+ import { createSSRApp, h, type DefineComponent } from 'vue'
8
+
9
+ export default function render(page: any) {
10
+ return createInertiaApp({
11
+ page,
12
+ render: renderToString,
13
+ resolve: (name) => {
14
+ const pages = import.meta.glob<DefineComponent>('../pages/**/*.vue', { eager: true })
15
+ {{ 'return pages[`../pages/${name}.vue`]' }}
16
+ },
17
+
18
+ setup({ App, props, plugin }) {
19
+ return createSSRApp({ render: () => h(App, props) }).use(plugin)
20
+ },
21
+ })
22
+ }
@@ -0,0 +1,16 @@
1
+ {{{
2
+ exports({ to: app.makePath('inertia/tsconfig.json') })
3
+ }}}
4
+ {
5
+ "extends": "@adonisjs/tsconfig/tsconfig.client.json",
6
+ "compilerOptions": {
7
+ "baseUrl": ".",
8
+ "jsx": "preserve",
9
+ "module": "ESNext",
10
+ "jsxImportSource": "vue",
11
+ "paths": {
12
+ "~/*": ["./*"],
13
+ },
14
+ },
15
+ "include": ["./**/*.ts", "./**/*.vue"],
16
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@adonisjs/inertia",
3
3
  "description": "Official Inertia.js adapter for AdonisJS",
4
- "version": "1.0.0-2",
4
+ "version": "1.0.0-22",
5
5
  "engines": {
6
6
  "node": ">=18.16.0"
7
7
  },
@@ -16,12 +16,14 @@
16
16
  "./services/main": "./build/services/inertia.js",
17
17
  "./inertia_middleware": "./build/src/inertia_middleware.js",
18
18
  "./inertia_provider": "./build/providers/inertia_provider.js",
19
- "./plugins/edge": "./build/src/plugins/edge.js",
20
- "./plugins/api_client": "./build/src/plugins/api_client.js"
19
+ "./plugins/edge": "./build/src/plugins/edge/plugin.js",
20
+ "./plugins/api_client": "./build/src/plugins/japa/api_client.js",
21
+ "./client": "./build/src/plugins/vite.js",
22
+ "./helpers": "./build/src/helpers.js"
21
23
  },
22
24
  "scripts": {
23
25
  "clean": "del-cli build",
24
- "copy:templates": "copyfiles \"stubs/**/*.stub\" build",
26
+ "copy:templates": "copyfiles --up 1 \"stubs/**/*.stub\" build",
25
27
  "typecheck": "tsc --noEmit",
26
28
  "lint": "eslint . --ext=.ts",
27
29
  "format": "prettier --write .",
@@ -36,55 +38,60 @@
36
38
  "prepublishOnly": "npm run build"
37
39
  },
38
40
  "devDependencies": {
39
- "@adonisjs/assembler": "6.1.3-25",
40
- "@adonisjs/core": "6.1.5-31",
41
- "@adonisjs/eslint-config": "^1.1.8",
42
- "@adonisjs/prettier-config": "^1.1.8",
43
- "@adonisjs/session": "7.0.0-13",
44
- "@adonisjs/tsconfig": "^1.1.8",
45
- "@japa/api-client": "^2.0.1",
46
- "@japa/assert": "2.0.1",
47
- "@japa/expect-type": "^2.0.0",
48
- "@japa/file-system": "^2.0.1",
49
- "@japa/plugin-adonisjs": "^2.0.1",
50
- "@japa/runner": "3.0.5",
51
- "@swc/core": "^1.3.96",
52
- "@types/node": "^20.9.0",
53
- "@types/qs": "^6.9.10",
54
- "@types/supertest": "^2.0.16",
55
- "c8": "^8.0.1",
41
+ "@adonisjs/assembler": "^7.2.3",
42
+ "@adonisjs/core": "6.3.1",
43
+ "@adonisjs/eslint-config": "^1.3.0",
44
+ "@adonisjs/prettier-config": "^1.3.0",
45
+ "@adonisjs/session": "7.3.0",
46
+ "@adonisjs/tsconfig": "^1.3.0",
47
+ "@adonisjs/vite": "^3.0.0-8",
48
+ "@japa/api-client": "^2.0.2",
49
+ "@japa/assert": "2.1.0",
50
+ "@japa/expect-type": "^2.0.1",
51
+ "@japa/file-system": "^2.2.0",
52
+ "@japa/plugin-adonisjs": "^3.0.0",
53
+ "@japa/runner": "3.1.1",
54
+ "@japa/snapshot": "^2.0.4",
55
+ "@swc/core": "^1.4.8",
56
+ "@tuyau/utils": "^0.0.1",
57
+ "@types/node": "^20.11.30",
58
+ "@types/qs": "^6.9.14",
59
+ "@types/supertest": "^6.0.2",
60
+ "@vavite/multibuild": "^4.1.1",
61
+ "c8": "^9.1.0",
56
62
  "copyfiles": "^2.4.1",
57
63
  "del-cli": "^5.1.0",
58
- "edge.js": "^6.0.0",
59
- "eslint": "^8.53.0",
60
- "get-port": "^7.0.0",
61
- "np": "^8.0.4",
62
- "prettier": "^3.0.3",
63
- "supertest": "^6.3.3",
64
- "tinybench": "^2.5.1",
65
- "ts-node": "^10.9.1",
66
- "tsup": "^7.3.0",
67
- "typescript": "~5.2.2"
64
+ "edge-parser": "^9.0.2",
65
+ "edge.js": "^6.0.2",
66
+ "eslint": "^8.57.0",
67
+ "get-port": "^7.1.0",
68
+ "np": "^10.0.2",
69
+ "prettier": "^3.2.5",
70
+ "supertest": "^6.3.4",
71
+ "tinybench": "^2.6.0",
72
+ "ts-node": "^10.9.2",
73
+ "tsup": "^8.0.2",
74
+ "typescript": "~5.4.3",
75
+ "vite": "^5.2.6"
68
76
  },
69
77
  "dependencies": {
70
- "@poppinss/utils": "^6.5.1",
78
+ "@poppinss/utils": "^6.7.2",
71
79
  "crc-32": "^1.2.2",
72
- "html-entities": "^2.4.0",
80
+ "edge-error": "^4.0.1",
81
+ "html-entities": "^2.5.2",
82
+ "locate-path": "^7.2.0",
73
83
  "qs": "^6.11.2"
74
84
  },
75
85
  "peerDependencies": {
76
- "@adonisjs/assembler": "6.1.3-25",
77
- "@adonisjs/core": "6.1.5-31",
78
- "@adonisjs/session": "7.0.0-13",
79
- "@japa/api-client": "^2.0.1",
86
+ "@adonisjs/core": "^6.2.0",
87
+ "@adonisjs/session": "^7.0.0",
88
+ "@adonisjs/vite": "^3.0.0-8",
89
+ "@japa/api-client": "^2.0.0",
80
90
  "edge.js": "^6.0.0"
81
91
  },
82
92
  "peerDependenciesMeta": {
83
- "@adonisjs/assembler": {
84
- "optional": false
85
- },
86
93
  "@japa/api-client": {
87
- "optional": false
94
+ "optional": true
88
95
  }
89
96
  },
90
97
  "author": "Julien Ripouteau <julien@ripouteau.com>,adonisjs",
@@ -121,11 +128,13 @@
121
128
  "entry": [
122
129
  "./index.ts",
123
130
  "./src/types.ts",
131
+ "./src/helpers.ts",
132
+ "./src/plugins/vite.ts",
124
133
  "./services/inertia.ts",
125
134
  "./src/inertia_middleware.ts",
126
135
  "./providers/inertia_provider.ts",
127
- "./src/plugins/edge.ts",
128
- "./src/plugins/api_client.ts"
136
+ "./src/plugins/edge/plugin.ts",
137
+ "./src/plugins/japa/api_client.ts"
129
138
  ],
130
139
  "outDir": "./build",
131
140
  "clean": true,
@@ -1,123 +0,0 @@
1
- // src/inertia.ts
2
- var kLazySymbol = Symbol("lazy");
3
- var Inertia = class {
4
- constructor(ctx, config) {
5
- this.ctx = ctx;
6
- this.config = config;
7
- }
8
- /**
9
- * Check if a value is a lazy prop
10
- */
11
- #isLazyProps(value) {
12
- return typeof value === "object" && value && kLazySymbol in value;
13
- }
14
- /**
15
- * Pick props to resolve based on x-inertia-partial-data header
16
- *
17
- * If header is not present, resolve all props except lazy props
18
- * If header is present, resolve only the props that are listed in the header
19
- */
20
- #pickPropsToResolve(component, props) {
21
- const partialData = this.ctx.request.header("x-inertia-partial-data")?.split(",").filter(Boolean);
22
- const partialComponent = this.ctx.request.header("x-inertia-partial-component");
23
- let entriesToResolve = Object.entries(props);
24
- if (partialData && partialComponent === component) {
25
- entriesToResolve = entriesToResolve.filter(([key]) => partialData.includes(key));
26
- } else {
27
- entriesToResolve = entriesToResolve.filter(([key]) => !this.#isLazyProps(props[key]));
28
- }
29
- return entriesToResolve;
30
- }
31
- /**
32
- * Resolve the props that will be sent to the client
33
- */
34
- async #resolvePageProps(component, props) {
35
- const entriesToResolve = this.#pickPropsToResolve(component, props);
36
- const entries = entriesToResolve.map(async ([key, value]) => {
37
- if (typeof value === "function") {
38
- return [key, await value(this.ctx)];
39
- }
40
- if (this.#isLazyProps(value)) {
41
- const lazyValue = value[kLazySymbol];
42
- return [key, await lazyValue()];
43
- }
44
- return [key, value];
45
- });
46
- return Object.fromEntries(await Promise.all(entries));
47
- }
48
- /**
49
- * Build the page object that will be returned to the client
50
- *
51
- * See https://inertiajs.com/the-protocol#the-page-object
52
- */
53
- async #buildPageObject(component, pageProps) {
54
- return {
55
- component,
56
- version: this.config.versionCache.getVersion(),
57
- props: await this.#resolvePageProps(component, { ...this.config.sharedData, ...pageProps }),
58
- url: this.ctx.request.url(true)
59
- };
60
- }
61
- /**
62
- * Render a page using Inertia
63
- */
64
- async render(component, pageProps, viewProps) {
65
- const pageObject = await this.#buildPageObject(component, pageProps);
66
- const isInertiaRequest = !!this.ctx.request.header("x-inertia");
67
- if (!isInertiaRequest) {
68
- return this.ctx.view.render(this.config.rootView, { ...viewProps, page: pageObject });
69
- }
70
- return pageObject;
71
- }
72
- /**
73
- * Create a lazy prop
74
- *
75
- * Lazy props are never resolved on first visit, but only when the client
76
- * request a partial reload explicitely with this value.
77
- *
78
- * See https://inertiajs.com/partial-reloads#lazy-data-evaluation
79
- */
80
- lazy(callback) {
81
- return { [kLazySymbol]: callback };
82
- }
83
- /**
84
- * This method can be used to redirect the user to an external website
85
- * or even a non-inertia route of your application.
86
- *
87
- * See https://inertiajs.com/redirects#external-redirects
88
- */
89
- async location(url) {
90
- this.ctx.response.header("X-Inertia-Location", url);
91
- this.ctx.response.status(409);
92
- }
93
- };
94
-
95
- // src/inertia_middleware.ts
96
- var InertiaMiddleware = class {
97
- constructor(config) {
98
- this.config = config;
99
- }
100
- async handle(ctx, next) {
101
- const { response, request } = ctx;
102
- ctx.inertia = new Inertia(ctx, this.config);
103
- await next();
104
- const isInertiaRequest = !!request.header("x-inertia");
105
- if (!isInertiaRequest)
106
- return;
107
- response.header("Vary", "Accept");
108
- response.header("X-Inertia", "true");
109
- const method = request.method();
110
- if (response.getStatus() === 302 && ["PUT", "PATCH", "DELETE"].includes(method)) {
111
- response.status(303);
112
- }
113
- const version = this.config.versionCache.getVersion().toString();
114
- if (method === "GET" && request.header("x-inertia-version", "") !== version) {
115
- response.header("x-inertia-location", request.url());
116
- response.status(409);
117
- }
118
- }
119
- };
120
-
121
- export {
122
- InertiaMiddleware
123
- };