Skip to content

Commit

Permalink
Merge branch 'release/1.2.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Feb 29, 2024
2 parents 33cd475 + a629700 commit f0de977
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
42 changes: 29 additions & 13 deletions docs/boring-stack/redirects.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ You can set up one route to redirect to another route within your app or even to

```js
module.exports.routes = {
'/chat': '/community', // [!code focus]
'GET /docs': 'https://docs.sailscasts.com' // [!code focus]
'/chat': '/community',
'GET /docs': 'https://docs.sailscasts.com'
}
```

Expand All @@ -39,14 +39,12 @@ You can also set an exit to signal a redirect in your action.
```js
module.exports = {
exits: {
// [!code focus]
success: {
// [!code focus]
responseType: 'redirect' // [!code focus]
} // [!code focus]
}, // [!code focus]
responseType: 'redirect'
}
},
fn: async function (inputs) {
return '/' // [!code focus]
return '/'
}
}
```
Expand All @@ -61,10 +59,19 @@ Learn more about [Actions and Controllers](https://sailsjs.com/documentation/con

When redirecting after a `PUT`, `PATCH`, or `DELETE` request from your SPA, you must use a `303` response code, otherwise the subsequent request will not be treated as a `GET` request. A `303` redirect is very similar to a `302` redirect; however, the follow-up request is explicitly changed to a `GET` request.

The Boring Stack provides you with a helper method to do that in your controller actions:
The Boring Stack provides you with the `inertiaRedirect` custom response to do that in your controller actions:

```js
return sails.inertia.location(url)
module.exports = {
exits: {
success: {
responseType: 'inertiaRedirect'
}
},
fn: async function (inputs) {
return '/users'
}
}
```

::: info
Expand All @@ -73,10 +80,19 @@ Learn more about [303 response code](https://inertiajs.com/redirects#303-respons

## External redirects

Sometimes it's necessary to redirect to an external website, or even another page in your app that's not an SPA. This can be accomplished using a server-side initiated `window.location` visit via the `sails.inertia.location()` method.
Sometimes it's necessary to redirect to an external website, or even another page in your app that's not an SPA. This can be accomplished using a server-side initiated `window.location` visit via the `inertiaRedirect` custom response.

```js
return sails.inertia.location(url)
module.exports = {
exits: {
success: {
responseType: 'inertiaRedirect'
}
},
fn: async function (inputs) {
return 'https://sailsjs.com'
}
}
```

The `sails.inertia.location(url)` method will generate a `409` Conflict response if the redirect request is coming from a `POST` or `GET` method and include the destination URL in the `X-Inertia-Location` header. When this response is received client-side, Inertia will automatically perform a `window.location = url` visit.
The `inertiaRedirect` custom response will generate a `409` Conflict response if the redirect request is coming from a `POST` or `GET` method and include the destination URL in the `X-Inertia-Location` header. When this response is received client-side, Inertia will automatically perform a `window.location = url` visit.
12 changes: 9 additions & 3 deletions docs/boring-stack/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ For example let's say we want to create a `/users` route, we can define the rout

```js
module.exports.routes = {
'GET /users': 'user/view-users' // [!code focus]
'GET /users': 'user/view-users'
}
```

Expand All @@ -41,11 +41,17 @@ Run `npx sails generate action user/view-users` to scaffold the action.
module.exports = {
inputs: {},
exits: {
success: {}
success: {
responseType: 'inertia'
}
},
fn: async function (inputs) {
const users = await User.find()
return sails.inertia.render('users/index', { users }) // [!code focus]
return { page: 'users/index', props: { users } }
}
}
```

::: info
Note the `responseType: 'inertia'` in the success exit.
:::
4 changes: 1 addition & 3 deletions docs/inertia-sails/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ editLink: true

## Responses

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).

The second argument is the props object where you can provide props to your components.
Sending back an Inertia responses is pretty simple, just use the return an Inertia custom response in your Sails actions(You can look at the example actions if you used create-sails). The returned response should be an object with the `page` and optionally a `props` property.

## Shared Data

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sailscasts-docs",
"version": "1.2.6",
"version": "1.2.7",
"private": true,
"description": "The official docs hub for Sailscasts",
"scripts": {
Expand Down

0 comments on commit f0de977

Please sign in to comment.