Add an option to nuxt generate to generate a single route #6138
Comments
You could already implement this yourself in generate: {
routes() {
const rIdx = process.argv.indexOf('-r')
if (rIdx) { // it will never be 0 as that would be the node/nuxt command
return [
process.argv[rIdx + 1]
]
}
// return default / all routes
}
} |
Nice solution, thanks! But I tried it now and even using the Looking around, it looks like I'd need a way to set the |
Hi ! I'm also facing this issue right now while making a blog. I will have a growing number of pages and doing the |
Much easier to just manage your dist folder a bit, no need to change anything in nuxt for that: $ yarn generate
$ rsync -auv dist/* production-build/
$ yarn generate --no-build -r /single/route
$ rsync -auv dist/* production-build/ and to start over: $ rm production-build/* -Rf |
Ok that seems like a good workaround! so you think having this built into nuxt would be useless/out of scope? |
I think it'd be a good fit for static generation |
About full static mode, re-generating a single route might be difficult for the payload hash @manniL |
@Atinux damn, thats right |
Yes, you want to look at the target PR :) |
Would love this as well. It doesn't work quite well now, as when people edit one page in the CMS whole site has to be regenerated which takes around 2000 seconds. |
Having the same trouble with my project, I have a |
We are looking to archive the same goal for our full static project when generating dynamic routes. We want to avoid regenerating the whole website for specific changes / addition made in the content. Next.js offer a very interesting incremental static generation : https://reactions-demo.now.sh/ How could we archive the same ? |
This could be done with some code in your config. With Nuxt full static the site won't be rebuilt, and you can turn off the crawler and just specify a smaller set of URLs in the generate options. |
Thanks for your reply. Unfortunately, when generating a smaller subset of routes. The other routes are removed from the /dist folder, thus when syncing to my external static storage, they are deleted too. I tried using the Still looking for a way to customize which pages are generated or not depending on payload of dynamic routes. |
Just to be sure. The "good way" would be to |
@f3ltron without knowing @wlarch exact scenario, I would say yes, but even then, I wouldn't worry about the comparison, just the build/generate part. A use case would be a CMS, where a content editor would "publish" a piece of content, and then trigger a webhook that would generate that specific route. |
Got the very same problem: What if you have a site with about 5.000 posts but you change only the content of one of them? Therefore, it would be useful to have an option, just as @hesselberg has described. |
@hesselberg This is exactly what we have though. We maintain a CMS that allow the creation and modification of content. Any action on the content would trigger a webhook for generating a specific single page or set of pages. The generate files would then be uploaded to the static hosting. |
I've found this approach: https://github.com/hanbyul-here/nuxt-incremental-build-exp In local dev mode it works fine, however if I use it on Netlify, deploying fails. |
no matter what I am doing it just generate everything
|
As we know static gen cache node_modules. Base on that: https://nuxtjs.org/blog/nuxt-static-improvements#introduction
I am trying things I think I understood what you wanted no? |
Hi there, Sharing my progress in this investigation has It seems to be at Node level but I'm not experiencing that error with any other command than For demonstration purpose, I've tried running it on nuxt/nuxtjs.org repo. Reproduction tests: ❯ git clone git@github.com:nuxt/nuxtjs.org.git
❯ npm i
❯ npm run generate
❯ npm run generate --no-build -r /blog/seed-round
❯ npm run generate -r /blog/seed-round
> nuxtjs.org@1.1.0 generate /Users/XXX/projects/nuxtjs.org
> nuxt generate "/blog/seed-round"
FATAL EROFS: read-only file system, mkdir '/blog'
╭────────────────────────────────────────────────────────╮
│ │
│ ✖ Nuxt Fatal Error │
│ │
│ Error: EROFS: read-only file system, mkdir '/blog' │
│ │
╰────────────────────────────────────────────────────────╯
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nuxtjs.org@1.1.0 generate: `nuxt generate "/blog/seed-round"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nuxtjs.org@1.1.0 generate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/XXX/.npm/_logs/2020-09-15T13_34_26_002Z-debug.log I've also tried deleting Last, using yarn, I'm getting the same issue as @f3ltron. |
When I fire Also the specific route I've set to generate (.e.g What am I missing? |
@griable, @simplenotezy and @f3ltron, did you edit your nuxt config? AFAIK the option to generate a single route hasn't been added yet, but you can get a similar behavior with some edits to your nuxt.config and build scripts, I'll just add below the full process as described by @pimlie : You can get the generate: {
routes() {
const rIdx = process.argv.indexOf('-r')
if (rIdx > -1) { // it will never be 0 as that would be the node/nuxt command
return [
process.argv[rIdx + 1]
]
}
// return default / all routes
}
} This will allow the generation of a single route like this In my package.json i have a
and a
Which i can call like this: |
@emiliobondioli thats cool! Thanks for sharing. How would we go about to add multiple routes to -R? Is that supported? |
Also this leads me into thinking; we could simply send a GET request to backend to determine what routes have changed and only generate those routes, EXCEPT, If Nuxt had to rebuild config (eg after JavaScript/component has changed). Is there a parameter to check if JS has changed / or if the rebuild was triggered during Nuxt generate? |
It doesn't break the hash on other files ? For old files @emiliobondioli |
@f3ltron I guess not, as long as nuxt haven't been rebuilt ( |
Oh yeah you right I will try it soon thanks for your work |
Thank @emiliobondioli ;) Also; I still need to figure out some what to check if build was rebuilt or not, because, if build was rebuilt, you'd have to do a complete deploy with all routes. |
As a simple workaround you could call it like this:
And edit the generate section of your nuxt config to split the incoming string on every space character (or on every comma if you prefer): routes() {
const rIdx = process.argv.indexOf('-r')
if (rIdx) { // it will never be 0 as that would be the node/nuxt command
return process.argv[rIdx + 1].trim().split(' ') // splits the incoming string to get the single routes
}
// return default / all routes
}
The way I'm using it is kind of the opposite: I have a headless Wordpress backend with some hooks set up when new posts are created or updated which run the |
I think it would've possible to make it as a module here |
@emiliobondioli that's interesting. Can you elaborate a bit on how you do the "generate:single"? |
npm run generate:single "$1"
rsync -auv dist/* public/ |
thanks @emiliobondioli |
Just wanted to update and say that I have made an awesome build process using @emiliobondioli's suggestion in Jenkins. Site can now be deployed incrementally or "fully", as well as running end-to-end tests using Cypress. If doing an incremental build, instead of passing the routes to generate, an API call will be made to query the backend for any changes that has happen since a given date, and then return those routes along with their payload. |
A little update/question. Perhaps you know the answer @emiliobondioli. Is it possible to set the "exclude" property inside the generate.routes function? E.g. if I am running an incremental build, then exclude all routes, except the ones coming from the generate.routes array? I am asking because when having to build just a single route, or two, it's pointless to generate all the other default routes in my app, such as /my-account/ etc. I tried with:
But that doesn't seem to get respect that far into the process. Any ideas? Good night ;) |
@simplenotezy I don't think it's possible to exclude the static routes inside generate.routes, but you can do it using the this.nuxt.hook('generate:extendRoutes', routes => {
const routesArg = process.argv.indexOf('-r');
if (routesArg > -1) routes.length = 0
}) note that in this hook you have to actually modify the routes array, you can't just return |
Thanks @emiliobondioli and where would I go an run the extendRoutes? Missing the whole picture here |
I have implemented @emiliobondioli solution to modify the routes array using the
Although I realize this completely empties the routes array and does not only exclude the static ones. |
EDIT : (removed my previous question as it was irrelevant, I made a mistake in my payload) |
It seems I found a problem regarding the process explained in this thread. The timestamp generated on an incremental build causes a problem when directly loading a page that was generated in a subsequent build. Exemple :
The second generation will create static assets in a different timestamps (version) of the app in the Changing the following parameter in
What is your though on this ? |
Hi everyone, Thanks. |
Bump on this. Would be awesome to generate specific routes in a Nuxt application which is otherwise a SPA. |
@emiliobondioli wouldn't excluding all other routes during full static generation break the links to those pages? I'm pretty sure Nuxt is tracking what is a valid path and what is not internally... |
In my case the whole dist folder is regenerated even if I use --no-build and -r arguments.
...and this is how my npm scripts look like:
I call the script like this: Any idea why |
@isvaljek Currently in full static mode, |
@danielroe Previous to your post I had rebuilds stating changes to my files. But now it doesn't specify the file changed. |
@isvaljek I'm happy to help but I would suggest you raise a new issue with a reproduction as this has diverged from this feature request somewhat ... |
@danielroe, may I ask one more question which is more on-topic-y? I've managed (with the help of this page) to create on demand route regeneration, but I'd like have the full site static generation available as well. For that I'd like to have crawler:true enabled, and crawler:false for the generate:single route rebuild. Is it possible? |
@isvaljek You can write a buildModule to overwrite Nuxt options during static site generation. Here is an example, code is in
You can reach out to me on Discord if you need more assistance. I may be able to help you as I have experienced a lot with static generation. |
You mean if pram -r page_url was set? const routesArg = process.argv.indexOf('-r'); |
@nuxtdev we have to add the route arg check, this is just a hook boiler. You can also add |
What problem does this feature solve?
Example use case:
A static generated app with thousands of routes, like an e-commerce having a route for each product.
Apart from scheduling
nuxt generate
to run periodically and generate all static and dynamic routes, it would be nice to have the possibility to generate the static files for a single route, for example in a hook when the related backend item is updated.In a situation where there's a high frequency of updates, re-generating all the dynamic routes every time there's an update to a single one is not really efficient. This way, for every update, just the interested route is re-generated and replaces the previous one (if present).
What does the proposed changes look like?
Update the
nuxt generate
command to accept a -r or --routes option, for example:nuxt generate --no-build -r /products/42
The text was updated successfully, but these errors were encountered: