inertia-sails 0.0.8 → 0.0.9
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/README.md +104 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Inertia.js Sails Adapter
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
## Backend
|
|
6
|
+
|
|
7
|
+
The quickest way to get setup an Inertia powered Sails app is to use the [create-sails](https://github.com/sailscastshq/create-sails) scaffolding tool. Just run
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npx create-sails <project-name>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
> Do replace `<project-name>` with the name you want your project to be.
|
|
14
|
+
|
|
15
|
+
## Frontend
|
|
16
|
+
If you are using the [create-sails](https://github.com/sailscastshq/create-sails) scaffolding tool then the Frontend framework you choose from the CLI prompt should already be setup for you.
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Responses
|
|
21
|
+
|
|
22
|
+
Sending back an Inertia responses is pretty simple, just use the `sails.inertia.render` method in your Sails actions(You can look at the example actions if you used create-sails). The `render` method accepts two arguments, the first is the name of the component you want to render from within your `pages` directory in `assets/js` (without the file extension).
|
|
23
|
+
|
|
24
|
+
The second argument is the props object where you can provide props to your components.
|
|
25
|
+
|
|
26
|
+
## Shared Data
|
|
27
|
+
|
|
28
|
+
If you have data that you want to be provided as a prop to every component (a common use-case is informationa about the authenticated user) you can use the `sails.inertia.share` method.
|
|
29
|
+
|
|
30
|
+
In Sails having a custom hook by running `sails generate hook custom` will scaffolding a project level hook in which you can share the loggedIn user information for example. Below is a real world use case:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
/**
|
|
34
|
+
* custom hook
|
|
35
|
+
*
|
|
36
|
+
* @description :: A hook definition. Extends Sails by adding shadow routes, implicit actions, and/or initialization logic.
|
|
37
|
+
* @docs :: https://sailsjs.com/docs/concepts/extending-sails/hooks
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
module.exports = function defineCustomHook(sails) {
|
|
41
|
+
return {
|
|
42
|
+
/**
|
|
43
|
+
* Runs when this Sails app loads/lifts.
|
|
44
|
+
*/
|
|
45
|
+
initialize: async function () {
|
|
46
|
+
sails.log.info('Initializing custom hook (`custom`)')
|
|
47
|
+
},
|
|
48
|
+
routes: {
|
|
49
|
+
before: {
|
|
50
|
+
'GET /': {
|
|
51
|
+
skipAssets: true,
|
|
52
|
+
fn: async function (req, res, next) {
|
|
53
|
+
if (req.session.userId) {
|
|
54
|
+
const loggedInUser = await User.findOne({
|
|
55
|
+
id: req.session.userId,
|
|
56
|
+
})
|
|
57
|
+
if (!loggedInUser) {
|
|
58
|
+
sails.log.warn(
|
|
59
|
+
'Somehow, the user record for the logged-in user (`' +
|
|
60
|
+
req.session.userId +
|
|
61
|
+
'`) has gone missing....'
|
|
62
|
+
)
|
|
63
|
+
delete req.session.userId
|
|
64
|
+
return res.redirect('/signin')
|
|
65
|
+
}
|
|
66
|
+
sails.inertia.share('loggedInUser', loggedInUser)
|
|
67
|
+
return next()
|
|
68
|
+
}
|
|
69
|
+
return next()
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Configuration
|
|
79
|
+
If you used the `create-sails` scaffolding tool, you will find the configuration file for Inertia.js in `config/inertia.js`. You will mostly use this file for asset-versioning in Inertia by setting either a number or string that you can update when your assets changes. Below is an example of how this file looks like:
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
/**
|
|
83
|
+
* Inertia configuration
|
|
84
|
+
* (sails.config.inertia)
|
|
85
|
+
*
|
|
86
|
+
* Use the settings below to configure session integration in your app.
|
|
87
|
+
*
|
|
88
|
+
* For more information on Inertia configuration, visit:
|
|
89
|
+
* https://inertia-sails.sailscasts.com
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
module.exports.inertia = {
|
|
93
|
+
/**
|
|
94
|
+
* https://inertiajs.com/asset-versioning
|
|
95
|
+
* You can pass a string, number that changes when your assets change
|
|
96
|
+
* or a function that returns the said string, number.
|
|
97
|
+
* e.g 4 or () => 4
|
|
98
|
+
*/
|
|
99
|
+
// version: 1,
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
Visit [inertiajs.com](https://inertiajs.com/) to learn more.
|