Nuxt/Vue-Router Navigation Guards Reroute to Dynamic Route - vue.js

I currently develop an SPA web application using nuxt 2.7.1 as part of my bachelor's thesis.
On some of my pages I have set up beforeRouteEnter navigation guards to check whether a user may access the page. If not, I want to redirect the user to a dynamic route (namely /info/1).
When accessing the page protected by the navigation guard, Nuxt displays an error page stating This page could not be found, although the URL is correct and a simple reload at the error page correctly leads me to the dynamic route.
I am currently using the navigation guard in the following way:
beforeRouteEnter(to, from, next) {
if (!isValidAccess()) {
console.warn("illegal access");
next(`/info/1`);
} else next();
}
For clarification, my directory structure looks as follows:
pages
|__ info
| |__ _index.vue (redirect target)
|__ shop
|__ _index.vue (with navigation guard)
I have tried experimenting with the argument passed to next() but so far without success. When implementing another beforeRouteEnter in the target route and logging the to parameter, everything seems normal.
I hope you guys can help me. I already spent too much time on this issue.
Best regards
EDIT:
I have created a minimal example, where the routing to a dynamic route works without problems... https://codesandbox.io/embed/codesandboxnuxt-t1d0e
I will update the question when I find the solution to my problem.

This works for me - on landing page (index) you get redirected to your /info/1 dynamic route. Is this what you want?
https://codesandbox.io/embed/codesandboxnuxt-vvbrt
The only thing I've done was wrapping your dynamic route in <no-ssr> tag to get rid of an error. Your component is not compatible with SSR.

Related

__vite_ssr_import_1__.Client is not a constructor Error

I am developing a simple app to showcase CRUD operations with Appwrite and Nuxt 3 (Release Candidate 11). The source code to the same can be found here.
I am using the landing page of the app (index.vue) for simple redirection i.e. if the account exits in the accountStore, I want to redirect the user to /workouts route, else ask them to login by redirection to the /login route.
The problem I am facing is when I am landing on the index.vue page (with no user session in progress in Appwrite), I am still getting redirected to /workouts route, instead of the /login route. I presume the reason for the same is the following error that is being logged in the console.
I need help in figuring out from where is the error message originating and how to possibly remedy it.
Here is my public github repo: https://github.com/EshaanAgg/workout-buddy
Maybe this is a bug with Nuxt 3 or Vite since Nuxt 3 is currently only a release candidate.
Also, in my opinion, Server Side Rendering (SSR) always complicates things so much. It doesn't look like you're using SSR, so you could possibly update your nuxt.config.ts to disable SSR:
{
// ... other stuff
ssr: false
}

How would I make Vue Router work with GitHub Pages?

I just deployed my Vue app to my website using GitHub Pages.
The website is successfully hosted at https://astroorbis.com.
Here's the problem; When you click the "links" button at the top of the page, it successfully nagivates you to https://astroorbis.com/links, but when you try visiting the URL itself (typing in https://astroorbis.com/links) into your browser, it returns a 404.
There are other links that have the same error, such as /discord, /github, etc.
I tried the solution at Vue Router, GitHub Pages, and Custom Domain Not Working With Routed Links, but it failed as well.
What would be the solution for this?
As stated in this section of the HTML5 mode
Here comes a problem, though: Since our app is a single page client side app, without a proper server configuration, the users will get a 404 error if they access https://example.com/user/id directly in their browser. Now that's ugly.
Not to worry: To fix the issue, all you need to do is add a simple catch-all fallback route to your server. If the URL doesn't match any static assets, it should serve the same index.html page that your app lives in. Beautiful, again!
So, the solution would be to use something like that
const routes = [
// will match everything and put it under `$route.params.pathMatch`
{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
]
On Netlify, you also need to add the following for it to work
/public/_redirects
/* /index.html 200
So I'm not sure about Github Pages but you should have something similar there, some way of catching all routes and sending them to the index.html of your initial SPA page load.
Otherwise maybe just give a try to Netlify with the _redirects configuration.
Maybe this article could help regarding Github pages.
The hack in your given link seems to be the only viable solution but it's still bad for SEO so yeah, depends if you want any (I guess so).
In that case, you could try Nuxt.js, Gridsome or Vitesse if you want to have some statically generated pages (best approach regarding SEO).

VueJS Router cuts query params on live site, works on localhost

In my VueJS app I want to confirm a registration by traditional confirmation link.
This link looks like this
https://dev.xyz.com/auth/register-confirm?some=Data
Inside my register-confirm component I got the following method
beforeCreate () {
const query = this.$route.query // Is empty object on live page, works on localhost
this.confirm(query)
},
It is also very weird, that when I copy the original link to the browser and enter the site, it automatically cuts the query parameter, so the link looks like this
https://dev.xyz.com/auth/register-confirm
no matter what I type in as a query parameter.
Found the issue. We are hosting the site on AWS Amplify. It's caused by the rewrites / redirects.
GitHub Issue

Error 404 on a page that exists and that works fine through internal link

I created a website with several pages on Vue.js.
Everything is working fine locally, but when I deploy to Heroku, all pages are only working when I click on an internal link in my menu that redirects to the corresponding page (using router push).
When I try to access directly /any-page from the browser I get a 404 with a message saying "Cannot GET /any-page" whereas the same page is displayed correctly via a click on a link.
As I mentioned when I locally serve my app I don't have this problem.
I really can't see where this can come from, thanks in advance for your help.
There's a deployment guide specifically for Heroku in the official Vue CLI documentation.
You'll quickly notice the relevant information:
static.json
{
"root": "dist",
"clean_urls": true,
"routes": {
"/**": "index.html"
}
}
For SPA's (Single Page Applications), you'll want to point every route to the index. Vue router will take care of navigating to the proper page.
Heroku is serving the contents of your Vue build folder. Since Vue builds the app as a single index.html file, only the main route works.
Vue doesn't actually navigate to the route, it rather rewrites the the browser url using the history API and handles the loading of the new route.
You could use one of these options:
OPTION 1
You could use mode: "hash" to fix routes when reloading the page. However this will add a # before every route.
const router = new VueRouter({
mode: "hash",
routes: [...]
})
OPTION 2
Write an Node.JS (eg Express) app that routes every request to your index.html file. This is called a middleware
Reference: https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations

Vue Router Push method within Nuxt causing a page reload

Working on a Nuxt project and trying to programmatically change route.
In a method I am using this:
$nuxt.$router.push({
path: `vehicles-for-sale`
})
This looks like it initially works, but it then causes a page refresh to '/vehicles-for-sale?.
Is there anything different I need to do with Nuxt to get this working? I have a pages folder called 'vehicles-for-sale' and an index.vue file within that.
Simply use this:
this.$router.push('vehicles-for-sale')