Skip to content

Fix HTTP server errors when trying to do a HTTP redirect from components#289

Open
sneko wants to merge 1 commit into
Akryum:masterfrom
sneko:fix/http-redirections
Open

Fix HTTP server errors when trying to do a HTTP redirect from components#289
sneko wants to merge 1 commit into
Akryum:masterfrom
sneko:fix/http-redirections

Conversation

@sneko

@sneko sneko commented Jun 10, 2022

Copy link
Copy Markdown

Hi,

It's to fix this issue when doing HTTP redirections:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:482:11)
    at ServerResponse.header (/Users/XXXXXXXXXX/Documents/YYYYYYY/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/Users/XXXXXXXXXX/Documents/YYYYYYY/node_modules/express/lib/response.js:170:12)
    at renderer.renderToString (/Users/XXXXXXXXXX/Documents/YYYYYYY/node_modules/@akryum/vue-cli-plugin-ssr/lib/app.js:119:15)
    at /Users/XXXXXXXXXX/Documents/YYYYYYY/node_modules/vue-server-renderer/build.dev.js:9536:15

It was not critical in the past (had this 2 years ago #144 (comment)), it was just logging things into the console. But since I upgraded some Vue dependencies the "simple log" is now something making the server crashes.

Note: my previous version of vue-server-renderer was v2.6.11 and I tried upgrading to v2.6.14. Don't know which change exactly breaks things... but it's fine with this PR 😄

So to avoid being stuck in a previous version I did a little trick to always respect values that could be passed to the res object.

Hope this helps even if the repo is no longer maintained.

In the meantime if you want to you it you can use a specific branch with some patches I made:

yarn add "@akryum/vue-cli-plugin-ssr@https://github.com/sneko/vue-cli-plugin-ssr#fix/http-redirections"

👍

@JeffJassky

JeffJassky commented Oct 2, 2023

Copy link
Copy Markdown

Here was my hacky solution that didn't require changing the main libs:

in vue.config.js:

module.exports = {
  pluginOptions: {
    ssr: {
      onRender(res, context){
        if(context._routeRedirect){
          res.redirect(301, context._routeRedirect); // do the redirect
          const send = res.send; // store local reference to original send method
          // override send method to prevent headers from being sent
          res.send = function(){
            res.send = send; // restore the original send method for subsequent calls
          }
        }
      },
      ... other configs
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants