Mostafa Said
Mostafa Said

Reputation: 1015

How to redirect to another route onDecodeError in nuxt2 using redirect-module

I have a project that is built on nuxt 2 and I'm having an issue where I'm getting a lot of URI Malformed errors on Sentry from routes that are valid but has %2 at the end of it which causes the error.

So I need a way to remove the %2 whenever there is an error while decoding and redirect the user to that route.

How can I redirect the user to a different route onDecodeError?

I tried to do something like this:

        onDecode: (req, res, next) => decodeURI(req.url),
        onDecodeError: (error, req, res, next) => {
            if (/^.*(%2)$/.test(req.url)) {
                res.setHeader('Location', encodeURI(req.url.slice(0, -2)))
            } else {
                next(error)
            }
        },

Upvotes: 0

Views: 176

Answers (1)

Mostafa Said
Mostafa Said

Reputation: 1015

Got that sorted out

            if (/^.*(%2)$/.test(req.url)) {
                res.setHeader('Location', encodeURI(req.url.slice(0, -2)))
                res.statusCode = 302
                res.end()
            } else {
                next(error)
            }

Upvotes: 1

Related Questions