Trouble arises when dynamic parameters fail to function post building in Nuxt

In my Nuxt project set to "spa" mode, I am facing an issue with a URL containing a dynamic parameter /shop/:product, which could have various values such as:

    /shop/ipad-128gb-rose-gold
    /shop/subway-gift-card
    /shop/any-string

and so on.

This setup works perfectly in the development environment with the following directory structure:

pages/
  shop/
    _product.vue

However, when it comes to production, I noticed that the bin/ folder generated by Nuxt does not contain anything inside the shop/ directory. Nuxt provides a solution for this situation here: https://nuxtjs.org/api/configuration-generate/#routes

My dilemma lies in the fact that I do not know what value the :product param will hold (it can be any string).

I retrieve the details of the product in pages/shop/_product.vue from the server, handling errors if necessary. How can I achieve this functionality in a production build?

I find myself confused about the approach suggested by Nuxt -- am I expected to generate routes for every possible product slug?

Answer №1

After encountering a challenge with my app deployment, I found the solution by implementing the following code snippet in my nuxt.config.js:

// nuxt.config.js

export default {
  ...
  generate: {
    fallback: true
  }
}

My application is being hosted from the dist/ folder. As I delved into the Netlify deployment documentation, I stumbled upon this crucial information:

When dealing with a single-page app, there arises an issue with refreshing pages as, by default, Netlify redirects to a "404 not found" error page. This poses a problem for pages that are not pre-generated, leading to a scenario where if you refresh or share such links, you end up on Netlify's standard 404 page. The reason behind this lies in the nature of single-page applications – any non-generated page technically does not exist; hence a refresh would trigger a 404 response due to the missing URL. To circumvent this, redirecting to the 404.html file in Nuxt will ensure proper reloading of your page during SPA fallback.

To resolve this issue seamlessly, simply add a 'generate' property in your nuxt.config file and assign it the value of 'fallback: true'. This configuration enables the application to revert to the generated 404.html when operating in SPA mode, rather than rendering Netlify's custom error page.

For more insights, please refer to:

Answer №2

When static pages are generated, directories and index.html files are created for each one. How can you expect the site to be dynamic if it is serving static HTML?

There are two possible solutions:

  1. Avoid using npm run generate. Instead, run Nuxt on the server. With this approach, ajax calls in the browser are eliminated. Nuxt handles the requests and sends pre-rendered HTML to the browser, which is beneficial for SEO.

  2. Configure your web server (nginx) to direct all requests to /index.html. In this scenario, JavaScript takes over after the page loads and can successfully locate the content by querying products via ajax. However, relying on ajax post-load may negatively impact SEO performance.

For more information and guidance on this topic, refer to Nuxt's official documentation online.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Failed to input data into the database

I created a modal window with a form that should submit the field data to the database without closing the modal window. It should then display a message saying "You have been RSVP'd" before fading out the modal window. There are 2 issues: 1) After ...

VUE JS - My methods are triggering without any explicit invocation from my side

I've encountered a frustrating problem with Vue JS >.<. My methods are being triggered unexpectedly. I have a button that is supposed to execute a specific method, but this method gets executed along with other methods, causing annoyance... Her ...

implement css styling to grid view upon clicking

I have been attempting to add a background color to a grid view row when clicked on that specific row. <script type="text/javascript"> function ChangeRowColor(objref) { objref.style.backgroundcolor = "red"; } </sc ...

"Troubleshooting: My Three.js scene is displaying blank, what could

I recently created a simple example using Three.js, but my code was not organized into classes which caused some issues with displaying anything in the view. Here is an example of the code I used: HTML file <!DOCTYPE html> <html> <head> ...

inter-site requests & browser extensions

I'm currently working on developing a Firefox Addon using the new WebExtensions system. My goal is to: Extract specific text from a webpage (not owned by me) Evaluate it using an external website Display the result on the same page The issue I&apo ...

Assign a temporary value to the Select component in Material UI version 1.0.0-beta.24

I am currently working on a test project using Material UI v1.0.0-beta.24, and I have noticed that the "Dropdown" menus behave differently compared to the previous version. What I am trying to achieve is setting up a placeholder in the Select component. P ...

Guide on converting a JavaScript object into an array consisting of key and value pairs

I have information that appears as follows: [{"letter": "a", "word" : "apple"}, {"letter": "b", "word":"ball"}, {"letter": "a", "word":"alpha"}, {"letter": "c", "word":"cat"}] My goal is to convert it into: {"a":["apple", "alpha"], "b": ["ball"], "c":[" ...

Having an issue with Vue.js and Firebase Storage - encountering an error stating "Uncaught TypeError: storage is not a function"

I'm currently working on a Vuejs & Firebase project where I need to upload images to Firebase storage. Despite following the documentation and tutorials, I keep encountering the same error. Uncaught TypeError: WEBPACK_MODULE_1__firebase_initializer.a ...

Adding additional elements to a div in a horizontal orientation

I am currently working on a project where I need to display bars and restaurants based on specific filter criteria. I have a container div called .resultsContainer where all the results will be displayed. While I can easily append more divs (.barContainer) ...

Using the Vue composition API to invoke a method within a child component

Summary: How can I achieve the same functionality in Vue 3 composition API as using this.$refs in Vue 2? I am currently working on integrating PrimeVue's CustomUpload feature into a Vue 3 project. However, I have encountered an issue where the upload ...

Utilize a standard JavaScript function from a separate document within a function of a React component

I am facing an issue where I need to incorporate a standard JavaScript function within a React component function. The script tags are arranged in the footer in the following order: <script src="NORMAL JAVASCRIPT STUFF"></script> // function ...

Adding specific props, such as fullWidth, at a certain width, like 'sm', in Material UI React

My goal is to include the fullWidth prop when the screen reaches a size of 600px or greater, which is equivalent to the breakpoint sm. I attempted to implement the following code, but unfortunately, it does not seem to be functioning as intended. [theme ...

Passing the value emitted from the vue-multiselect.js component to the main view

I am trying to pass data from my MultiSelectFilter component to my Home.vue view, but I am not seeing any results when using console.log. Even after checking the documentation on using the @input event for capturing changes in selection (), I can't se ...

Unable to trigger the show_popup function by clicking the button

Why won't this piece of code function as expected? <script src="web_push.js"></script> <body> <button onclick="show_popup()"> Button </button> </body> The content of web_push.js file is shown below ...

What is the method for incorporating an onmouseover event into child states while extending the parent state controller?

I have a basic angularjs controller coupled with jquery that triggers a console log message when the mouse hovers over an anchor element: app.controller('MenuController', function() { $("a").on('mouseover', function (e) { c ...

Plow through the contents of the S3 Bucket, exploring every Folder and File

I've encountered an issue while iterating through an S3 bucket using s3.listObjects. The error message { [UnexpectedParameter: Unexpected key 'Key' found in params] has been popping up. Below is the snippet of code I'm currently working ...

Jsoup encounters a complication due to lack of support for Javascript

1http://www.biletix.com/search/TURKIYE/en#!city_sb:İstanbul,date_sb:today Trying to access an element from the provided link results in a message about Javascript not being found. It suggests enabling Javascript and then abruptly closes. As I plan to mi ...

What is the best way to modify the display of a specific section on a page in VueJs 3?

I have three main sections on my webpage, and I am looking to change the view for just one section: <template> <div id="main"> <div id="scene"> <scene/> </div> < ...

Tips for properly passing a Vue3 Ref variable as a reference (plus the mystery of the "global" variable)

I created a custom Quasar/Vue3 component called TemperatureOutside.vue: <template> <div class='text-large-1 text-weight-bold'> {{ temperature }}° </div> </template> <script setup lang='ts'> import ...

What steps can I take to generate 12 random div elements using jQuery out of a pool of 30?

Currently, all the words are displaying, but I would like the script to randomly select 12 out of the 30 words var randomdivs = $("wordstyle").get().sort(function(){ return Math.round(Math.random())-0.5; }).slice(0,12) $(randomdivs).show(); The follo ...