Home  >  Q&A  >  body text

Nuxt.js: Unable to automatically generate routing configuration

<p>I want to generate static routes (<code>/contact</code>, <code>/about</code>,...) and dynamic routes (<code>/project /1</code>, <code>/project/2</code>, ...) so that when the user refreshes the page while accessing one of these routes, the page still works correctly. </p> <p>But when executing <code>npm run generate</code>, I only get the <code>generated route "/"</code>, in <code>/dist</code> The generated routes are not visible in the folder. </p> <p>Nuxt.js version used: <code>2.14.7</code></p> <p>I tried both modes, <code>universal</code> and <code>spa</code>, neither worked. </p> <p>In the nuxt.config.js file, I added the following code at the top: </p> <pre class="brush:js;toolbar:false;">const axios = require('axios') const dynamicRoutes = async () => { const routes = await axios.get('http://my-project.com/wp/wp-json/projects/v1/posts') .then(res => res.data.map((project) => `/project/${project.ID}/${project.post_name}`)) .then(res => res.concat( [ '/about', '/contact', '/portfolio' ] )) return routes } </pre> <p>Then in <code>export default {}</code>: </p> <pre class="brush:js;toolbar:false;">generate: { routes: dynamicRoutes }, </pre> <p><br /></p>
P粉404539732P粉404539732399 days ago453

reply all(2)I'll reply

  • P粉904405941

    P粉9044059412023-08-26 12:02:31

    First of all, you don't need to add mode: 'universal' to the configuration, just add target: 'static' to simplify the configuration. Learn more - https://nuxtjs.org/docs/2.x/features/deployment-targets/. With ssr: true you will get a fully static mode website with relevant hooks as described in https://stackoverflow.com/a/65208463/8153537.

    Next, you can remove the @nuxt/router module. Check out my gist - https://gist.github.com/MexsonFernandes/d04495c86b115bbe29f26b36b0b35d2d. Nuxt generates all required routes based on the folder structure, so no additional configuration is required.

    Check out this gist for project page routing - https://gist.github.com/MexsonFernandes/d04495c86b115bbe29f26b36b0b35d2d#gistcomment-3555332.

    reply
    0
  • P粉752826008

    P粉7528260082023-08-26 00:46:34

    router.mode='hash' appears to be incompatible with the generate.routes configuration. When router.mode is set to hash, the Nuxt generator ignores generate.routes and only creates a route for / , this is probably because the homepage is only expected to exist in hash mode (i.e. index.html has a route set up that handles all routes for the application).

    This hash mode also conflicts with the mode set in router.js, but if you really need hash routing, you should choose only in router.js Set this in # to allow processing of generate.routes.

    Also note that mode='universal' is equivalent to ssr=true, so the configuration ssr=false is not available in this mode significance. If generating a static site, you need ssr=true so that any asyncData() and fetch() hooks can be called to populate the static page data. This setup also eliminates the need to add /about, /contact and /portfolio in dynamicRoutes() # because they are already included in the generated route.

    GitHub PR

    reply
    0
  • Cancelreply