-
-
Notifications
You must be signed in to change notification settings - Fork 764
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
Support for getServerSideProps
#652
Comments
NextJs 9.3 was released a week ago. None of the new data fetching methods are supported yet, but in general I'd lean towards The |
I'm kind of newbie so sorry for asking this question. As I've figured out your amazing library currently fully supports Next js 9.2, right? Can I start my project using Next js 9.2 and stick to next-i18next 4.2.1 for the next few months? |
It fully supports NextJs 9.3 as well, just not the new data fetching methods. |
That is entirely false. That'd be a breaking change, and would go against semver. Read the docs.
|
May I know if there is any schedule for supporting |
I do not currently have much time for development on this project due to other factors in life. I don't see this changing for the next few weeks, at least. As I've stated, |
Just noticed that you can use |
@maxmalov Can you explain what you mean? We still have a dependency on the i18next browser detector middleware. |
import i18next from 'i18next';
const createI18Next = (lang) => {
// create & configure i18next instance
}
export const serverSideMethod = ({ params, req, res }) => {
// detect lang from req somehow (probably can reuse express middleware)
const lang = detect(req);
const i18n = createI18Next(lang);
const { t } = i18n;
// do whatever you want with t
} So later one can use // somewhere in pages directory
import { serverSideMethod } from '...';
export const getServerSideProps = ctx => serverSideMethod(ctx)
export default props => { ... } For now, it's just a theory. We're working on the AMP first app and have to reuse the existing 18next resources we use in the separate single-page app. So in a few days, we eventually find out whether it works or not |
Yes, that approach will work if you want to roll your own implementation, but has nothing to do with this package. Let's keep discussion here on topic. |
Any updates here?
Thanks |
Hey @pettomartino - no update from my side. You can always silence warnings by setting |
@isaachinman thank for so quick reply. Gonna try to implement something then. Any hints? |
@pettomartino Do you mean contributing to |
@pettomartino there is a demonstration that the i18n solution works in SSG, Hope it helps. |
since we cant use serverSideProps together with initialProps. we can make use of React FunctionComponent.defaultProps
|
this works. thanks! |
This comment has been minimized.
This comment has been minimized.
I'm using this version, so that it picks up recursive namespaces from WrappedComponents when using HOC MyApp.getInitialProps = async (appContext: AppContextType<Router>) => {
const recursiveNamespaces = (component: NextComponentType & { WrappedComponent?: NextComponentType }, acc: string[] = []): string[] => {
const ns = component.defaultProps?.i18nNamespaces;
const a = (typeof ns === "string") ? [...acc, ns] : [...acc, ...(ns || [])];
let wrappedComponent = component.WrappedComponent;
if (wrappedComponent) {
return recursiveNamespaces(wrappedComponent, a)
} else
return a
}
const appProps = await App.getInitialProps(appContext)
const namespaces: any = recursiveNamespaces(appContext.Component)
return {
...appProps,
pageProps: {
namespacesRequired: [
...(appProps.pageProps.namespacesRequired || []),
...namespaces]
}
}
} It allows to use your defaultProps like this: TotoComponent.defaultProps = {
i18nNamespaces: ['toto','bar']
}
export default withTranslation('toto')(TotoComponent); |
@rparree I think you're commenting on the wrong issue. This issue is about |
@isaachinman I was referring to the comment with the workaround submitted by @icrona above (#652 (comment)) |
Sorry, what exactly is that workaround achieving? |
With the solution in their comment you can use |
As far as I can tell, that doesn't achieve anything except slightly modifying the API. It won't enable SSG. |
how would that work for typescript? |
@isaachinman It does the trick for me. I am able to use a component like this (and it will be SSR) export const getServerSideProps = async function (ctx: NextPageContext) {
…
return {
props: {
totoProp : ""
}
}
}
TotoComponent.defaultProps = {
i18nNamespaces: ['toto','bar']
}
export default withTranslation('toto')(TotoComponent); |
This approach breaks SSR for me. Only the keys are loaded initially and after a delay they are replaced by the translation. Edit: nvm this works great, I just missed that the defaultProp is called |
To get all namespaces loaded in case none are defined on the page, you would have to adapt icronas solution like this: NextApp.getInitialProps = async (appContext) => {
const appProps = await App.getInitialProps(appContext);
const { defaultProps } = appContext.Component;
let pageProps = {}
if(appProps.pageProps.namespacesRequired || defaultProps?.i18nNamespaces) {
pageProps = {
namespacesRequired: [...(appProps.pageProps.namespacesRequired || []), ...(defaultProps?.i18nNamespaces || [])],
}
}
return {
...appProps,
pageProps
};
}; |
@mattcrn The |
@isaachinman yes, but using icrona's solution will break this functionality since it will return an empty array for |
As far as I understand it, getServerSideProps is not part of the page component, so we would have to create a higher order function to wrap the pages getServerSideProps. Or is there a better way? |
* feat(tech): fetch a single tech by id or slug * refactor(techs): extract logic to query scope * feat(button): add info variant * feat(tech): map technology object * feat(tech): initial header design * feat(button): improve font-size * feat(tech): create provider * fix(button): proptype variants * feat(tech): useTechnology hook * refactor(tech): componentize * feat(tech): add react-tabs package * feat(tech): make it responsiveness * feat(tech): improve styles * feat(tabs): extract components * chore(deps): remove unused package * feat(tech): show data with filters * feat(tech): add optional params * style(tech): tabs content title tks @lcnogueira 🙂 * refactor(tech): rename components * style(tech): description value * style(tech): improve title container metrics * fix(tech): error when not found * style(tech): hide buttons container * chore(tech): add col wrapper to future updates * chore(tech): getInitialProps to getServerSideProps * chore(tech): code style * feat(tech): render error component when necessary * test(tech): initial fetch feature * test(tech): fix fetch and expected status * test(tech): making assertions * refactor(tech): prevent error on build move styles from dedicated file to internal component file tks @lcnogueira 👍 * style(tech): improve titles * style(tech): image shadow * style(tech): improve metrics and colors * fix: i18n namespaces with getServerSideProps tks @icrona based on i18next/next-i18next#652 * style(tech): improve margins * style(tech): fix tab title width * style(tech): improve padding * feat(helpers): unslugify * chore: remove unslugify * feat(tech): list some taxonomies * fix(edit): trying to fix tests * style(tech): improve readability * fix(home): technologies list * fix: prevent erros * refactor(tech): extract components to another path * feat(tech): only render if section has children * refactor(tabs): make props more readable * refactor(tech): styles * test(hooks): useTechnology coverage * fix(techs): error when database is empty resolver #305 * fix(techs): error when database is empty resolves #305 * test(tech): remove unnecessary data * fix(error): propTypes validation resolves #324 * tests(edit/new): trying to fix * fix: 🔥 remove getServerSideProps 💥 also add 't' technology prefix * style(tech): remove css vendor prefix * feat(tech): reactivate buttons * refactor(tech): early bail out if * test(tech): remove unnecessary mock properties * refactor(tabs): use global row and cell * refactor(tech): early bail out if * fix(tech): mobile title responsivity * refactor(tech): rename section component * feat(error): define 400 as default error code * refactor(home): technologies as object of arrays * feat(tech): normalize taxonomies * test(tech): normalize taxonomies * refactor(tech): redirect to error page when not found * test(tech): redirect to 404 * chore(tech): remove unused prop * fix(card): redirect without refresh page resolves #328 * Quick fixces * Increasing timeouts * Updating snapshots * Adding a wait period before fetching last email Co-authored-by: Nícholas Oliveira <[email protected]>
I think when this feature lands, it would be ideal to support both |
@ckeeney Agreed. |
Any update since then ? |
@divlo Feel free to submit a PR. |
+1 |
Well, i thought it would work good, but it works with delay: At the first loading, it does not load the Inspecting it for the first time we get this: After reloading, we can find the required namespace: Both namespaces are in the option, but just the first one, defined on Why does this happen? |
@SalahAdDin This will be solved by #869. |
Fixed in |
Thanks a lot for this @isaachinman |
@isaachinman Does the new release mean we must deleted the whole workaround for this matter and use the new hook? It is not so much clear for me. |
@SalahAdDin Correct, there is no need for a workaround in v8. |
Import the serverSideTranslations
Now from the getServerSideProps, pass the .
Now add your language string inside For example
You can add more language strings inside the locales directory. Restart your Next.js app. It will work. |
Is your feature request related to a problem? Please describe.
When using
getServerSideProps
as opposed togetInitialProps
results in anamespacesRequired
warning, despite the fact that I am returningnamespacesRequired
inside the requiredprops
keyI don't know if this is factual or not, or if the warning is a red herring
Describe the solution you'd like
Ideally I'd like
getServerSideProps
to be supported.Apologies if this is vague or unclear, or a completely unreasonable request.
Thanks in advance
The text was updated successfully, but these errors were encountered: