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

Is there a way to make localeSubpaths from the beta compatible with getStaticPaths/-Props? #747

Closed
borispoehland opened this issue Jul 5, 2020 · 13 comments

Comments

@borispoehland
Copy link

The use of publicRuntimeConfig is mostly to facilitate e2e tests internally. How you propagate localeSubpaths is entirely up to you.

@isaachinman is there an alternate way (you can think of) to propagate localeSubpaths that's compatible with getStaticPaths?

Originally posted by @borispoehland in #274 (comment)

@isaachinman
Copy link
Contributor

Why not hard code it, or treat it as any other variable?

@borispoehland
Copy link
Author

borispoehland commented Jul 5, 2020

The problem is this snippet:

// next.config.js
experimental: {
    async rewrites() {
      return [
        ...nextI18NextRewrites(localeSubpaths),
      ];
    },
  },

because it calls res.writeHead, which is not available with pages prerendered with getStaticProps/getStaticPaths.
The build-error is: TypeError: res.writeHead is not a function

As long as localeSubpaths is an empty object - no problem. Once it's populated, the error is thrown.

Can you think of any solution or workaround?

Edit: Even when commenting out the snippet, the error still arises. Unfortunately, I don't know where it comes from

@isaachinman
Copy link
Contributor

@borispoehland I don't understand what you mean. localeSubpaths is just a normal JavaScript object.

@borispoehland
Copy link
Author

Yes, I know.

When running next build with localeSubpaths being empty, everything works fine.
When running it with localeSubpaths being populated, the TypeError: res.writeHead is not a function is thrown.

I don't know why this is the case, tho...

@isaachinman
Copy link
Contributor

Can you provide a reproducible repo?

@borispoehland
Copy link
Author

borispoehland commented Jul 6, 2020

Yes, here: https://github.com/borispoehland/next-i18next-getStaticProps-Error

npm run build leads to an error, due to getStaticProps being used.

Thanks Isaac for investigating!!

@isaachinman
Copy link
Contributor

@borispoehland Very strange that they don't provide a stack trace with that error.

Anyways, it looks like the problem here is that redirectWithoutCache relies on res.writeHead (naturally), and this is incompatible with a fully static app. If you want locale subpaths, we need some sort of server (or serverless) runtime in which we can execute a redirect when necessary.

Hope that makes sense!

@borispoehland
Copy link
Author

@isaachinman but my app isn't fully static, only one single page of it is (the page with getStaticProps).

I'm relatively new to next.js, for the particular situation that I'm stuck in, can you provide some configurational workaround?

@isaachinman
Copy link
Contributor

We cannot generate a static page at all if you desire localeSubpaths. That's just a natural conclusion of the functionality itself.

There is no workaround - the redirect functionality has to exist at some level. If you wanted a static app that still supports localeSubpaths, you'd need to move that functionality up a level, and rewrite it at, for example, an nginx level.

@borispoehland
Copy link
Author

@isaachinman thanks, that's the clear answer I was looking for!

Fortunately, I was able to replace getStaticProps with an API call. So I basically found a workaround myself, but I wanted to make sure that they are indeed incompatible (localeSubPaths and getStaticProps)

Thank you for your support!

@isaachinman
Copy link
Contributor

No problem. The question of redirects is an interesting one, and may end up being the last piece of the puzzle when it comes to static/SSG locale subpath support. I can discuss with the NextJs team.

Good luck with your project!

@borispoehland
Copy link
Author

@isaachinman Is there a bank account of you that I can donate some euros to? I think you make great work

@isaachinman
Copy link
Contributor

@borispoehland You can sponsor my work via GitHub sponsors, or contact me directly via email. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants