Skip to content

Commit

Permalink
too simplistic implementation multiple pages dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
alvesvin committed May 18, 2023
1 parent c790241 commit 750ab55
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from './utils'
import { LoaderOptions } from './types'
import templateAppDir from './templateAppDir'
import { removeCommentsFromCode } from './utils'

export default function loader(
this: webpack.LoaderContext<LoaderOptions>,
Expand Down Expand Up @@ -47,10 +48,8 @@ export default function loader(
return getDefaultAppJs(hasLoadLocaleFrom)
}

// Skip files that are not inside a valid page dir
if (!normalizedPagesPath) return rawCode

const page = normalizedResourcePath.replace(normalizedPagesPath, '/')
const page = normalizedResourcePath.replace(normalizedPagesPath || '', '/')
const pageNoExt = page.replace(extensionsRgx, '')
const pagePkg = parseFile(basePath, normalizedResourcePath)
const defaultExport = getDefaultExport(pagePkg)
Expand All @@ -59,19 +58,20 @@ export default function loader(
// "export default" on the page
if (!defaultExport) return rawCode

if (normalizedPagesPath.endsWith('app/')) {
if (page.endsWith('page.tsx')) {
const isClientComponent = /^['"]use client['"]/.test(removeCommentsFromCode(pagePkg.getCode()))

if (Boolean(normalizedPagesPath && normalizedPagesPath.endsWith('app/')) || isClientComponent) {
return templateAppDir(pagePkg, {
hasLoadLocaleFrom,
pageNoExt,
normalizedResourcePath,
normalizedPagesPath,
})
}

return rawCode
}

// Skip files that are not inside a valid page dir
if (!normalizedPagesPath) return rawCode

// Skip any transformation if the page is not in raw code
// Fixes issue with Nx: https://github.com/vinissimus/next-translate/issues/677
if (hasExportName(pagePkg, '__N_SSP') || hasExportName(pagePkg, '__N_SSG')) {
Expand Down
2 changes: 1 addition & 1 deletion src/templateAppDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function templateAppDirClientComponent({ code, hash, pageVariableName }: ClientT
const forceUpdate = __react.useReducer(() => [])[1]
const isClient = typeof window !== 'undefined'
if (isClient && !window.__NEXT_TRANSLATE__) {
if (isClient && !window.__NEXT_TRANSLATE__ && document.getElementById('__NEXT_TRANSLATE_DATA__')) {
window.__NEXT_TRANSLATE__ = { lang: __i18nConfig.defaultLocale, namespaces: {} }
update(false)
}
Expand Down

0 comments on commit 750ab55

Please sign in to comment.