Giter Site home page Giter Site logo

redirectTo not working, Throwing Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client about after.js HOT 4 OPEN

qualwebs avatar qualwebs commented on June 15, 2024
redirectTo not working, Throwing Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

from after.js.

Comments (4)

qualwebs avatar qualwebs commented on June 15, 2024

@jaredpalmer If someone can look into please :)

from after.js.

blinkcat avatar blinkcat commented on June 15, 2024

@qualwebs This is a bug indeed. if your code return { redirectTo: '/login' }, res.redirectwill be called before res.send.
meanwhile, the html variable will be empty string, so we can try this:

// your server.js in src
server
  .disable('x-powered-by')
  .use(express.static(process.env.RAZZLE_PUBLIC_DIR))
  .get('/*', async (req, res) => {
    try {
      const html = await render({
        req,
        res,
        routes,
        assets,
        chunks,
      });
       if(html){ // add a judgement here
         res.send(html);
      }
    } catch (error) {
      console.error(error);
      res.json({ message: error.message, stack: error.stack });
    }
  });

from after.js.

jschroed91 avatar jschroed91 commented on June 15, 2024

We ended up using a very similar solution to what @blinkcat posted here.

But instead of if(html){ we did

if (res.statusCode == '302' || res.statusCode == '301') {
  return;
}

res.send(html);

I'm using == there instead of strict comparison === because I don't recall what type res.statusCode was , but obvi a strict comparison would be better. But you get the point

I think in our case html was still getting a value, so that's why we went this route.

from after.js.

stale avatar stale commented on June 15, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from after.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.