omarryhan/nextjs-i18n-example

use rewrite functionality for pain #2?

ujwal-setlur opened this issue · 5 comments

First, very very cool stuff. I haven’t tried it yet, but I read the code, and it makes sense. I will prototype this later tonight in my project.

Regarding pain #2:

Redirecting from '/' to '/ar' or '/en' is easy. But what if the user goes to '/ssr' and not '/[language]/ssr'. It should redirect to /[language]/ssr and not return a 404.

Could you potentially use the new rewrite/redirect functionally in NextJS 9.5.0?

Oops, I seem to have missed the notification for this issue, sorry.

Answering your question, yes, you can. I tried it in a quick example and it works. I'm open for a pull request if you want to add this feature :)

This is what my next.config.js currently looks like:

module.exports = {
  async rewrites() {
    return [
      { source: "/:lang?/:path*", destination: "/:path*" },
      { source: "/:lang?", destination: "/" },
    ];
  },
};

I originally didn't implement it with rewrites in mind because the feature wasn't there at the time, not even in Next@canary.

I think that method is a bit prohibitive though. Let's say you want to create a sitemap.xml in the root of your website, using the rewrite functionality above, the /sitemap.xml route will have a language context. Is there a way to make an exception for certain routes?

Good point. I think we can put conditionals in rewrite logic. Haven’t tried it though.

Not sure if that's possible. It's not mentioned neither in the RFC nor in the docs. But given it's a .js file, then a simple if should probably work fine. I will give it a try and let you know how it goes.