Sitelet https://web.archive.org/web/20200912025611/https://github.com/nuxt/nuxt.js/issues/6138
Skip to content
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

Add an option to nuxt generate to generate a single route #6138

Open
emiliobondioli opened this issue Jul 26, 2019 — with CMTY · 22 comments
Open

Add an option to nuxt generate to generate a single route #6138

emiliobondioli opened this issue Jul 26, 2019 — with CMTY · 22 comments

Comments

Copy link

@emiliobondioli emiliobondioli commented Jul 26, 2019 — with CMTY •

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

This feature request is available on Nuxt community (#c9562)
@cmty cmty bot added the feature-request label Jul 26, 2019
@pimlie
Copy link
Member

@pimlie pimlie commented Jul 26, 2019

You could already implement this yourself in nuxt.config.js as follows:

  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
    }
}
@emiliobondioli
Copy link
Author

@emiliobondioli emiliobondioli commented Jul 29, 2019 •

Nice solution, thanks! But I tried it now and even using the --no-build flag this still removes all the other generated pages, leaving only the new one.

Looking around, it looks like I'd need a way to set the init option in cli-generate to false, would this cause any issue if it was false only when generating single routes?

@monisnap-julien
Copy link

@monisnap-julien monisnap-julien commented Jul 30, 2019

Hi ! I'm also facing this issue right now while making a blog. I will have a growing number of pages and doing the nuxt generate will take more and more time. Hope to find a solution as soon as possible !

@pimlie
Copy link
Member

@pimlie pimlie commented Jul 30, 2019

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
@emiliobondioli
Copy link
Author

@emiliobondioli emiliobondioli commented Jul 31, 2019

Ok that seems like a good workaround! so you think having this built into nuxt would be useless/out of scope?

@manniL
Copy link
Member

@manniL manniL commented Jul 31, 2019

I think it'd be a good fit for static generation ☺️

@Atinux
Copy link
Member

@Atinux Atinux commented Aug 9, 2019

About full static mode, re-generating a single route might be difficult for the payload hash @manniL 🤔

@manniL
Copy link
Member

@manniL manniL commented Aug 9, 2019

@Atinux damn, thats right 🙈

@hesselberg
Copy link

@hesselberg hesselberg commented Feb 26, 2020

@Atinux @manniL did you ever find a way of handling the payload hash? 🤔

@Atinux
Copy link
Member

@Atinux Atinux commented Mar 6, 2020

Yes, you want to look at the target PR :)

@simplenotezy
Copy link

@simplenotezy simplenotezy commented May 27, 2020

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.

@jonalxh
Copy link

@jonalxh jonalxh commented Jun 4, 2020

Having the same trouble with my project, I have a products page and a details page which is dynamic, If I access it by products page and then refresh, it throws 404 in production. I've done the process with npm run generate.

@wlarch
Copy link

@wlarch wlarch commented Sep 2, 2020

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/
https://nextjs.org/docs/basic-features/data-fetching#incremental-static-regeneration

How could we archive the same ?

@danielroe
Copy link
Collaborator

@danielroe danielroe commented Sep 2, 2020

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.

@wlarch
Copy link

@wlarch wlarch commented Sep 2, 2020

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 generate.exclude property for excluding the pages that have not been modified, although their payload and html are still generated.

Still looking for a way to customize which pages are generated or not depending on payload of dynamic routes.

@f3ltron
Copy link

@f3ltron f3ltron commented Sep 2, 2020 •

Just to be sure. The "good way" would be to yarn generate --no-build -r /single/route? (using target: 'static') and compare it with ou current static gen website?

@hesselberg
Copy link

@hesselberg hesselberg commented Sep 2, 2020

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

@happynautpro
Copy link

@happynautpro happynautpro commented Sep 3, 2020

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.

@wlarch
Copy link

@wlarch wlarch commented Sep 3, 2020

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

@happynautpro
Copy link

@happynautpro happynautpro commented Sep 4, 2020

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.

@f3ltron
Copy link

@f3ltron f3ltron commented Sep 5, 2020

yarn generate --no-build -r /single/about.vue is not really working no ?

no matter what I am doing it just generate everything

yarn run v1.22.4
$ nuxt generate --no-build -r pages/about.vue
✔ Skipping webpack build as no changes detected                                                                                                                                                                                                                                                                                                                   
ℹ Generating output directory: dist/                                                                                                                                                                                                                                                                                                                              
ℹ Generating pages with full static mode                                                                                                                                                                                                                                                                                                                          
✔ Generated route "/"                                                                                                                                                                                                                                                                                                                                             
✔ Generated route "/about"                                                                                                                                                                                                                                                                                                                                        
✔ Generated route "/test"                                                                                                                                                                                                                                                                                                                                         
✔ Client-side fallback created: 200.html
@f3ltron
Copy link

@f3ltron f3ltron commented Sep 5, 2020

As we know static gen cache node_modules. Base on that: https://nuxtjs.org/blog/nuxt-static-improvements#introduction

  • we should have something when the code is push on github then cache again node modules. with the build.json infos nuxt generate will generate only files who changed.
  • when cms or whatever update a page send a webhook that just nuxt generate only [pages]

I am trying things

I think I understood what you wanted no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.