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

Can't access locale files in public directory from nested routes using getServerSideProps #359

Closed
hrahimi270 opened this issue May 30, 2021 · 10 comments
Labels
priority: low type: bug code to address defects in shipped code

Comments

@hrahimi270
Copy link

Describe the bug
I'm using the built-in feature of internationalization of Nextjs. I don't want to use other third-party modules for translations of my app.
So I decided to put my translations in /public/locales and access them using getServerSideProps for dynamic pages and getStaticProps for static pages.
Static pages work fine. But dynamic pages not working and they are not able to access the file using getServerSideProps.
This is the error I have on netlify:

Screen Shot 2021-05-30 at 12 11 25 PM

Also, I'm trying to get my translations simply by this lines of codes in both pages:

const dir = path.resolve('./public/locales')
const filePath = `${dir}/${locale}.json`;
const buffer = fs.readFileSync(filePath);
const translations = JSON.parse(buffer.toString());

Expected behavior
Have my translations in both Static and Dynamic pages.

Versions

  • Next.js: 10.2.0
  • @netlify/plugin-nextjs: 3.3.0
@lindsaylevine lindsaylevine added the type: bug code to address defects in shipped code label May 30, 2021
@lindsaylevine
Copy link

hey @hrahimi270 ! good news, we just recently released support for this :). check out this section of the README: https://github.com/netlify/netlify-plugin-nextjs#local-files-in-runtime. throwing "public" in included_files should solve your problem! if that works for you, we could possibly update this issue to a feature request asking for public to be included by default? let me know!

@hrahimi270
Copy link
Author

Hey @lindsaylevine. Thanks for the response.
Actually, I did this before when I have this problem in all of the pages (both static and dynamic). But the problem still remains in dynamic pages.
As you can see in the picture, the app tries to load the locale files from /var/task/public/locales/fa.json.
But once I logged the path that loads the locale files from static pages, and it was something else.
I think in dynamic pages, path.resolve() have a different behaviour :D not sure about that.

@lindsaylevine
Copy link

lindsaylevine commented May 30, 2021

@hrahimi270 hmm, can you try path.resolve(process.cwd(), '/public/locales')? or maybe not using path.resolve? i can't remember at the moment what the correct usage. this uses process.cwd() and i know we had a next-i18next issue that worked with included_files as public/*. if that doesn't work, if you wanted to give me a repo to try to figure out, i could maybe better help that way!

@hrahimi270
Copy link
Author

Then I will test path.resolve(process.cwd(), '/public/locales') just like the link that you mentioned.
If it didn't work, I will create a reproduce repo to check it closely (:
Thanks.

@hrahimi270
Copy link
Author

Hey @lindsaylevine.
I did test path.resolve(process.cwd(), '/public/locales') but was not successful. So, I created a reproduce repo that you can have a look closely.
https://github.com/hrahimi270/next-netlify-reproduce

Also, deployed it on Netlify to see the error when clicking on brands.
https://quirky-williams-edd128.netlify.app/

Thanks for your time.

@eduardoboucas
Copy link
Contributor

Hi @hrahimi270! Thanks for the detailed report and the reproduction repo — these were super helpful.

Can you try to add to your build an environment variable named NETLIFY_EXPERIMENTAL_FUNCTION_HANDLER_V2 with the value true? You can do this in the UI as shown in the screenshot below.

Screenshot 2021-06-02 at 19 08 14

This will enable a functionality that we're planning to roll out next week, and from my tests it should fix your issue. Let me know how you get on.

Thanks!

@hrahimi270
Copy link
Author

Hi @eduardoboucas. Yes, it works fine now with that variable 🤘🏼
So, for now, I just need to use this variable until this functionality gets available by default?
And thanks for your time.

@eduardoboucas
Copy link
Contributor

Hi @eduardoboucas. Yes, it works fine now with that variable 🤘🏼
So, for now, I just need to use this variable until this functionality gets available by default?
And thanks for your time.

I'm glad it works! 🎉

And yes, please use that variable until this change is rolled out. I'll update this issue once that happens.

Thanks!

@eduardoboucas
Copy link
Contributor

Hi @eduardoboucas. Yes, it works fine now with that variable 🤘🏼
So, for now, I just need to use this variable until this functionality gets available by default?
And thanks for your time.

This has now been rolled out.

@hrahimi270 can you please check if it works without the environment variable? Thanks!

@hrahimi270
Copy link
Author

@eduardoboucas
Yes, it works without the variable.
Great job.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: low type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

3 participants