Next-i18next: Cannot set headers after they are sent to the client - ^ 4.3.0

Created on 15 May 2020  ·  21Comments  ·  Source: isaachinman/next-i18next

Describe the bug

Error on server middleware (server.use(nextI18NextMiddleware(nextI18next))) when res.writeHead({ Location: "/" }) from _app.tsx happen in version 4.3.0 (next-i18next) but works fine in 4.2.0 (next-i18next)

Occurs in next-i18next version

next-i18next - 4.3.0
yarn - 1.22.4

Steps to reproduce

Basic setup and add res.writeHead(302, { Location: "/" }); in _app.jsx

Screenshot

Capture d’écran 2020-05-15 à 13 09 23

Expected behaviour

Should work normally like in version 4.3.0 (next-i18next)

OS (please complete the following information)

  • Device: MBP 2019 16
  • Browser: Chrome 81.0.4044.138

Most helpful comment

Many thanks for releasing the new version @adrai! I can confirm that I no longer see log statements with [email protected] and [email protected] 🎉

It might be still worth investigating the situation further. If I understand correctly, we just silenced the unsuccessful header setting attempt. Perhaps, the logic should change so that it did not happen at all?

All 21 comments

Please provide a reproducible repo.

I'm sorry, i just noticed i gave you the wrong version. The issue was happening on version "next-i18next": "^4.3.0".

With 4.4.2, all works fine i just have a message "no possibility found to set header"

_no possibility found to set header_ is quite annoying, especially given that it is printed unconditionally, both in dev and prod 🤔 @cfaure004, did you manage to find the workaround or the root cause?

@kachkaev This is a different issue...

There seems to be a different way for setting the header...

PRs are welcome

Ahh you mean the second time... when the header is already sent...?
Will probably change the logic to return in that case

@kachkaev should be better with [email protected]

Many thanks for releasing the new version @adrai! I can confirm that I no longer see log statements with [email protected] and [email protected] 🎉

It might be still worth investigating the situation further. If I understand correctly, we just silenced the unsuccessful header setting attempt. Perhaps, the logic should change so that it did not happen at all?

This must be somewhere in next-i18next or in userland... 🤷‍♂️
any help is welcome

We'll need @cfaure004 to provide a reproducible example.

About what ? The message "no possibility found to set header" ?

This initial issue is not happening on latest version.

I think we can close this issue.

Can you create an example with the old version which reproduces the error?
This would be very helpful to investigate the root cause.

Ok no problem, i'll do that

👍 make sure you use i18next-http-middleware <= 1.0.6

On my side, i've always used middleware included in next-i18next package

const nextI18NextMiddleware = require('next-i18next/middleware').default

I've been able to reproduced the message "no possibility found to set header" but not to reproduce the initial error

https://github.com/cfaure004/next-i18next-issues-716

Finally i've been able to reproduce the initial issue using [email protected], all is in my repo 👍

On my side, i've always used middleware included in next-i18next package

Yes, that's what you _should_ be doing. We need a repro of that, not using i18next-http-middleware/cjs directly.

thx @cfaure004 I found somethig... it's caused by the redirect... here: https://github.com/cfaure004/next-i18next-issues-716/blob/master/pages/_app.jsx#L48
which seems correct (I'm not a nextjs expert)...

I also found in i18next-http-middleware that certain header values can be set twice (because of the languageChanged event), so I tried to optimize this with [email protected]

On my side, i've always used middleware included in next-i18next package

Yes, that's what you _should_ be doing. We need a repro of that, not using i18next-http-middleware/cjs directly.

@isaachinman I have not been able to reproduce the issue using previous versions of next-i18next library, i think it was an outdated dependency. I just created this exemple to help @adrai to identify where does the issue was coming from.

Great @adrai !

Was this page helpful?
0 / 5 - 0 ratings