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

app.render from '/' to '/something' do not work #9648

Closed
6be709c0 opened this issue Dec 6, 2019 · 2 comments
Closed

app.render from '/' to '/something' do not work #9648

6be709c0 opened this issue Dec 6, 2019 · 2 comments

Comments

@6be709c0
Copy link

6be709c0 commented Dec 6, 2019

Bug report

Describe the bug

I use the parameters [langs] in my project to use multi lang.
So If I can easily access: /fr /fr/about, /en, /en/about

Thing is, I want my default locale to redirect to / and /about.
So I use a custom router to do that and the following method:

app.render(req, res, `/en/${pathname.slice(1)}`, query);

But when I access to /, it doesn't redirect me in the app into /en.
However, it work with /en/about > /about, and it work within the app.

To Reproduce

  • Use [lang] as parameters and have a [lang] directory
  • server.js
const { createServer } = require("http");
const { parse } = require("url");
const next = require("next");

// locales =  ['en','fr']
const { locales } = require("./translations/config");

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

app.prepare().then(() => {
  createServer((req, res) => {
    // Be sure to pass `true` as the second argument to `url.parse`.
    // This tells it to parse the query portion of the URL.
    const parsedUrl = parse(req.url, true);
    const { pathname, query } = parsedUrl;

    const pathNameSplit = pathname.split("/");
    if (
      (pathNameSplit[pathNameSplit.length - 1].indexOf(".") < 0 &&
        pathname.indexOf("_next/webpack-hmr") < 0) ||
      !pathname
    ) {
      if (locales.indexOf(pathname.slice(1, 3)) < 0) {
        // Display english version if the lang is not detected
        console.log("Render", `/en/${pathname.slice(1)}`);
        app.render(req, res, `/en/${pathname.slice(1)}`, query); // Here is my issue /en is not redirected to /en in the app but to /
      } else if (pathname.slice(1, 4) === "en/" || pathname == "/en") {
        res.writeHead(301, { Location: `/${pathname.slice(4)}` });
        return res.end();
      }
    }
    handle(req, res, parsedUrl);
  }).listen(3001, err => {
    if (err) throw err;
    console.log("> Ready on http://localhost:3001");
  });
});
@timneutkens
Copy link
Member

This is more so a question which belongs on https://spectrum.chat/next-js. When creating issues please make sure a clear and concise full reproduction is provided otherwise it becomes incredibly hard to help.

I guess this specifically will be covered by #9081

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants