Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to provide config for i18next-express-middleware #68

Closed
thebat93 opened this issue Dec 22, 2018 · 13 comments
Closed

Ability to provide config for i18next-express-middleware #68

thebat93 opened this issue Dec 22, 2018 · 13 comments
Assignees
Labels
bug Something isn't working

Comments

@thebat93
Copy link

Hi! Thanks for the great library!

I'm experiencing the following problem:
The first request that I send to the Express server sets the language cookie correctly.

image

But other requests to internal URLs that include "_next" in their route set the incorrect cookie afterwards.

image

So, as a result the cookie of the page is the incorrect one.

image

I think I'm experiencing this because each of my requests is processed by the i18next-express-middleware. I noticed that there is a way to specify routes for the middleware that you want to ignore in the config (https://github.com/i18next/i18next-express-middleware#wire-up-i18next-to-request-object) but I didn't find the same functionality here.

Could I suggest adding this feature to the library or maybe there is another way of resolving my issue?

@isaachinman
Copy link
Contributor

Very strange, I remember writing this regex to fix that exact issue.

Moreover, I cannot seem to reproduce this bug in the example:

screen shot 2018-12-22 at 18 55 53

Can you please provide a demo repo that reproduce this bug? I'm sure we can get this fixed very quickly. Thanks for your patience.

@isaachinman isaachinman self-assigned this Dec 22, 2018
@isaachinman isaachinman added the bug Something isn't working label Dec 22, 2018
@thebat93
Copy link
Author

Thank you for your answer. Here's the minimal setup that I'm using: https://github.com/thebat93/next-i18next-example

I think the problem is that I'm not actually using localePaths. For my project I need to have a locale subpath for the default language too with redirections from paths without locale subpaths in them. When I set localePaths to true the regex that you mentioned is applied and the cookie is set correctly, but I don't get the feature that I want. I saw the open PR with just the feature that I want. Maybe it will be a solution for me.

@isaachinman
Copy link
Contributor

@thebat93 Thanks - quick fix with 54f26f2.

In the future we might want to use a req, res, next middleware to validate the ignoreRoutes setting on i18next-express-middleware as this will still be a problem for people using custom routes for assets, etc.

I think to do this we'd need the full list of page routes. As per this comment, I think we'd have to do something like:

const pages = await glob('pages/**/*.js', { cwd: __dirname })

Which is not an approach I care to get into at the moment. If more people run into this issue we can readdress it.

@thebat93
Copy link
Author

Thanks for the quick fix. Unfortunately it resolved the problem only with routes that have '/_next' and '/static' in them, but I also have routes that start with '/fonts' and '/v0'. These routes still mess up the cookie.

@isaachinman
Copy link
Contributor

Did you read my comment...? I addressed that exact problem.

@thebat93
Copy link
Author

I did, but I don't see how it applies to the problem that I need to solve now. The cookie is still set incorrectly. If I use i18next-express-middleware and add ignoreRoutes for '/_next', '/static' and '/fonts' everything works as intended. I just wish there was a way to configure the middleware in your library. Right now I can't fully apply it to my case and have to use the middleware for Express.

@anthonywebb
Copy link

@thebat93 I am very curious where you added ignoreRoutes? I'd like to ensure that '/_next', '/static' and '/fonts' are not being processed for my app as well!

@isaachinman
Copy link
Contributor

@anthonywebb There's currently no way to do this in next-i18next - I assume that @thebat93 meant they were rolling their own implementation.

I can add an options object to the nextI18NextMiddleware function, and allow users to pass in routes to ignore. I suppose that's the only sensible thing to do for now. Besides /static and /_next, it's not safe to ignore any route by default.

@thebat93
Copy link
Author

It's not possible to provide the config object with ignoreRoutes to the middleware using this library.
But you can simply process all the routes that you need to ignore before passing them to the nextI18NextMiddleware.

So, something like this:

const app = next({ dev: process.env.NODE_ENV !== 'production' })
const handle = app.getRequestHandler();

(async () => {
  await app.prepare()
  const server = express()

  // Ignored routes
  server.use(express.static(__dirname + '/public'));

  server.use('/locales', express.static(__dirname + '/locales'));

  server.use('/v0/', someApiMiddleware);

  server.use('/fonts', express.static('./node_modules/fonts/.../'));
  
  // Middleware
  nextI18NextMiddleware(nextI18next, app, server)

  server.get('*', (req, res) => handle(req, res))

  await server.listen(3000)
  console.log('> Ready on http://localhost:3000')
})()

@isaachinman
Copy link
Contributor

I'll be releasing a fix momentarily.

@anthonywebb
Copy link

Thanks @thebat93 @isaachinman this will be great. The interim fix of just including the middleware after the routes that you want to ignore is fine for me so no rush.

@isaachinman
Copy link
Contributor

309ed4b

@isaachinman
Copy link
Contributor

Released in v0.15.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants