nuxt-community / redirect-module

No more cumbersome redirects for Nuxt 2!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Also affects API calls?

SHxKM opened this issue · comments

commented

I have the following in nuxt.config.js:

redirect: [
    {
      from: '^(/.+){2,}/$',
      to: (from, req) => {
        return req.url.substr(0, req.url.length - 1)
      }
    },
    {
      from: '/page/1$',
      to: "/"
    }
  ],

This basically insures frontendsite.com/pages/2/ is redirected to frontendsite.com/pages/2, etc...for every page except homepage.

The problem is, this rule seems to be affecting my internal API calls as well, which I make in vuex store. It's stripping apisite.com/api/accounts/auth/login/ to apisite.com/api/accounts/auth/login, which is causing an infinite loop of redirections in the backend.

How can I make sure these rules only apply to "incoming" requests, or only client side requests?

You can adapt the regex and make it more strict, so this won't happen.

redirect-module is a server middleware, meaning that it'll be executed on all request hitting the Node.js server. If you use proxy as middleware and execute it after the redirect module, then the redirect will take place first.

commented

@manniL any idea/hint? (I know some regex, not sure what I need to adapt here) I ended up just giving up on this redirection in favor of listing a canonical URL in meta tags.

@SHxKM You can for example exclude anything that starts with /api/:

from: '^(?!/api/)(/.+){2,}/$',