Skip to content

Cannot set headers after they are sent to the client error occurs a lot #4246

@1070rik

Description

@1070rik

Current behavior

Currently in our bug tracking software we see this error a lot. (47k occurrences in like 3 weeks)
It seems to happen when a 404 or error page gets rendered according to the error.

image

Don't know if I'm correct but if you do a res.redirect or something like that, you need to do a return after that so it doesn't try to do another redirect?
So if you would want to do a 404 or 500 redirect the errorHandler in server.ts would look like this:

  const errorHandler = err => {
    if (err && err.code === 404) {
      if (NOT_ALLOWED_SSR_EXTENSIONS_REGEX.test(req.url)) {
        apiStatus(res, 'Vue Storefront: Resource is not found', 404)
        console.error(`Resource is not found : ${req.url}`)
        return;
      } else {
        res.redirect('/page-not-found')
        console.error(`Redirect for resource not found : ${req.url}`)
        return;
      }
    } else {
      res.redirect('/error')
      console.error(`Error during render : ${req.url}`)
      console.error(err)
      return;
    }
  }

I don't have a lot of experience with express so I might be wrong.

Expected behavior

It shouldn't happen at all.

Steps to reproduce the issue

Run VSF in a production environment.

Repository

Can you handle fixing this bug by yourself?

  • YES
  • NO

Which Release Cycle state this refers to? Info for developer.

Pick one option.

  • This is a bug report for test version on https://test.storefrontcloud.io - In this case Developer should create branch from develop branch and create Pull Request 2. Feature / Improvement back to develop.
  • This is a bug report for current Release Candidate version on https://next.storefrontcloud.io - In this case Developer should create branch from release branch and create Pull Request 3. Stabilisation fix back to release.
  • This is a bug report for current Stable version on https://demo.storefrontcloud.io and should be placed in next stable version hotfix - In this case Developer should create branch from hotfix or master branch and create Pull Request 4. Hotfix back to hotfix.

Environment details

  • Browser: Chrome 81.0.4044.92
  • OS: Mac os 10.15.1
  • Node: V10.19.0
  • Code Version: V1.11.0

Additional information

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3: NormalPriority mark - normal prioritybugBug reports

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions