After reloading the page, Nuxt dynamic routes are displaying a 404 error

Hey there! I'm currently diving into a project that involves using nuxt js, and it's all new to me. I've set it up in spa mode without any modifications in the nuxt config file, just sticking with the default settings. Here's how I've organized my pages:

pages/users/index.vue - displays a list of users

pages/users/_id.vue - shows details of a specific user

I've completed the deployment by running the commands npm run build and npm run start. The dist directory is now hosted on an nginx server.

The problem arises when I try to access /user/id directly through the URL or refresh the page – I end up getting a nginx 404 error page instead of my intended content being rendered.

I've come across suggestions about using nuxt generate for pre-rendering pages, but I'm uncertain if it would be suitable for handling a large number of records efficiently. Any advice or assistance on this matter would be greatly appreciated!

Thanks in advance!

Answer №1

Before diving into Nuxt, it's important to understand how it can help solve your problems.

Nuxt allows you to create three different types of applications:

  1. Static Pages

In this mode, Nuxt generates SEO-friendly HTML files based on routing. This is great for simple websites like business cards, where you get ready-made html files such as index.html, contact.html, etc.

  1. Single Page Applications (SPA)

SPAs do not require SEO but have dynamic paths and interfaces. They do not use server-side rendering but still offer benefits like dynamic routing and customizable configurations in Nuxt.

  1. Universal Mode

This mode offers all the benefits of Nuxt.js. By utilizing dedicated methods like fetch, asyncData, nuxtServerInit, etc., you can prepare data on the server side to generate SEO-friendly content on the browser side.

When choosing between SPA and Universal mode for dynamic routing, it's essential to consider your specific needs. Refer to Nuxt's documentation for guidance on which commands to use.

Answer №2

Creating a Nuxt site with static generation and dynamic routes

If you're unsure of the dynamic routes or SEO isn't a concern, this method is for you.

To implement this approach, set up a custom SPA fallback page in the generate configuration:

export default {
  generate: {
    fallback: "custom_sap_fallbackpage.html"
  }
}

Next, configure Nginx to use the same fallback page for unknown routes:

location / {
    try_files $uri /custom_sap_fallbackpage.html;
}

When using the `universal` mode, run `nuxt generate` to create the static site and deploy the contents of the `dist` folder by default.

In version 2.13.0, Nuxt introduced the `target: static` feature. Be sure to explore more about it here.

If you enable `fallback: true`, Nuxt will use `404.html` as the fallback page unless you override nginx's default 404 page. Some services like Netlify take advantage of this behavior for SPA integration. For nginx users, setting up a custom fallback page is recommended for mixing static sites and SPAs seamlessly.

This strategy involves pre-rendering static pages while treating dynamic routes as unknown for nginx, which then uses the fallback SPA page for rendering.

Remember to handle real unknown routes appropriately.

Handling a specific set of dynamic routes

You can follow a function-based approach to generate the static site.

Opting for a pure SPA experience

If you only want a SPA site, consider using the `spa` mode and refer to the recommended nginx configurations outlined here.

Answer №3

It seems like there is some confusion here. The default mode for Nuxt.js is actually universal, not spa as mentioned. You can refer to the official documentation for more information.

In order to run a universal app, you will need to use npm run start. On the other hand, for spa mode, you simply need to have a single html file and route all requests from nginx to that file.

If you are using npm run start, it means you are running a universal app and not hosting a simple html file via nginx. Instead, you should set up a downstream source for nginx and route all requests to your node server, typically located at localhost:3000.

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

What is the hexadecimal color code for a specific element's background?

How can I retrieve the background color code of a specified element? var backgroundColor = $(".element").css("background-color"); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="elemen ...

TemplateUrl malfunctioning

While experimenting with basic Angular code, I encountered an issue where everything was functioning correctly when using template, but not when switching to templateUrl. I did not provide the previous code as it was working fine except for this specific c ...

What is the best way to partition JSON data from an API request in React and display it in various sections within the component

There are 2 JSON objects that contain sales period details based on Month to date and year to date. The data includes information such as Units Sold, Gross Revenue, Year to Date Totals, Month to Date Averages, Expenses, Net Revenues, and Per Unit values. I ...

Next.js deployments on Vercel are encountering issues with resolving local fonts

Currently, I am facing an issue while trying to incorporate next/fonts into my project in next.js 13.3. The setup works perfectly on my local machine, but as soon as I deploy it to Vercel, a build error arises with the message Module not found: Can't ...

"Enhancing Forms with Multiple Event Listeners for Seamless Sub

I'm working on creating a countdown timer with 3 buttons for controlling the timer: start, cancel, and pause. The timer itself is a text input field where you can enter a positive integer. I need to use core JavaScript, not jQuery Start Button: Init ...

What is the best way to incorporate padding into an Angular mat tooltip?

I've been attempting to enhance the appearance of the tooltip dialog window by adding padding. While adjusting the width and background color was successful, I'm encountering difficulties when it comes to styling the padding: https://i.sstatic.ne ...

Is it considered safe to modify variables by using this[varName] = something within a function that includes varName as a parameter?

As I continue working on this function, a question arises regarding the safety of changing variables in this manner. In my Angular service, I utilize utility functions where context represents this from the component calling the function. The code snippet ...

The $scope.$watch function does not activate with each individual change

Yesterday, I realized that in my angularJS 1.4.8 application, the $scope.$watch doesn't trigger on every change causing a bug to occur. Is there a way to make it work on every change immediately? For example, in this code snippet, I want the function ...

Incorporating external files into Javascript code may cause issues with its functionality

Currently, I have a fully developed PHP theme that I am in the process of designing. Within this theme, I have integrated an image slideshow plugin to enhance its functionality. The following code represents the implementation of the image slideshow: &l ...

Ways to verify a correct email address using ReactJS

I'm currently working on a project using React.js and Next.js. I'm encountering an issue with handling the Axios response in Next.js as it's displaying "[object Object]" instead of the actual response data. How can I properly handle the resp ...

Is there a way for me to showcase a collection of pictures in an array format

I am facing an issue on my react page where the data is successfully fetched from an API, but I am having trouble fetching the array of images. Below is the code snippet that I have written: import React, {Component} from 'react'; export defaul ...

Implementing a post request triggered by a button click in Node.js with Express

How can I invoke a controller from a button click event? I tested it in Postman and it works fine, but I'm having trouble calling it from a button on my front-end. I am using Nodemailer and Node Express to send emails. Here is my code. Can someone p ...

Issue displaying daily data on Google bar chart

Here is the link to my code on JSFiddle: https://jsfiddle.net/vbdy7fLe/1/ I am attempting to utilize a Google column chart to display multiple fields of data organized by date. However, I am facing an issue where the dates 02 Jan 2015 and 04 Jan 2015 are ...

Sort through a list of objects by certain properties

I'm currently dealing with two arrays: one contains displayed columns and the other contains objects retrieved from a database, with more attributes than the displayed columns. displayedColumns = ['CompanyName','Ticker', 'Id& ...

Child object referencing in JavaScript

As I delved into testing Javascript, a curiosity arose regarding the interaction between child and parent objects. Would the parent object dynamically update to reflect changes in the child object's value, or would it remain static at the initial stat ...

Look for and choose various fields from a lengthy JSON object

I am working with a JSON object that contains a large list of offerValue objects. { "Code": 0, "response": "SUCCESS", "offerValue": [ { "id": "111", "name": "ABC", "flag": "V" }, { ...

How come the data I send gets converted to Undefined when working with Tabulator?

I am currently facing an issue with integrating JSON data as search results into my Tabulator. The goal is to display these search results in their respective columns within the Tabulator. Here is the code snippet I have implemented: <body> <div ...

Reset Vuetify toggle button if API call is unsuccessful

Hello, I am currently working with Vue and Vuetify 2. Within my project, I have a simple component that utilizes Vuetify's v-btn-toggle: <template> <v-btn-toggle v-model="localProp"> <v-btn value="1">Value1& ...

What steps can be taken to customize the default keyboard shortcuts functionality in Swiper.js?

I am trying to customize the functionality for left/right keys in Swiper.js but I am unable to find a way to do this through the API () It seems that the API only allows you to disable/enable default actions: mySwiper.keyboard.enabled // Whether th ...

Is there a way for me to delay the return from eval() until a callback is received?

A script in node.js has been created to retrieve JS code from a file and then run it through an eval(). The code that handles passing the JavaScript code to the eval function looks like this: // Read JavaScript code from a file var outputBuffer = '&ap ...