New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update API/generate, add Russian #733
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
title: "API: The generate Property" | ||
title: 'API: The generate Property' | ||
description: Configure the generation of your universal web application to a static web application. | ||
--- | ||
|
||
|
@@ -30,7 +30,7 @@ The path to the SPA fallback. This file can be used when doing deploys of genera | |
- Type: `Number` | ||
- Default: `0` | ||
|
||
Interval between 2 render to avoid flooding the API calls made to a potential API from the web application. | ||
Interval between two render cycles to avoid flooding a potential API with API calls from the web application. | ||
|
||
## minify | ||
|
||
|
@@ -60,7 +60,7 @@ minify: { | |
} | ||
``` | ||
|
||
You can change the default configuration of [html-minifier](https://github.com/kangax/html-minifier) used by Nuxt.js to minify HTML files created during generate process. | ||
You can change the default configuration of [html-minifier](https://github.com/kangax/html-minifier) used by Nuxt.js to minify HTML files created during generation. | ||
|
||
## routes | ||
|
||
|
@@ -86,11 +86,7 @@ We add routes for `/users/:id` in `nuxt.config.js`: | |
```js | ||
module.exports = { | ||
generate: { | ||
routes: [ | ||
'/users/1', | ||
'/users/2', | ||
'/users/3' | ||
] | ||
routes: ['/users/1', '/users/2', '/users/3'] | ||
} | ||
} | ||
``` | ||
|
@@ -126,14 +122,10 @@ const axios = require('axios') | |
|
||
module.exports = { | ||
generate: { | ||
routes: function () { | ||
return axios.get('https://my-api/users') | ||
.then((res) => { | ||
return res.data.map((user) => { | ||
return '/users/' + user.id | ||
}) | ||
}) | ||
} | ||
routes: () => | ||
axios | ||
.get('https://my-api/users') | ||
.then(res => res.data.map(user => '/users/' + user.id)) | ||
} | ||
} | ||
``` | ||
|
@@ -147,23 +139,21 @@ const axios = require('axios') | |
|
||
module.exports = { | ||
generate: { | ||
routes: function (callback) { | ||
axios.get('https://my-api/users') | ||
.then((res) => { | ||
var routes = res.data.map((user) => { | ||
return '/users/' + user.id | ||
routes: callback => | ||
axios | ||
.get('https://my-api/users') | ||
.then(res => { | ||
const routes = res.data.map(user => '/users/' + user.id) | ||
callback(null, routes) | ||
}) | ||
callback(null, routes) | ||
}) | ||
.catch(callback) | ||
} | ||
.catch(callback) | ||
} | ||
} | ||
``` | ||
|
||
### Speeding up dynamic route generation with `payload` | ||
|
||
In the example above, we're using the `user.id` from the server to generate the routes but tossing out the rest of the data. Typically, we need to fetch it again from inside the `/users/_id.vue`. While we can do that, we'll probably need to set the `generate.interval` to something like `100` in order not to flood the server with calls. Because this will increase the run time of the generate script, it would be preferable to pass along the entire `user` object to the context in `_id.vue`. We do that with by modifying the code above to this: | ||
In the example above, we're using the `user.id` from the server to generate the routes but tossing out the rest of the data. Typically, we need to fetch it again from inside the `/users/_id.vue`. While we can do that, we'll probably need to set the `generate.interval` to something like `100` in order not to flood the server with calls. Because this will increase the run time of the generate script, it would be preferable to pass along the entire `user` object to the context in `_id.vue`. We do that by modifying the code above to this: | ||
|
||
`nuxt.config.js` | ||
|
||
|
@@ -172,28 +162,24 @@ const axios = require('axios') | |
|
||
module.exports = { | ||
generate: { | ||
routes: function () { | ||
return axios.get('https://my-api/users') | ||
.then((res) => { | ||
return res.data.map((user) => { | ||
return { | ||
route: '/users/' + user.id, | ||
payload: user | ||
} | ||
}) | ||
}) | ||
} | ||
routes: () => | ||
axios.get('https://my-api/users').then(res => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return statement missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What, where? |
||
res.data.map(user => ({ | ||
route: '/users/' + user.id, | ||
payload: user | ||
})) | ||
) | ||
} | ||
} | ||
``` | ||
|
||
Now we can access the `payload` from `/users/_id.vue` like so: | ||
|
||
```js | ||
async asyncData ({ params, error, payload }) { | ||
if (payload) return { user: payload } | ||
else return { user: await backend.fetchUser(params.id) } | ||
} | ||
asyncData: async ({ params, error, payload }) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While it might be optimised, I don't think it's a good idea for documentation since it makes it harder to read for a human :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
and stopped at that. That said, I'm open to change this and go through all the files substituting it, just tell me which specific operator you dislike here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would like @manniL opinion on this particular comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @manniL it has come a full circle UwU There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd go for something like async asyncData ({ params, error, payload }) {
if (payload) {
return { user: payload }
}
const user = await backend.fetchUser(params.id)
return { user }
} Not all readers know ES6 and short circuits very well, so I'd favor a "more readable" /simple version There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oke, will do. async asyncData ({ params, error, payload }) {
if (payload) return { user: payload }
else return { user: await backend.fetchUser(params.id) }
} i'd just change it to arrow notation since asyncData: async ({ params, error, payload }) => {
if (payload) return { user: payload }
return { user: await backend.fetchUser(params.id) }
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @qm3ster I don't like "inline ifs" actually. Was a fan of them years ago but now I appreciate the extra braces for readability. This is also aligns with our eslint config. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha, will do like yours. Should the |
||
payload | ||
? { user: payload } | ||
: { user: await backend.fetchUser(params.id) } | ||
``` | ||
|
||
## subFolders | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
title: "API: The generate Property" | ||
title: 'API: The generate Property' | ||
description: Configure the generation of your universal web application to a static web application. | ||
--- | ||
|
||
|
@@ -86,11 +86,7 @@ We add routes for `/users/:id` in `nuxt.config.js`: | |
```js | ||
module.exports = { | ||
generate: { | ||
routes: [ | ||
'/users/1', | ||
'/users/2', | ||
'/users/3' | ||
] | ||
routes: ['/users/1', '/users/2', '/users/3'] | ||
} | ||
} | ||
``` | ||
|
@@ -126,14 +122,10 @@ const axios = require('axios') | |
|
||
module.exports = { | ||
generate: { | ||
routes: function () { | ||
return axios.get('https://my-api/users') | ||
.then((res) => { | ||
return res.data.map((user) => { | ||
return '/users/' + user.id | ||
}) | ||
}) | ||
} | ||
routes: () => | ||
axios | ||
.get('https://my-api/users') | ||
.then(res => res.data.map(user => '/users/' + user.id)) | ||
} | ||
} | ||
``` | ||
|
@@ -147,16 +139,14 @@ const axios = require('axios') | |
|
||
module.exports = { | ||
generate: { | ||
routes: function (callback) { | ||
axios.get('https://my-api/users') | ||
.then((res) => { | ||
var routes = res.data.map((user) => { | ||
return '/users/' + user.id | ||
routes: callback => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually @manniL I now see that this may be confusing, because it does call the callback, but also returns a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we want to keep a callback example for the routes function at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fine by me. 😩 👌 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say so |
||
axios | ||
.get('https://my-api/users') | ||
.then(res => { | ||
const routes = res.data.map(user => '/users/' + user.id) | ||
callback(null, routes) | ||
}) | ||
callback(null, routes) | ||
}) | ||
.catch(callback) | ||
} | ||
.catch(callback) | ||
} | ||
} | ||
``` | ||
|
@@ -172,28 +162,24 @@ const axios = require('axios') | |
|
||
module.exports = { | ||
generate: { | ||
routes: function () { | ||
return axios.get('https://my-api/users') | ||
.then((res) => { | ||
return res.data.map((user) => { | ||
return { | ||
route: '/users/' + user.id, | ||
payload: user | ||
} | ||
}) | ||
}) | ||
} | ||
routes: () => | ||
axios.get('https://my-api/users').then(res => | ||
res.data.map(user => ({ | ||
route: '/users/' + user.id, | ||
payload: user | ||
})) | ||
) | ||
} | ||
} | ||
``` | ||
|
||
Now we can access the `payload` from `/users/_id.vue` like so: | ||
|
||
```js | ||
async asyncData ({ params, error, payload }) { | ||
if (payload) return { user: payload } | ||
else return { user: await backend.fetchUser(params.id) } | ||
} | ||
asyncData: async ({ params, error, payload }) => | ||
payload | ||
? { user: payload } | ||
: { user: await backend.fetchUser(params.id) } | ||
``` | ||
|
||
## subFolders | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this makes it clearer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was Prettier that did this, actually. Seemed more than short enough not to split.
I split the french because it uses
/utilisateurs/
instead of/users/
which imo is a bit much. Maybe the examples should be monolingual, so that if we later have a translation build process they can be imported from one place.