Error when Vue 3 router does not redirect to fallback page due to a "cannot GET"

I've encountered a problem with setting up a fallback route to the home page on my static SPA webpage. Despite trying various solutions found online, I keep getting the CANNOT GET error when navigating to any URL other than the main one. The following URLs illustrate the issue:

  • www.mywebpage.com -> This works
  • www.mywebpage.com/services -> This will not work if entered directly in the browser

While navigation from the main page works fine (e.g., going from www.mywebpage.com to www.mywebpage.com/services), the issue arises because the browser is unable to locate a nonexistent page when it should be landing on the index.html file.

I attempted to address this by adding the following route configuration:

path: '/:pathMatch(.*)*', 
name: 'home', 
component: landing

In addition, I set up redirects as follows:

{
    path: '/',
    redirect: '/page-home'
  },
  {
    path: '/index.html',
    redirect: '/page-home'
  },
  {
    path: '/page-home',
    name: 'Home',
    component: landing
  }

The router was created using history mode:

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  mode: 'history',
  routes
})

Answer №1

Many thanks to the comment left by IVO GELOV. It hadn't occurred to me that setting up a web server fallback would be necessary. After implementing this, I successfully redirected error pages to index.html and managed to solve the problem.

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

Preventing sibling element events in JavaScript

I have a list that displays events, with an animation sequence triggered upon hovering over the event. Subsequently, social icons appear. However, when I hover over the social icons, the mouse leave event is triggered on the other sibling causing the socia ...

Tips for creating a horizontal list within a collapsible card

When a user clicks on a button, I am dynamically populating a list of items. Despite using list-group-horizontal, I am unable to make it display horizontally. HTML code <div id="textarea_display" class="mt-2 "> <label&g ...

Momentjs initially indicates that the date is valid, logs the correct date, and displays the accurate duration using .fromNow(). However, this suddenly switches

I am currently working on converting the createdAt date in my Nuxtjs application that is fetched from MongoDB using an express app, with the help of moment.js. Initially, when I check if the date is valid, it shows as valid but then it switches to an incor ...

Which is more effective for transferring data between pages: HTML5 storage or Angular services?

I'm currently exploring ways to transfer data between two pages that have Angular controllers. So far, I've discovered two methods for passing data: through an Angular service (similar to the example found here) using HTML5 storage opti ...

Storing numerous JSON records into an array by parsing them from a file

As a beginner in JS programming, I have a question that may seem trivial. Despite hours of searching online, I haven't been able to find a solution that works for me. I am dealing with multiple JSON feeds where each URL provides a multilayer JSON rec ...

Execute JavaScript script once when route changes

I'm currently working on a system where I want to ensure that my animation-based JavaScript code only runs once after the route changes. The issue I'm facing is that when switching between pages and returning to the about page, the scripts are ca ...

Component in NuxtJS rendering with empty data

One issue I encountered is that my component, which utilizes fetch to retrieve data via Axios and render it, is displaying empty data. Upon further investigation, I noticed that the data() method is being called before the fetch() method. How can this prob ...

remove MongoDB entry using unique identifier

I am currently working on a blog project using nodejs, express, and mongodb. My goal is to delete a specific document by its ID. For instance, if I want to remove a blog post with the ID 52976b1b0855c7e81a6192e9, I would make a request to localhost:3000/bl ...

What is causing the ng-show function to malfunction after building with phonegap?

My current javascript framework for my PhoneGap app is the Ionic Framework. I have a specific item on one of my pages that I want to make expandable and collapsible by clicking an anchor on the page. Here is what the code looks like: In the HTML file: & ...

Issue with event.preventDefault() in Jquery not functioning as expected

My goal is to have the menu display and hide list items on click, with them being hidden by default. However, the issue I am facing is that the menu is generated in the admin section, so it automatically assigns a URL to each item. If I set the URL field o ...

i18next initialization problem detected

I've implemented the i18next script in my jQuery Mobile page within a Cordova/PhoneGap app. At the bottom of my HTML page, I have included the following scripts: <script src="js/jquery-1.8.3.min.js"></script> <script sr ...

Using a PHP array as a parameter in a link for an AJAX function

I am attempting to pass a PHP array to an AJAX function in order to populate dynamically created tables. To achieve this, I need to pass the values in an array all at once to avoid the issue where only the last dynamic table is populated. However, it see ...

The HTMLElement type does not include the Ionic typescript property

I am currently using Ionic v3 with TypeScript. My goal is to store the file_url in an array after checking if its width is equal to twice its height. However, I am encountering an error: The 'name_array' property does not exist on the ' ...

Difficulty arises when trying to deploy a React application on a stationary Nginx server

I'm encountering issues trying to deploy my React app on a remote server. My Nginx configuration looks like this: server { listen 80; listen [::]:80; server_name xxx.xxx.x.x; root /var/www/my-site; inde ...

The sourcemap for a Vue file based on TypeScript is not available due to the presence of the lang="ts" attribute

I am facing an issue where I need to transition my vue files from JavaScript to TypeScript. Currently, they have a standard structure like this: <template> ... </template> <script> ... </script> To use them with TypeScript, I le ...

"Troubleshooting: The v-model in my Vue project is not functioning properly when used

I am encountering an issue where the v-model directive is not functioning properly inside a v-for loop. Within my template <li v-for="(data, key) in product.variants" :key="data.id"> <input type="radio" :id=" ...

Trigger the submission of a form on the current php page when a checkbox is altered

Looking for assistance with the following code: <form id="form" method="post" action=""> <input type="checkbox" name="checkbox" onchange="$('#form').submit();"> <label class="onoffswitch-label" for="myonoffswitch"> <span ...

modify the code to accommodate the update to angular-ui-router version 2.0.13

Looking for some assistance with debugging the code in my plnkr. Check out my Plnkr I suspect there may be issues with the stateProvider configuration. validationApp.config(['$stateProvider', function ($stateProvider) { After updating to ui-r ...

Unable to set up cordova using npm version 5.6.0

I'm encountering issues trying to install Cordova on my Linux system (Deepin 15.5). Can anyone offer assistance? $ sudo npm install -g ionic /usr/local/bin/ionic -> /usr/local/lib/node_modules/ionic/bin/ionic npm WARN optional SKIPPING OPTIONAL ...

Eliminate rows in an HTML table that contain "undefined" using Javascript

I am facing an issue with my HTML table created using JS. Despite not having any corresponding data in the JSON source, several rows are filled with "undefined" values in the output. These rows need to be removed. Here is the JS code: const requestUrl9 = ...