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/next/translation memoization #5036

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/pages/CreateFirstUser/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const generateMetadata = async ({
}: {
config: Promise<SanitizedConfig>
}): Promise<Metadata> => {
const t = getNextT({
const t = await getNextT({
config: await config,
})

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/pages/ForgotPassword/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const generateMetadata = async ({
}: {
config: Promise<SanitizedConfig>
}): Promise<Metadata> => {
const t = getNextT({
const t = await getNextT({
config: await config,
})

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/pages/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const generateMetadata = async ({
}: {
config: Promise<SanitizedConfig>
}): Promise<Metadata> => {
const t = getNextT({
const t = await getNextT({
config: await config,
})

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/pages/Logout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const generateMetadata = async ({
}: {
config: Promise<SanitizedConfig>
}): Promise<Metadata> => {
const t = getNextT({
const t = await getNextT({
config: await config,
})

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/pages/ResetPassword/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const generateMetadata = async ({
}: {
config: Promise<SanitizedConfig>
}): Promise<Metadata> => {
const t = getNextT({
const t = await getNextT({
config: await config,
})

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/pages/Unauthorized/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const generateMetadata = async ({
}: {
config: Promise<SanitizedConfig>
}): Promise<Metadata> => {
const t = getNextT({
const t = await getNextT({
config: await config,
})

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/pages/Verify/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const generateMetadata = async ({
}: {
config: Promise<SanitizedConfig>
}): Promise<Metadata> => {
const t = getNextT({
const t = await getNextT({
config: await config,
})

Expand Down
7 changes: 4 additions & 3 deletions packages/next/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import config from 'payload-config'
import { createPayloadRequest } from '../utilities/createPayloadRequest'
import type { PayloadRequest } from 'payload/types'
import type { Endpoint } from 'payload/config'
import { match } from 'path-to-regexp'

import { createPayloadRequest } from '../utilities/createPayloadRequest'

import { me } from './auth/me'
import { init } from './auth/init'
import { login } from './auth/login'
Expand Down Expand Up @@ -32,8 +35,6 @@ import { docAccess as docAccessGlobal } from './globals/docAccess'
import { findVersions as findVersionsGlobal } from './globals/findVersions'
import { restoreVersion as restoreVersionGlobal } from './globals/restoreVersion'
import { findVersionByID as findVersionByIdGlobal } from './globals/findVersionByID'
import { PayloadRequest } from 'payload/types'
import { Endpoint } from 'payload/config'

const endpoints = {
root: {
Expand Down
8 changes: 4 additions & 4 deletions packages/next/src/utilities/createPayloadRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { getAuthenticatedUser } from 'payload/auth'
import { getPayload } from 'payload'
import { URL } from 'url'
import { parseCookies } from 'payload/auth'
import { initI18n } from '@payloadcms/translations'
import { getRequestLanguage } from './getRequestLanguage'
import { getRequestLocales } from './getRequestLocales'
import { getNextI18n } from './getNextI18n'
import { getDataAndFile } from './getDataAndFile'

type Args = {
Expand Down Expand Up @@ -61,10 +61,10 @@ export const createPayloadRequest = async ({
cookies,
})

const i18n = getNextI18n({
config,
const i18n = await initI18n({
config: config.i18n,
language,
translationContext: 'api',
translationsContext: 'api',
})

const customRequest: CustomPayloadRequest = {
Expand Down
20 changes: 0 additions & 20 deletions packages/next/src/utilities/getNextI18n.ts

This file was deleted.

17 changes: 8 additions & 9 deletions packages/next/src/utilities/getNextT.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { translations } from '@payloadcms/translations/client'
import type { TFunction } from '@payloadcms/translations'

import type { SanitizedConfig } from 'payload/types'

import { initTFunction } from '@payloadcms/translations'
import { initI18n } from '@payloadcms/translations'
import { cookies, headers } from 'next/headers'
import { getRequestLanguage } from './getRequestLanguage'

export const getNextT = ({
export const getNextT = async ({
config,
language,
}: {
config: SanitizedConfig
language?: string
}): TFunction => {
const lang = language || getRequestLanguage({ cookies: cookies(), headers: headers() })

return initTFunction({
language: lang,
}): Promise<TFunction> => {
const i18n = await initI18n({
translationsContext: 'client',
language: language || getRequestLanguage({ cookies: cookies(), headers: headers() }),
config: config.i18n,
translations,
})

return i18n.t
}
11 changes: 8 additions & 3 deletions packages/next/src/utilities/initPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import type {
} from 'payload/types'
import { redirect } from 'next/navigation'
import { parseCookies } from 'payload/auth'
import { getNextI18n } from './getNextI18n'
import { getRequestLanguage } from './getRequestLanguage'
import { findLocaleFromCode } from '../../../ui/src/utilities/findLocaleFromCode'
import { I18n } from '@payloadcms/translations/types'
import { initI18n } from '@payloadcms/translations'

export const initPage = async ({
configPromise,
Expand All @@ -31,7 +32,7 @@ export const initPage = async ({
permissions: Awaited<ReturnType<typeof auth>>['permissions']
user: Awaited<ReturnType<typeof auth>>['user']
config: SanitizedConfig
i18n: ReturnType<typeof getNextI18n>
i18n: I18n
collectionConfig?: SanitizedCollectionConfig
globalConfig?: SanitizedGlobalConfig
locale: ReturnType<typeof findLocaleFromCode>
Expand Down Expand Up @@ -59,7 +60,11 @@ export const initPage = async ({
config: configPromise,
})

const i18n = getNextI18n({ config, language })
const i18n = await initI18n({
config: config.i18n,
language,
translationsContext: 'client',
})
let collectionConfig: SanitizedCollectionConfig
let globalConfig: SanitizedGlobalConfig

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function localForgotPassword<T extends keyof GeneratedTypes['collections']
data,
disableEmail,
expiration,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/auth/operations/local/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function localLogin<TSlug extends keyof GeneratedTypes['collections']>(
data,
depth,
overrideAccess,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function localResetPassword<T extends keyof GeneratedTypes['collections']>
collection,
data,
overrideAccess,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/auth/operations/local/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function localUnlock<T extends keyof GeneratedTypes['collections']>(
collection,
data,
overrideAccess,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/auth/operations/local/verifyEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function localVerifyEmail<T extends keyof GeneratedTypes['collections']>(

return verifyEmailOperation({
collection,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
token,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default async function createLocal<TSlug extends keyof GeneratedTypes['co
)
}

const req = createLocalReq(options, payload)
const req = await createLocalReq(options, payload)
req.file = file ?? (await getFileByPath(filePath))

return createOperation<TSlug>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function deleteLocal<TSlug extends keyof GeneratedTypes['collections']>(
collection,
depth,
overrideAccess,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
where,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/collections/operations/local/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default async function findLocal<T extends keyof GeneratedTypes['collecti
overrideAccess,
page,
pagination,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
sort,
where,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default async function findByIDLocal<T extends keyof GeneratedTypes['coll
disableErrors,
draft,
overrideAccess,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default async function findVersionByIDLocal<T extends keyof GeneratedType
depth,
disableErrors,
overrideAccess,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default async function findVersionsLocal<T extends keyof GeneratedTypes['
limit,
overrideAccess,
page,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
sort,
where,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default async function restoreVersionLocal<T extends keyof GeneratedTypes
depth,
overrideAccess,
payload,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async function updateLocal<TSlug extends keyof GeneratedTypes['collections']>(
)
}

const req = createLocalReq(options, payload)
const req = await createLocalReq(options, payload)
req.file = file ?? (await getFileByPath(filePath))

const args = {
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/globals/operations/local/findOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default async function findOneLocal<T extends keyof GeneratedTypes['globa
draft,
globalConfig,
overrideAccess,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
slug: globalSlug as string,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default async function findVersionByIDLocal<T extends keyof GeneratedType
disableErrors,
globalConfig,
overrideAccess,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default async function findVersionsLocal<T extends keyof GeneratedTypes['
limit,
overrideAccess,
page,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
sort,
where,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default async function restoreVersionLocal<T extends keyof GeneratedTypes
depth,
globalConfig,
overrideAccess,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
})
}
2 changes: 1 addition & 1 deletion packages/payload/src/globals/operations/local/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default async function updateLocal<TSlug extends keyof GeneratedTypes['gl
draft,
globalConfig,
overrideAccess,
req: createLocalReq(options, payload),
req: await createLocalReq(options, payload),
showHiddenFields,
slug: globalSlug as string,
})
Expand Down
7 changes: 3 additions & 4 deletions packages/payload/src/translations/getLocalI18n.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { initI18n } from '@payloadcms/translations'
import { translations } from '@payloadcms/translations/api'

import type { SanitizedConfig } from '../exports/types'

export const getLocalI18n = ({
export const getLocalI18n = async ({
config,
language = 'en',
}: {
config: SanitizedConfig
language?: string
}): ReturnType<typeof initI18n> =>
}) =>
initI18n({
config: config.i18n,
language,
translations,
translationsContext: 'api',
})
6 changes: 3 additions & 3 deletions packages/payload/src/utilities/createLocalReq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ type CreateLocalReq = (
user?: Document
},
payload: Payload,
) => PayloadRequest
export const createLocalReq: CreateLocalReq = (
) => Promise<PayloadRequest>
export const createLocalReq: CreateLocalReq = async (
{ context, fallbackLocale, locale, req = {} as PayloadRequest, user },
payload,
) => {
const i18n = req?.i18n || getLocalI18n({ config: payload.config })
const i18n = req?.i18n || (await getLocalI18n({ config: payload.config }))

if (payload.config?.localization) {
const defaultLocale = payload.config.localization.defaultLocale
Expand Down
Loading
Loading