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

feat: Public dir support #569

Merged
merged 16 commits into from Dec 24, 2019
Merged

Conversation

nathanfriemel
Copy link
Contributor

@nathanfriemel nathanfriemel commented Nov 22, 2019

Took a pass at adding public folder support, looking for feedback:

  • Changed default config localePath to public/static/locales
  • Updated create config to look for combinedConfig localePath and if not found try to fall back to static/locales and print a deprecation console warning in the server config; client config attempts to remove public/ from backend paths
  • Updated tests

close #523

@nathanfriemel nathanfriemel changed the title Fixes #523, Public dir support Fix: Public dir support #523 Nov 22, 2019
@nathanfriemel nathanfriemel changed the title Fix: Public dir support #523 feature: Public dir support Nov 22, 2019
@nathanfriemel nathanfriemel changed the title feature: Public dir support feat: Public dir support Nov 22, 2019
@nathanfriemel
Copy link
Contributor Author

Didn't realize there was already a PR for this, feel free to close this one.

@isaachinman
Copy link
Contributor

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!

@nathanfriemel
Copy link
Contributor Author

Return types are gone.

Copy link
Contributor

@isaachinman isaachinman left a 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.

"next-i18next": "link:../../",
"react": "link:../../node_modules/react",
"react-dom": "link:../../node_modules/react-dom"
"next-i18next": "link:../../"
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

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",
Copy link
Contributor

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.

Copy link
Contributor Author

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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverting

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}`))) {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

// 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.*')
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will switch over.

}

} else {

// remove public/ prefix from client site config
const clientLocalePath = localePath.replace(/^public\//, '')
Copy link
Contributor

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/').

Copy link
Contributor Author

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.

@nathanfriemel
Copy link
Contributor Author

Changing the default to public/static/locales would probably simplify things. I'm pretty sure the Next blog suggests just moving the static folder into the public folder so that you don't have to update import paths across your app. I can make that change and probably undo a lot of the test changes in the process.

@isaachinman
Copy link
Contributor

Sounds good. Thanks for being flexible!

@isaachinman
Copy link
Contributor

@nathanfriemel Just let me know when this is ready for review again.

@nathanfriemel
Copy link
Contributor Author

@isaachinman I think it is ready to look at again.

Copy link
Contributor

@isaachinman isaachinman left a 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?

@@ -15,24 +15,24 @@ const userConfig = {
defaultNS: 'universal',
fallbackLng: 'it',
otherLanguages: ['fr', 'it'],
localePath: 'static/translations',
localePath: 'public/translations',
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@nathanfriemel
Copy link
Contributor Author

@isaachinman README updated and updated test-helpers localePath.

localeStructure: '{{ns}}/{{lng}}',
localeSubpaths: localeSubpathVariations.FOREIGN,
}

const userConfigClientSide = {
...userConfig,
backend: {
loadPath: '/static/translations/{{ns}}/{{lng}}.json',
Copy link
Contributor

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?

},
}

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',
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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?

Copy link
Contributor

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!

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor Author

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?

Copy link
Contributor Author

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

src/utils/console-message.ts Outdated Show resolved Hide resolved
yarn.lock Outdated Show resolved Hide resolved
@isaachinman
Copy link
Contributor

@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.

@isaachinman
Copy link
Contributor

@nathanfriemel Let me know when this ready for re-review. Thanks!

@nathanfriemel
Copy link
Contributor Author

@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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@isaachinman isaachinman merged commit 99f82cd into i18next:master Dec 24, 2019
@isaachinman
Copy link
Contributor

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.

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

Successfully merging this pull request may close these issues.

Add support for "public" directory
3 participants