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
feat: Public dir support #569
feat: Public dir support #569
Conversation
Didn't realize there was already a PR for this, feel free to close this one. |
Hi @nathanfriemel, thanks for doing this work. I haven't heard from the author of #525 in awhile, so we can proceed with this PR if you're keen. Can you please remove all the function return typings? I appreciate you want to fix TS warnings, but that should be handled in a separate PR as it just makes it harder to review this work. Once that's sorted I will review. Thanks! |
…8next into public-dir-support
Return types are gone. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this looks like very good work. There are some small things to change, and there are some changes in here which I'm not convinced need to be associated with this PR.
In general, I wonder if it's best to drop stuff directly in the public dir, or if we should, by convention, expect locales to be in public/static/locales
. If we did that, a lot of logic could remain the same.
Let me know what you think.
examples/simple/package.json
Outdated
"next-i18next": "link:../../", | ||
"react": "link:../../node_modules/react", | ||
"react-dom": "link:../../node_modules/react-dom" | ||
"next-i18next": "link:../../" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's going on here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upgrading next to version 9.0.6+ was causing the tests to throw this error. Seemed related to this bug in next, a work around was mentioned here. Wanted to test these changes against next 9.1, probably not important, could revert back to 9.0.3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, that's what I figured. I tried to upgrade myself, but ran into that bug.
I'm not understanding how this runs at all without react
and react-dom
as deps.
package.json
Outdated
@@ -68,8 +68,8 @@ | |||
"@types/express": "^4.16.1", | |||
"@types/jest": "^24.0.16", | |||
"@types/jest-environment-puppeteer": "^4.0.0", | |||
"@types/react": "^16.8.4", | |||
"@types/react-dom": "^16.8.2", | |||
"@types/react": "^16.9.11", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these @types changes necessary? If not, let's revert that please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverting
package.json
Outdated
@@ -115,6 +115,7 @@ | |||
}, | |||
"peerDependencies": { | |||
"next": ">= 7.0.0", | |||
"react": ">= 16.8.0" | |||
"react": ">= 16.8.0", | |||
"react-dom": ">= 16.8.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this added?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverting
src/config/create-config.ts
Outdated
throw new Error(`Default namespace not found at ${defaultNSPath}`) | ||
// if defaultNS doesn't exist, try to fall back to the deprecated static folder | ||
// https://github.com/isaachinman/next-i18next/issues/523 | ||
if (fs.existsSync(path.join(process.cwd(), `${STATIC_LOCALE_PATH}/${defaultLanguage}/${combinedConfig.defaultNS}.${localeExtension}`))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this logic out to a variable? Something like const staticDirExists
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.
src/config/create-config.ts
Outdated
// if defaultNS doesn't exist, try to fall back to the deprecated static folder | ||
// https://github.com/isaachinman/next-i18next/issues/523 | ||
if (fs.existsSync(path.join(process.cwd(), `${STATIC_LOCALE_PATH}/${defaultLanguage}/${combinedConfig.defaultNS}.${localeExtension}`))) { | ||
console.warn('Deprecation Warning - falling back to /static folder, deprecated in next@9.1.*') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've got our own logging util in src/utils/console-message.ts
. Please use that where possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will switch over.
src/config/create-config.ts
Outdated
} | ||
|
||
} else { | ||
|
||
// remove public/ prefix from client site config | ||
const clientLocalePath = localePath.replace(/^public\//, '') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder how safe this is. We should probably check via something like localePath.startsWith('/public/')
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be a no-op if the pattern doesn't match, but couldn't hurt to check startsWith first.
Changing the default to |
Sounds good. Thanks for being flexible! |
…ocales, tests updated, changed next version back to 9.0.3 and undid the fixes that were needed for 9.1
@nathanfriemel Just let me know when this is ready for review again. |
@isaachinman I think it is ready to look at again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks pretty much ready to go. Can you update the README to reflect these changes?
__tests__/config/test-helpers.ts
Outdated
@@ -15,24 +15,24 @@ const userConfig = { | |||
defaultNS: 'universal', | |||
fallbackLng: 'it', | |||
otherLanguages: ['fr', 'it'], | |||
localePath: 'static/translations', | |||
localePath: 'public/translations', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why you didn't make this public/static/translations
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just missed it, will update it and the tests.
…ructure and updated the readme
@isaachinman README updated and updated test-helpers localePath. |
__tests__/config/test-helpers.ts
Outdated
localeStructure: '{{ns}}/{{lng}}', | ||
localeSubpaths: localeSubpathVariations.FOREIGN, | ||
} | ||
|
||
const userConfigClientSide = { | ||
...userConfig, | ||
backend: { | ||
loadPath: '/static/translations/{{ns}}/{{lng}}.json', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this change made?
__tests__/config/test-helpers.ts
Outdated
}, | ||
} | ||
|
||
const userConfigServerSide = { | ||
...userConfig, | ||
backend: { | ||
loadPath: '/home/user/static/translations/{{ns}}/{{lng}}.json', | ||
addPath: '/home/user/static/translations/{{ns}}/{{lng}}.missing.json', | ||
loadPath: '/home/user/public/translations/{{ns}}/{{lng}}.json', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again wondering why this isn't public/static
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be public/static, also the create-config.test should be changed to public/static on lines 115-116.
Looks like process.env.NODE_ENV is always 'production' in the create-config.test even with NODE_ENV=test jest
so the test never runs the Validate defaultNS
block and serverLocalePath is just whatever the test-helpers userConfig.localePath is set to. I'm not sure why NODE_ENV is production atm and interested to see if you see the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it looks like it's due to this hacky setup.
We should instead capture the original value in a var, and reassign before the end of the test:
const originalNodeEnv = process.env.NODE_ENV
process.env.NODE_ENV = 'production'
...perform test
process.env.NODE_ENV = originalNodeEnv
Modifying globals in tests is almost always problematic!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @capellini
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @isaachinman - do you need something from me?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tagged you to notify you that we'll most likely need to update these tests. If @nathanfriemel is happy to do the work, I don't think we need anything!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I'm here to assist, if needed. Just let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved the conflicts, want me to go ahead and change static to public/static?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went ahead and changed to public/static
@nathanfriemel Need to resolve a couple conflicts unfortunately. But this PR is my top priority at the moment, so I won't merge anything except any potential urgent bug fixes until we get it in. |
@nathanfriemel Let me know when this ready for re-review. Thanks! |
@isaachinman Resolved conflicts, ready to look at. |
const staticDirExists = fs.existsSync(staticDirPath) | ||
if (staticDirExists) { | ||
consoleMessage('warn', 'Falling back to /static folder, deprecated in next@9.1.*', combinedConfig) | ||
serverLocalePath = STATIC_LOCALE_PATH |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am just now realising that this fallback will only take place in non-production modes. Wouldn't that effectively break all currently-deployed apps where people rely on the default value of localePath
being the static
dir?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah you're right, will look at this again in the morning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the break, holidays and work and what not. What was the original reason for the process.env.NODE_ENV !== 'production'
here? Was it to prevent the error throw in production? I looked at the ticket in the comment and the PR for it and just want to figure out the original intent before changing anything more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem. Yes that's correct - this if
block is/was solely for non-production validation of the default namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the production check to just before the error throw, hopefully that sorts things.
Thank you @nathanfriemel for all your work on this. I merged the PR and cleaned up a few things, and have released under v3.0.0. There should be no breaking changes, just wanted to be on the safe side. |
Took a pass at adding public folder support, looking for feedback:
public/
from backend pathsclose #523